I wrote a method that saves all the images from the email. The problem is that it doesn't save images that are contained in div containers. How can this be fixed using MailKit (without installing additional nuget packages)?
This method adds the images from the message to the attachments list. But it ignores the images in the div. I would be very grateful for help. Thank you very much in advance!
private void AttachPicturesFromMessage(MimeMessage message, List<EMailMessageAttachment> attachments)
{
var counter = 1;
foreach (MimePart att in message.BodyParts)
{
if (att.ContentId != null && att.ContentObject != null
&& att.ContentType.MediaType == "image")
{
byte[] byteCode;
using (var mem = new MemoryStream())
{
att.ContentObject.DecodeTo(mem);
byteCode = mem.ToArray();
}
attachments.Add(new EMailMessageAttachment {
FileName = $"{message.Subject}_{counter}.jpg",
Content = byteCode });
counter++;
}
}
}
Thank you very much for the suggested way, jstedfast! Before your answer, I solved the problem this way:
private void AttachImagesFromMessage(MimeMessage message, List<EMailMessageAttachment> attachments)
{
var imageCounter = 1;
AttachImagesFromBody(message, attachments, ref imageCounter);
AttachImagesFromDiv(message, attachments, ref imageCounter);
}
private void AttachImagesFromBody(MimeMessage message,
List<EMailMessageAttachment> attachments, ref int imageCounter)
{
// Processing embedded images in the message body
foreach (MimePart image in message.BodyParts)
{
if (image.ContentId != null && image.ContentObject != null && image.ContentType.MediaType == "image")
{
byte[] byteCode;
using (var mem = new MemoryStream())
{
image.ContentObject.DecodeTo(mem);
byteCode = mem.ToArray();
}
attachments.Add(new EMailMessageAttachment
{
FileName = $"Image_{imageCounter}.jpg",
Content = byteCode
});
imageCounter++;
}
}
}
private void AttachImagesFromDiv(MimeMessage message,
List<EMailMessageAttachment> attachments, ref int imageCounter)
{
var htmlBody = message.HtmlBody;
if (htmlBody != null)
{
// Search for div tags containing images
var regex = new Regex("<div[^>]*>.*?<img[^>]*src=\"(.*?)\".*?>.*?</div>",
RegexOptions.IgnoreCase | RegexOptions.Singleline);
var matchCollection = regex.Matches(htmlBody);
// Extracting images and adding them to the attachments collection
foreach (Match match in matchCollection)
{
var imageUrl = match.Groups[1].Value;
if (!string.IsNullOrEmpty(imageUrl) && imageUrl.StartsWith("http"))
{
var imageBytes = DownloadImage(imageUrl);
if (imageBytes != null)
{
attachments.Add(new EMailMessageAttachment
{
FileName = $"Image_{imageCounter}.jpg",
Content = imageBytes
});
imageCounter++;
}
}
}
}
}
private byte[] DownloadImage(string imageUrl)
{
using (var webClient = new WebClient())
{
try
{
return webClient.DownloadData(imageUrl);
}
catch (WebException ex)
{
throw new Exception($"Failed to load the image from the link {imageUrl}. {ex.Message}");
}
}
}
It looks bulky and ugly, but it works.