2017-01-03 43 views
10

的身体表现我发现这个解决方案显示在电子邮件的正文中的图像: Add image to body of an 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; 

哪有我停止将图像附加到电子邮件中?

+1

请看到这个帖子:http://stackoverflow.com/questions/18358534/send-inline-image-in-email – Creep

+1

是L从'缺少'mailMsg.Body =“一个错字“;'行,或者它出现在你的主应用程序中? – Takarii

+1

看看[这篇文章](http://stackoverflow.com/a/32767496/3110834)的一些选项。 –

回答

1

另一种可以嵌入图像的方式嵌入图像,但通常也不会被电子邮件客户端过滤掉(现在通常情况下就是垃圾邮件)您可以使用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>"; 
3

使用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字符串并不是一个好主意,因为许多邮件客户端不支持这种功能。

1

如果要在电子邮件中显示图像,它必须存在某处。它可以作为消息有效负载的一部分(不管它是“内联显示”还是作为真正的“附件”),或者在读取器读取电子邮件时从远程Web服务器获取(并且可以选择“查看图像”)

要将图像不附加到电子邮件负载本身:

  1. 你必须拥有一个公共的Web服务器上的图像,让读者打开邮件可以访问它。
  2. 您必须在消息正文源中使用完全限定的URL,以便能够找到它。

假设你已经存储在图像Web服务器上的以下网址: 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>"; 

无需附加它根本就没有。