Search The ForumSearch   RegisterRegister  LoginLogin

MailBee.NET SMTP

 AfterLogic Forum : MailBee.NET SMTP
Subject Topic: Convert Embedded Images to Attachments Post ReplyPost New Topic
Author
Message << Prev Topic | Next Topic >>
Deantwo
Newbie
Newbie
Avatar

Joined: 10 January 2018
Location: Denmark
Online Status: Offline
Posts: 5
Posted: 12 January 2018 at 7:06am | IP Logged Quote Deantwo

Since some mail clients don't support embedded images in HTML mails (see this thread), I had to extract the embedded images and attach them as inline attachments.

Just thought I would share what I ended up doing here for others to use.

C# code:
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(_body);
foreach (HtmlAgilityPack.HtmlNode img in doc.DocumentNode.SelectNodes("//img"))
{
    string src = img.GetAttributeValue("src", null);
    if (string.IsNullOrEmpty(src))
        continue;

    // Embedded inline image.
    if (src.StartsWith("data:"))
    {
        int p1 = src.IndexOf(';'), p2 = src.IndexOf(',');
        string mime = src.Substring(5, p1 - 5);
        string encoding = src.Substring(p1 + 1, p2 - p1 - 1);
        if (encoding.ToLower() != "base64")
            throw new Exception("Invalid data encoding.");
        byte[> data = Convert.FromBase64String(src.Substring(p2 + 1));
        string cId = System.Guid.NewGuid().ToString();
        _mailer.Message.Attachments.Add(data, string.Empty, cId, mime, null, MailBee.Mime.NewAttachmentOptions.None, MailBee.Mime.MailTransferEncoding.Base64);
        img.SetAttributeValue("src", "cid:" + cId);
    }
}
_mailer.Message.BodyHtmlText = doc.DocumentNode.OuterHtml;
_mailer.Message.MakePlainBodyFromHtmlBody();


I used HtmlAgilityPack to read the HTML.

Would have loved it if MailBee had a function to do this automatically.
You can consider this a feature request I guess? And look I did a chuck of the work for you. ^^
Back to Top View Deantwo's Profile Search for other posts by Deantwo
 
Alex
AfterLogic Support
AfterLogic Support
Avatar

Joined: 19 November 2003
Online Status: Offline
Posts: 2206
Posted: 12 January 2018 at 7:11am | IP Logged Quote Alex

Thank you. Will consider adding this functionality in future versions (although it won't depend on HtmlAgilityPack).

Regards,
Alex
Back to Top View Alex's Profile Search for other posts by Alex
 

If you wish to post a reply to this topic you must first login
If you are not already registered you must first register

  Post ReplyPost New Topic
Printable version Printable version

Forum Jump

Powered by Web Wiz Forums version 7.9
Copyright ©2001-2004 Web Wiz Guide