I'm trying to strip gif images from emails in order to save storage space in Outlook and our document management system.
Say for example you've got an email approx 2MB's in size and the gif is 1MB. I'm expecting the result of the file size of the email to be 1MB.
The first part uses MimeKit to remove the gif. The problem I find with this code is that if you are not debugging it doesn't reduce the file size by what I'd expect. I've found this is because the image is still in the html properties of the MimeMessage.
MimeMessage mimeMessage = MimeMessage.Load(testFile);
var bodyParts = mimeMessage.BodyParts.ToList();
if (bodyParts.Any())
{
var multipart = mimeMessage.Body as Multipart;
if (multipart != null)
{
MimeEntity bodyPartToRemove = null;
foreach (var bodyPart in bodyParts)
{
var mimeBodyPart = bodyPart as MimePart;
if (mimeBodyPart == null)
{
continue;
}
if (mimeBodyPart.ContentType.MimeType == "image/gif")
{
bodyPartToRemove = mimeBodyPart;
}
}
if (bodyPartToRemove != null)
{
multipart.Remove(bodyPartToRemove);
}
}
mimeMessage.Body = multipart;
}
So after this I thought I'd use HtmlAgilityPack to remove the img tags from the html and then use the MimeKit.BodyBuilder to set the MimeMessage correctly.
var builder = new BodyBuilder();
// Set the plain-text version of the message text
builder.TextBody = mimeMessage.TextBody;
// Set the html version of the message text
builder.HtmlBody = StripHtml(mimeMessage.HtmlBody);
// Attachments
foreach (var blah in mimeMessage.Attachments)
builder.Attachments.Add(blah);
mimeMessage.Body = builder.ToMessageBody();
private string StripHtml(string html)
{
HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
htmlDoc.LoadHtml(html);
var nodes = htmlDoc.DocumentNode.SelectNodes("//img");
foreach (var node in nodes)
{
if (node.OuterHtml.Contains(".gif"))
node.Remove();
}
return htmlDoc.DocumentNode.InnerHtml;
}
The problem with this solution is that by using the builder.ToMessageBody() it is not displaying the other non gif images that could be contained in the email as well as rendering other parts of the email correctly like emoji's.
Has anyone come across this before?