繁体   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