Question

When I email myself sample mhtml files (e.g. from here) images display fine in Outlook. However, when I convert a Word document to mht (Web Archive) format, the images do not display. If I open the file in a browser, the images display fine, or if I attach the mht file and double click on the attachment. But if the file is inlined in the email, then I get the red X box with 'Right click here to download pictures', and if I select download pictures, then 'file can not be displayed...may have moved...'.

Any ideas why images in Word docs converted to MHTML do not like to display inline in emails?

Was it helpful?

Solution

An MHTML document is a multi-part MIME document. The first part of the document is HTML and has links to the images in the other parts. The problem is that the links don't work in an inline email even though they do work in a browser. Looking at some examples, you can see the links must be prefixed by "cid:", and the part after the "cid:" must have a Content-ID in the header of the corresponding MIME part.

The link can be as simple as "cid:image002.gif" with the Content-ID in the corresponding MIME part being:

Content-ID: <image002.gif>

If all of the links are fixed in this way, the html with the images will display inline in Outlook.

OTHER TIPS

As mentioned above, you use the Content ID to link attachments to image tags within the HTML body of your email. Below is a full program for opening an MHT file, adjusting the links, and emailing the results.

I have a client that is using the Word Automation Service to convert incoming emails to MHT files and emailing them. The issue is that Outlook didn't care much for the raw MHT and didn't inline the images. Here is my POC for a solution. I utilized the MimeKit and MailKit (http://www.mimekit.net/) in the code, the Bouncy Castle C# API (http://www.bouncycastle.org/csharp/) to cover a dependency within the MailKit, and Antix SMTP Server for Developers (http://antix.co.uk/Projects/SMTP-Server-For-Developers) running on the local server to receive the SMTP traffic for testing the code in dev. Below is the POC code that opens an existing MHT file and emails it with embedded images.

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];
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top