【问题标题】:Images in MHT files from MS Word do not display in email来自 MS Word 的 MHT 文件中的图像不会显示在电子邮件中
【发布时间】:2012-02-17 00:54:00
【问题描述】:

当我通过电子邮件向自己发送示例 mhtml 文件(例如来自 here)时,图像在 Outlook 中显示良好。但是,当我将 Word 文档转换为 mht(Web 存档)格式时,图像不显示。如果我在浏览器中打开文件,图像显示正常,或者如果我附加 mht 文件并双击附件。但是如果文件是内联在电子邮件中,那么我会看到红色的 X 框“右键单击此处下载图片”,如果我选择下载图片,那么“文件无法显示...可能已移动... '。

任何想法为什么转换为 MHTML 的 Word 文档中的图像不喜欢在电子邮件中内联显示?

【问题讨论】:

    标签: outlook ms-word mhtml webarchive


    【解决方案1】:

    MHTML 文档是一个多部分的 MIME 文档。文档的第一部分是 HTML,并具有指向其他部分中的图像的链接。问题是链接不能在内联电子邮件中工作,即使它们在浏览器中工作。查看some examples,可以看到链接必须以“cid:”为前缀,并且“cid:”之后的部分必须在对应的MIME部分的header中有Content-ID。

    链接可以像“cid:image002.gif”一样简单,对应的 MIME 部分中的 Content-ID 为:

    Content-ID: <image002.gif>
    

    如果所有链接都以这种方式固定,带有图像的 html 将在 Outlook 中内嵌显示。

    【讨论】:

      【解决方案2】:

      如上所述,您可以使用内容 ID 将附件链接到电子邮件 HTML 正文中的图像标签。以下是用于打开 MHT 文件、调整链接和通过电子邮件发送结果的完整程序。

      我有一个客户正在使用 Word Automation Service 将收到的电子邮件转换为 MHT 文件并通过电子邮件发送它们。问题是 Outlook 不太关心原始 MHT 并且没有内联图像。这是我的 POC 解决方案。我在代码中使用了 MimeKit 和 MailKit (http://www.mimekit.net/),使用 Bouncy Castle C# API (http://www.bouncycastle.org/csharp/) 来覆盖 MailKit 中的依赖关系,以及在本地服务器上运行的 Antix SMTP Server for Developers (http://antix.co.uk/Projects/SMTP-Server-For-Developers)接收 SMTP 流量以测试 dev 中的代码。以下是打开现有 MHT 文件并通过电子邮件发送嵌入图像的 POC 代码。

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.IO;
      using System.Threading.Tasks;
      using MimeKit;
      using MailKit;
      using MimeKit.Utils;
      
      namespace ConsoleApplication3
      {
          class Program
          {
              static void Main(string[] args)
              {
                  MimeMessage messageMimeKit = MimeMessage.Load(@"c:\test.mht");
                  var images = messageMimeKit.BodyParts.Where(x => x.ContentLocation.LocalPath.EndsWith("png"));
                  var bodyString = messageMimeKit.HtmlBody;
                  var builder = new BodyBuilder();
                  foreach (var item in images)
                  {
                      item.ContentId = MimeUtils.GenerateMessageId();
                      bodyString = bodyString.Replace(GetImageName(item), "cid:" + item.ContentId.ToString());
                      builder.LinkedResources.Add(item);
                  }
                  builder.HtmlBody = bodyString;
                  messageMimeKit.Body = builder.ToMessageBody();
      
                  messageMimeKit.From.Add(new MailboxAddress("from address", "NoReply_SharePoint2013Dev@smithmier.com"));
                  messageMimeKit.To.Add(new MailboxAddress("to address", "larry@smithmier.com"));
                  messageMimeKit.Subject = "Another subject line";
                  using (var client = new MailKit.Net.Smtp.SmtpClient())
                  {
                      client.Connect("localhost");
                      client.Send(messageMimeKit);
                      client.Disconnect(true);
                  }
              }
      
              private static string GetImageName(MimeEntity item)
              {
                  return item.ContentLocation.Segments[item.ContentLocation.Segments.Count() - 2] +
                                      item.ContentLocation.Segments[item.ContentLocation.Segments.Count() - 1];
              }
          }
      }
      

      【讨论】:

      • 找出问题所在后,我用 perl 写了一些东西,将 Word 文档转换为 mhtml,修复链接,然后通过电子邮件发送。
      猜你喜欢
      • 1970-01-01
      • 2013-04-21
      • 2012-06-23
      • 2013-04-27
      • 2013-09-13
      • 2016-12-31
      • 1970-01-01
      • 2020-08-20
      • 1970-01-01
      相关资源
      最近更新 更多