1

I'm trying to send emails with embedded images using MailKit. It works well on MS Outlook. However, images do not display as embedded images in Gmail. I tried to attach with "data:image/png;base64," format to it also worked on Outlook but not Gmail.

Any help would be much appreciated!

public string SendEmail (MyModel myModel)
{
    var message = new MimeMessage();
    var bodyBuilder = new BodyBuilder
    {
        HtmlBody = myModel.Body
    };
    GetAllImgTags(bodyBuilder);
    message.Body = bodyBuilder.ToMessageBody();
    // Send Email Here...
    return "OK"
}

    public string GetAllImgTags(BodyBuilder bodyBuilder)
    {
        HtmlDocument document = new HtmlDocument();

        document.LoadHtml(bodyBuilder.HtmlBody);

        var imgList = document.DocumentNode.Descendants("img").Where(x =>
        {
            string src = x.GetAttributeValue("src", null) ?? "";
            return !string.IsNullOrEmpty(src);
        }).ToList();

        foreach(var item in imgList)
        {
            string currentSrcValue = item.GetAttributeValue("src", null);
            var file = Path.Combine(_env.WebRootPath,"images", currentSrcValue);
            if (File.Exists(file))
            {                    
                byte[] imageData = System.IO.File.ReadAllBytes(file);
                string contentId = string.Format("{0}@{1}", Path.GetFileName(file), Guid.NewGuid().ToString());
                LinkedResource inline = new LinkedResource(new MemoryStream(imageData), Image.Jpeg)
                {
                    ContentId = contentId,
                    TransferEncoding = TransferEncoding.Base64,
                    ContentLink = new Uri("cid:" + contentId),

                };
                inline.ContentType.Name = contentId;
                inline.ContentType.MediaType = Image.Jpeg;
                bodyBuilder.LinkedResources.Add(contentId, new MemoryStream(imageData));

                item.SetAttributeValue("src", "cid:" + contentId);
                inline.Dispose();
            }
        }

        bodyBuilder.HtmlBody = document.DocumentNode.OuterHtml;
        string result = document.DocumentNode.OuterHtml;
        return result;
    }

enter image description here

ecasper
  • 489
  • 1
  • 10
  • 30

1 Answers1

5

Well, first of all, why are you creating System.Net.Mail objects and then simply disposing them without using them in any way?

See this code for what I mean:

LinkedResource inline = new LinkedResource(new MemoryStream(imageData), Image.Jpeg)
{
    ContentId = contentId,
    TransferEncoding = TransferEncoding.Base64,
    ContentLink = new Uri("cid:" + contentId),
};
inline.ContentType.Name = contentId;
inline.ContentType.MediaType = Image.Jpeg;

Let's try this instead:

var contentType = new ContentType ("image", "jpeg");
var contentId = MimeKit.Utils.MimeUtils.GenerateMessageId ();
var image = (MimePart) bodyBuilder.LinkedResources.Add (file, contentType);
image.ContentTransferEncoding = ContentEncoding.Base64;
image.ContentId = contentId;

item.SetAttributeValue ("src", "cid:" + contentId);
jstedfast
  • 35,744
  • 5
  • 97
  • 110
  • It worked, thank you so much. I spent two days looking for a solution and tried many different things in vain. by the way, what's the use of image obj? – ecasper Nov 06 '19 at 03:20