]> Converting .eml to .msg in C# .net 🌐:aligrant.com

Converting .eml to .msg in C# .net

Alastair Grant | Fri 18 Oct 2019

I've had the odd requirement recently to convert a standard SMTP email message (RFC-822), commonly seen with a .eml extension, into the propriety Microsoft .msg Outlook Item format. The more common requirement is the other way round, taking an e-mail that a user has saved from Outlook, and revert it back to the standard format.  I though am dealing with a strange piece of software that needs to import .msg files and won't handle .eml files.

The common approach to working with any MS Office formats is to use the OLE Automation libraries that come with Office, which allow you to automate any office application in the background.  This though is notoriously unstable, requires an active desktop, and an installation of Office (which limits your OS choice).  Microsoft are much better these days at publishing their file formats, so there is no reason why you can't work with these files directly.

And banking on the fact that I'm not the first person to have this requirement I had a nose around GitHub and found MsgKit, a small library that handles the generation of .msg files, including a helpful converter that will take a .eml stream and produce a .msg stream.  And usage couldn't be simpler, as it's published to NuGet.

Add the NuGet package and then run the converter:

MsgKit.Converter.ConvertEmlToMsg(emlStream, msgStream);

Where msgStream where you want the output to go to.  It also has an overload to deal directly with file system files.

Breaking from the voyeuristic norms of the Internet, any comments can be made in private by contacting me.

Related