簡體   English   中英

如何刪除圖像作為附件,但顯示在電子郵件正文中

[英]How to remove image as attachment but show in body of email

我找到了這個解決方案,用於在電子郵件正文中顯示圖像: 將圖像添加到電子郵件正文中

它工作正常,但它也添加圖像作為電子郵件的附件。

Attachment inlineLogo = new Attachment(EmailLogo.ImageUrl);
mailMsg.Attachments.Add(inlineLogo);
string contentID = "Image";
inlineLogo.ContentId = contentID;

//To make the image display as inline and not as attachment
inlineLogo.ContentDisposition.Inline = true;
inlineLogo.ContentDisposition.DispositionType = DispositionTypeNames.Inline;

//To embed image in email
mailMsg.Body = "<htm><body> <img height=\"49\" width=\"169\" src=\"cid:" + contentID + "\"> </body></html>";

有一行代碼,評論顯示為內聯而非附件,但此行無法正常工作,因為圖片仍會附加到電子郵件中:

//To make the image display as inline and not as attachment
inlineLogo.ContentDisposition.Inline = true;
inlineLogo.ContentDisposition.DispositionType = DispositionTypeNames.Inline;

如何阻止圖像附加到電子郵件?

使用AlternateView存儲您的html代碼,其中圖像嵌入為LinkedResource

string contentID = "Image";

var inlineLogo = new LinkedResource(EmailLogo.ImageUrl, "image/png");    
inlineLogo.ContentId = contentID;

var htmlView = AlternateView.CreateAlternateViewFromString(
    "<html><body> <img height=\"30\" width=\"30\" src=\"cid:" + contentID + "\"> </body></html>",
    Encoding.UTF8, "text/html");
htmlView.TransferEncoding = TransferEncoding.QuotedPrintable;
htmlView.LinkedResources.Add(inlineLogo);

mailMsg.AlternateViews.Add(htmlView);

PS嵌入圖像作為base24字符串不是很好主意,因為許多郵件客戶端不支持這種能力。

如果要在電子郵件中顯示圖像,則必須存在於某處。 它作為消息有效負載的一部分附加(無論是“顯示為內聯”還是作為真正的“附件”) - 或者在讀取器讀取電子郵件時從遠程Web服務器獲取(並且可選地必須選擇“查看圖片”)

要不將圖像附加到電子郵件有效負載本身:

  1. 您必須在公共Web服務器上托管映像,以便打開郵件的讀者可以訪問它。
  2. 您必須在郵件正文源中使用完全限定的URL,以便它可以找到它。

假設您已將圖像存儲在Web服務器上的以下URL: http//www.example.com/images/myimage.jpg

...那么您的來源應該只是改變以反映:

mailMsg.Body = "<html><body> <img height=\"49\" width=\"169\" src=\"http://www.example.com/images/myimage.jpg\"> </body></html>";

根本不需要附加它。

可以使用的替代方法是嵌入圖像,但通常也不會被電子郵件客戶端過濾(現在通常就像垃圾郵件這樣)您可以使用DataURL。

<img src="data:image/<type>;base64,<string>"/>

其中<type>是圖像類型,即jpg,gif,png,並且是base64字符串。 只需將圖像轉換為base64字符串,然后使用上述語法將其分配給源

例如,使用jpeg ...

mailMsg.Body = "<html><body> <img height=\"49\" width=\"169\" src=\"data:image/jpg;base64,<myPictureString>"\"> </body></html>";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM