DEV Community

Atir Tahir
Atir Tahir

Posted on

Convert an email file with attachments to PDF using C#

There are a lot of document conversion APIs and they support Email to PDF conversion. But what about the Email (.eml or .msg) attachments?
Blow is the code to convert an Email file with attachments to PDF:

var source = "sample-with-attachment.eml";
var loadOptions = new EmailLoadOptions {ConvertAttachments = true};
using (var converter = new Converter(source, () => loadOptions))
{
    var index = 1;
    var options = new PdfConvertOptions();
    // Note: index = 1 is the email itself, all following indexes are attachments
    converter.Convert(() => new FileStream($"converted-{index++}.pdf", FileMode.Create) , options);
}
Enter fullscreen mode Exit fullscreen mode

For further details see this post. You can also raise your concerns on forum.

Top comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.