【问题标题】:Thunderbird doesn't show embedded images in email [duplicate]Thunderbird 不显示电子邮件中的嵌入图像 [重复]
【发布时间】:2017-08-10 19:45:35
【问题描述】:

我正在使用以下代码 (C#) 将图像嵌入到 html 电子邮件中:

Bitmap image = new Bitmap();
// set something to image
MemoryStream ms = new MemoryStream();
image.Save(ms, ImageFormat.Jpeg);
ContentType contentType = new ContentType(MediaTypeNames.Image.Jpeg);
ms.Seek(0, SeekOrigin.Begin);
Attachment attachment = new Attachment(ms, contentType);
attachment.ContentId = "image@email";

MailMessage mail = new MailMessage();
mail.From = new MailAddress("...");
mail.To.Add("...");
mail.Subject = "...";
mail.Body = body; // view below
mail.IsBodyHtml = true;
mail.Attachments.Add(attachment);
smtpClient.Send(mail);
ms.Close();

这里是html正文的相关部分:

<img align='center' alt='' src='cid:image@email' width='100' style='padding-bottom: 0; display: inline !important; vertical-align: bottom;'>";            

在 gmail 网站上查看电子邮件看起来是正确的:即图像嵌入到 html 正文中。

相反,使用 Thunderbird 时,图像会作为附件接收,但不会被识别为 jpg。附件名为“第 1.2 部分”。如果我双击它,则使用计算机上安装的默认图像查看器打开图像。

为了以与最常见的电子邮件阅读器兼容的方式正确发送嵌入图像,我需要添加什么?

更新

要为附加的图像命名,添加以下内容就足够了:

attachment.Name = "name.jpg";

Thunderbird 仍然拒绝在 html 中显示它。

【问题讨论】:

    标签: c# html email


    【解决方案1】:

    正如 Scarnet 所指出的,这是将嵌入图像放入 html 电子邮件的正确方法:

    AlternateView view = AlternateView.CreateAlternateViewFromString(body, null, MediaTypeNames.Text.Html);
    LinkedResource inline = new LinkedResource(filename, MediaTypeNames.Image.Jpeg);
    inline.ContentId = "image@email";
    view.LinkedResources.Add(inline);
    
    MailMessage mail = new MailMessage();
    mail.From = new MailAddress(username);
    mail.To.Add(address);
    mail.Subject = subject;
    mail.IsBodyHtml = true;
    mail.AlternateViews.Add(view);
    await smtpClient.SendMailAsync(mail);
    

    【讨论】:

      猜你喜欢
      • 2013-04-27
      • 2019-02-02
      • 2015-02-17
      • 1970-01-01
      • 2015-06-29
      • 1970-01-01
      • 2017-06-11
      • 2020-05-28
      • 2012-06-23
      相关资源
      最近更新 更多