Sicos1977 / MSGReader

C# Outlook MSG file reader without the need for Outlook
http://sicos1977.github.io/MSGReader
MIT License
490 stars 168 forks source link

Images inside .eml files are not getting added in html properly #296

Closed dhrupdubey closed 2 years ago

dhrupdubey commented 2 years ago

Describe the bug Images inside .eml files are not getting added in html properly

To Reproduce Steps to reproduce the behavior:

  1. Create a new mail with image inside it not attached format
  2. Create a .eml file from the above email
  3. Now try to get the html from above file
  4. Now put this html in .html file and check image is loaded properly or not

Expected behavior Image should be shown properly in html

Screenshots Created a email adding following image image

this is the result in html: image

Desktop (please complete the following information):

Smartphone (please complete the following information):

Additional context Add any other context about the problem here.

TriJoe commented 2 years ago

This is expected. You will need to save the attachments first, then modify the html to point to the local file. Just to clarify, the images within the email are still attached, but attached as inline attachments rather than storage attachments.

I'm using this method:

if (message.Attachments != null) //if has attachments
{
    foreach (var attachment in message.Attachments)
    {
        if (attachment.IsInline)
        {
            string attachmentPath = Path.GetTempPath() + parentUniqueId + attachment.FileName;
            Debug.WriteLine("Inline Attachment: " + attachment.FileName + ", " + attachmentPath);

            var attachmentFileInfo = new FileInfo(attachmentPath);
            attachment.Save(attachmentFileInfo);
            inlineAttachments.Add(new InlineAttachments(attachment.FileName, attachmentPath, attachment.ContentId));
        }
    }
}

And then using this to look for the CID string in HTML and replace it with the new local path

foreach (InlineAttachments inline in inlineAttachments)
{
    Debug.WriteLine("Looking for: " + "cid:" + inline.Cid);
    body = body.Replace("cid:" + inline.Cid, inline.Path);
}
dhrupdubey commented 2 years ago

@TriJoe Thanks a lot for the code, it is working now.

Closing this as this is not a bug, @TriJoe has provided solution for it.