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];
}
}
}