Rebex MSG

Outlook MSG file format library for .NET

Download 30-day free trial Buy from $199
More .NET libraries

Back to feature list...

MSG message API

This API is provided by Rebex.Msg.dll assembly.

Contents of Outlook MSG messages are stored in MAPI properties. See MAPI properties for more details about working with them directly.

Loading and saving messages 

To load a message, use MsgMessage.Load() method. To save a message use MsgMessage.Save() method. Both methods accept path or Stream arguments.

// create an instance of MsgMessage
var message = new MsgMessage();

// load a message from file
message.Load(@"C:\MyData\sample.msg");

// .. work with the message

// save the message into a memory stream for later use
var stream = new MemoryStream();
message.Save(stream);
stream.Position = 0;
' create an instance of MsgMessage
Dim message = New MsgMessage()

' load a message from file
message.Load("C:\MyData\sample.msg")

' .. work with the message

' save the message into a memory stream for later use
Dim stream = New MemoryStream()
message.Save(stream)
stream.Position = 0

Message subject 

Message 'Subject' is accessible via MsgMessage.Subject property.

// create an instance of MsgMessage
var message = new MsgMessage();

// set the 'Subject'
message.Subject = "Rebex MSG sample";
' create an instance of MsgMessage
Dim message = New MsgMessage()

' set the 'Subject'
message.Subject = "Rebex MSG sample"

When reading the value, content of the MsgPropertyTag.Subject MAPI property is returned. If it is not present, the subject is constructed from the MsgPropertyTag.SubjectPrefix, MsgPropertyTag.NormalizedSubject and MsgPropertyTag.ConversationTopic MAPI properties.

When setting the value, the following MAPI properties are updated:

  • MsgPropertyTag.Subject
  • MsgPropertyTag.SubjectPrefix
  • MsgPropertyTag.NormalizedSubject
  • MsgPropertyTag.ConversationTopic

Plain, HTML and RTF bodies 

An Outlook MSG message might contain up to three representations of the message body: RTF, HTML, and plain text.

To get a value of the desired body representation, use MsgMessage.BodyRtf, MsgMessage.BodyHtml, or MsgMessage.BodyText properties.

To set the bodies, use MsgMessage.SetBody() method.

// create an instance of MsgMessage
var message = new MsgMessage();

// set plain, HTML and RTF message bodies
string plainTextBody = "This is sample body.";
string htmlBody = "This is sample <b>HTML</b> body.";
string rtfBody = @"{\rtf1\ansi\ This is sample \b RTF\b0  body.}";
message.SetBody(plainTextBody, htmlBody, rtfBody);

// display the message bodies
Console.WriteLine("Plain text body: {0}", message.BodyText);
Console.WriteLine("HTML body: {0}", message.BodyHtml);
Console.WriteLine("RTF body: {0}", message.BodyRtf);
' create an instance of MsgMessage
Dim message = New MsgMessage()

' set plain, HTML and RTF message bodies
Dim plainTextBody As String = "This is sample body."
Dim htmlBody As String = "This is sample <b>HTML</b> body."
Dim rtfBody As String = "{\rtf1\ansi\ This is sample \b RTF\b0  body.}"
message.SetBody(plainTextBody, htmlBody, rtfBody)

' display the message bodies
Console.WriteLine("Plain text body: {0}", message.BodyText)
Console.WriteLine("HTML body: {0}", message.BodyHtml)
Console.WriteLine("RTF body: {0}", message.BodyRtf)

The following MAPI properties are set based on the specified arguments:

  • MsgPropertyTag.Body
  • MsgPropertyTag.BodyHtml
  • MsgPropertyTag.RtfCompressed
  • MsgPropertyTag.RtfInSync

From and Sender fields 

The 'From' and 'Sender' addresses are accessible using the MsgMessage.From and MsgMessage.Sender properties.

Note: Microsoft Outlook does not display the 'From' address unless the 'Sender' is specified.
// create an instance of MsgMessage
var message = new MsgMessage();

// specify 'From' and 'Sender' addresses:

// address with a display name specified explicitly
message.From = new MsgAddress("bob@example.com", "Bob");

// addresses in MIME address format are accepted as well
message.Sender = "Joe <joe@example.com>";
' create an instance of MsgMessage
Dim message = New MsgMessage()

' specify 'From' and 'Sender' addresses:

' address with a display name specified explicitly
message.From = New MsgAddress("bob@example.com", "Bob")

' addresses in MIME address format are accepted as well
message.Sender = "Joe <joe@example.com>"

The following MAPI properties are used for 'From' address:

  • MsgPropertyTag.SentRepresentingAddressType
  • MsgPropertyTag.SentRepresentingEmailAddress
  • MsgPropertyTag.SentRepresentingName
  • MsgPropertyTag.SentRepresentingEntryId
The following MAPI properties are used for 'Sender' address:
  • MsgPropertyTag.SenderAddressType
  • MsgPropertyTag.SenderEmailAddress
  • MsgPropertyTag.SenderSmtpAddress
  • MsgPropertyTag.SenderName
  • MsgPropertyTag.SenderEntryId

Recipients collection 

MSG message recipients are accessible using MsgMessage.To, MsgMessage.CC and MsgMessage.Bcc collections.

// create an instance of MsgMessage
var message = new MsgMessage();
message.Sender = "me@example.com";

// specify some recipients:

// display name as argument
message.To.Add("bob@example.com", "Bob");

// display name in MIME address format
message.To.Add("Joe <joe@example.com>");

// no display name (address used as a display name)
message.To.Add("tom@example.com");

// add 'Sender' to 'CC'
message.CC.Add(message.Sender);
' create an instance of MsgMessage
Dim message = New MsgMessage()
message.Sender = "me@example.com"

' specify some recipients:

' display name as argument
message.To.Add("bob@example.com", "Bob")

' display name in MIME address format
message.To.Add("Joe <joe@example.com>")

' no display name (address used as a display name)
message.To.Add("tom@example.com")

' add 'Sender' to 'CC'
message.CC.Add(message.Sender)

Working with attachments 

MSG message attachments are accessible using MsgMessage.Attachments collection.

// create an instance of MsgMessage
var message = new MsgMessage();

// add an attachment
message.Attachments.Add(@"c:\MyData\image.jpg");

// display the list of attachments
foreach (MsgAttachment attachment in message.Attachments)
{
    Console.WriteLine("Attachment: {0} ({1} bytes)",
        attachment.FileName,
        attachment.Data.Length);
}
' create an instance of MsgMessage
Dim message = New MsgMessage()

' add an attachment
message.Attachments.Add("c:\MyData\image.jpg")

' display the list of attachments
For Each attachment As MsgAttachment In message.Attachments
    Console.WriteLine("Attachment: {0} ({1} bytes)",
                      attachment.FileName,
                      attachment.Data.Length)
Next

Back to feature list...