Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I preview EML file in a web application?

I need to be able to preview EML file in a web application built on Angular/.Net core api. I was able to find a service here by microsoft that previews Word, Excel, or PowerPoint documents only. I can embed this page within the web app and preview these file types. However this service does not support EML files.

There is another service by encryptomatic that previews EML file online. But they do not have something that can be embedded within the application.

Google docs viewer is able to preview images, text and pdf but it also doesn't support previewing EML files.

I need a feature similar to how outlook web app previews EML file.

I have found a few npm packages like eml-format that can parse an EML file. I also know that Aspose provides EML parsers. However I am a little hesitant to go down the route of building my own EML viewer as I need to handle embedded images, multi-part messages, attachments and god knows what!

Any suggestions are welcome.

like image 807
Alvin Saldanha Avatar asked Feb 03 '26 14:02

Alvin Saldanha


1 Answers

Aspose.Email.MailMessage mailMessage = Aspose.Email.MailMessage.Load(templateStream);
            foreach (var linkedResource in mailMessage.LinkedResources)
            {
                using (var memoryStream = new MemoryStream())
                {
                    linkedResource.ContentStream.CopyTo(memoryStream);
                    string base64String = Convert.ToBase64String(memoryStream.ToArray());
                    mailMessage.HtmlBody = mailMessage.HtmlBody.Replace($"cid:{linkedResource.ContentId}", $"data:{linkedResource.ContentType.MediaType};base64," + base64String);

                }
            }
        }
        mailMessage.HtmlBody = Regex.Replace(mailMessage.HtmlBody, @"\<!--(.|\n)*?-->", "");

Here's what I did in case anyone else is looking for the same solution. Used the Aspose Email Library to read the EML/MSG stream and extract HTML out of it. Couple of additional things that were required were to replace the CID by Base64 string for inline images and removing the commented out code using Regex.

like image 91
Alvin Saldanha Avatar answered Feb 06 '26 07:02

Alvin Saldanha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!