What is MSGReader
MSGReader is a C# .NET 4.5 and Standard 2.0 (supports C# .NET 4.6.1 and up) library to read Outlook MSG and EML (Mime 1.0) files. Almost all common object in Outlook are supported:- Appointment
- Task
- Contact card
- Sticky note
- Text
- HTML
- HTML embedded into RTF
- RTF
If you realy want to write MSG files then see my MsgKit project on GitHub (https://github.com/Sicos1977/MsgKit)
Read properties from an Outlook (msg) message
using (var msg = new MsgReader.Outlook.Storage.Message("d:\\testfile.msg"))
{
var from = msg.Sender;
var sentOn = msg.SentOn;
var recipientsTo = msg.GetEmailRecipients(MsgReader.Outlook.RecipientType.To, false, false);
var recipientsCc = msg.GetEmailRecipients(MsgReader.Outlook.RecipientType.Cc, false, false);
var subject = msg.Subject;
var htmlBody = msg.BodyHtml;
// etc...
}
Read properties from an Outlook (eml) message
var fileInfo = new FileInfo("d:\\testfile.eml");
var eml = MsgReader.Mime.Message.Load(fileInfo);
if (eml.Headers != null)
{
if (eml.Headers.To != null)
{
foreach (var recipient in eml.Headers.To)
{
var to = recipient.Address;
}
}
}
var subject = eml.Headers.Subject;
if (eml.TextBody != null)
{
var textBody = System.Text.Encoding.UTF8.GetString(eml.TextBody.Body);
}
if (eml.HtmlBody != null)
{
var htmlBody = System.Text.Encoding.UTF8.GetString(eml.HtmlBody.Body);
}
// etc...
Delete attachment from an Outlook message
This example deletes the first attachmentvar outlook = new Storage.Message(fileName, FileAccess.ReadWrite);
outlook.DeleteAttachment(outlook.Attachments[0]);
outlook.Save("d:\\deleted.msg");
Translations
-
Kees van Spelde
- English (US)
- Dutch
-
Ronald Kohl
- German
-
Yan Grenier (@ygrenier on GitHub)
- French
-
xupefei
- Simpl Chinese
Installing via NuGet
The easiest way to install MSGReader is via NuGet.In Visual Studio's Package Manager Console, simply enter the following command:
Install-Package MSGReader
Side note
This project can also be used from a COM based language like VB script or VB6. To use it first compile the code and register the com visible assembly with the command:Regasm.exe /codebase MsgReader.dll
After that you can call it like this:
dim msgreader
set msgreader = createobject("MsgReader.Reader")
msgreader.ExtractToFolderFromCom "the msg file to read", "the folder where to place the extracted files"
No comments:
Post a Comment
Note: only a member of this blog may post a comment.