Read Outlook *.eml file without Outlook API in C#

This post addresses how to  extract From,To,Subject and message body content from  *.eml file. Initialy i have tried my hand in using Outlook API but results intermittent security exception while instatntiating the object. After much exploration in the web came to know about the COM API called Collaborative Data Object model it can be located under Add Reference in VisualStudio under COM section namely Microsoft CDO For Windows 2000 Library.

Below the code snippet where *.eml file name will be passed in turn returns object of type CDO.Message.

protected CDO.Message ReadMessage(String emlFileName)
CDO.Message msg = null;
msg = new CDO.MessageClass();
ADODB.Stream stream = new ADODB.StreamClass();
stream.Open(Type.Missing, ADODB.ConnectModeEnum.adModeUnknown, ADODB.StreamOpenOptionsEnum.adOpenStreamUnspecified, String.Empty, String.Empty);
msg.DataSource.OpenObject(stream, “_Stream”);
catch (Exception ex)
{ }
return msg;
Note: using Outlook Express you can save the message as *.eml file.

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now