Get HTML string with embeded images

Hi Team,
We have created extension to get HTML string of outlook email(.msg).
We are rendering this HTML string in web application.
Our email is rendered as HTML correctly except inline images.
Below is our code.

        var fileBytes = File.ReadAllBytes("Sample.msg");
        Stream stream = new MemoryStream(fileBytes);         
        MailMessage message = MailMessage.Load(stream);
        var htmlString = message.HtmlBody;

Above code is giving HTML string as

When we save it as HTML then images are embedded as base 64-
message.Save(“output.html”, SaveOptions.DefaultHtml);
This is saving HTML as -
<img src="data:image/png;base64, …>

But we don’t want to save as HTML file we need HTML string with embedded images as base 64.

Best Regards,
Nitin

Hello @nitinnchavan99,

Thank you for reporting your case.

Aspose.Email has an option to save messages in HTML format. In this case, you can save images either embedded in HTML or in a separate folder.

Use the following code example to convert an email to an HTML file with images, which are saved to a target directory:

// Load MSG or EML
var msg = MailMessage.Load(msgFileName);

// Create HTML save options
var htmlSaveOptions = new HtmlSaveOptions
{
    ResourceRenderingMode = ResourceRenderingMode.SaveToFile
};

htmlSaveOptions.SaveResourceHandler +=
    (AttachmentBase attachment, out string resourceFileName) =>
    {
        if (!Directory.Exists(resourceDir))
        {
            Directory.CreateDirectory(resourceDir);
        }
        resourceFileName = Path.Combine(resourceDir, attachment.ContentType.Name);
        attachment.Save(resourceFileName);
    };

msg.Save(htmlFileName, htmlSaveOptions);

Use the following code example to convert EML to HTML format and save the images inside HTML:

// MSG or EML filename
var msgFileName = Path.Combine(sourcePath, "A Day in the Park.msg");

// target HTML
var htmlFileName = Path.Combine(targetPath, "A Day in the Park.html");

// Load MSG or EML
var msg = MailMessage.Load(msgFileName);

// Create HTML save options
var htmlSaveOptions = new HtmlSaveOptions
{
    ResourceRenderingMode = ResourceRenderingMode.EmbedIntoHtml
};

msg.Save(htmlFileName, htmlSaveOptions);

Also, you can explore a related article on our blog:

Please, feel free to contact us if you still have any questions.