Author |
|
Deantwo Newbie
Joined: 10 January 2018 Location: Denmark
Online Status: Offline Posts: 5
|
Posted: 12 January 2018 at 7:06am | IP Logged
|
|
|
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 |
|
|
Alex AfterLogic Support
Joined: 19 November 2003
Online Status: Offline Posts: 2206
|
Posted: 12 January 2018 at 7:11am | IP Logged
|
|
|
Thank you. Will consider adding this functionality in future versions (although it won't depend on HtmlAgilityPack).
Regards,
Alex
|
Back to Top |
|
|