【问题标题】:How to remove image as attachment but show in body of email如何删除图像作为附件但显示在电子邮件正文中
【发布时间】:2017-05-17 17:00:36
【问题描述】:

我找到了在电子邮件正文中显示图像的解决方案: 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;

如何阻止图像附加到电子邮件?

【问题讨论】:

标签: c# asp.net email email-attachments


【解决方案1】:

使用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);

附:将图片嵌入为 base24 字符串并不是一个好主意,因为很多邮件客户端不支持这种能力。

【讨论】:

    【解决方案2】:

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

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

    1. 您必须将图像托管在公共网络服务器上,以便打开消息的读者可以访问它。
    2. 您必须在消息正文源中使用完全限定的 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>";
    

    根本不需要附加它。

    【讨论】:

      【解决方案3】:

      可以使用内嵌图像但通常不被电子邮件客户端过滤的替代方法是(今天在垃圾邮件等情况下通常是这种情况)您可以使用 DataURL。

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

      其中&lt;type&gt; 是图像类型,即jpg、gif、png,并且是base64 字符串。只需将图像转换为 base64 字符串并使用上述语法将其分配给源

      例如,使用 jpeg...

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

      【讨论】:

        猜你喜欢
        • 2016-08-02
        • 2013-05-27
        • 2011-09-06
        • 2012-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-29
        相关资源
        最近更新 更多