Home > Code > C# > How to Import & Export MSG, EML, MHT Email Messages inside .NET Apps

How to Import & Export MSG, EML, MHT Email Messages inside .NET Apps

by sher azam   on Oct 14, 2015   Category: C#   |  Views: 1062    |  Points: 25   |  Gold 


This technical tip explains how to .NET developers can import and export emails inside thir .NET Applications. There are many components in the market that allow parsing emails in different formats like RFC 822 Compliant Message Format (EML) and Microsoft HTML Format (MHT) but Aspose.Email is the first .NET component in the market that also allows developers to parse Microsoft Outlook Email Message Format (MSG). This gives a competitive advantage to Aspose.Email over its competitors. Aspose.Email's MailMessage class can import email in different formats:

• Microsoft Outlook Email Message Format (MSG)
• Microsoft HTML Format (MHT)
• RFC 822 Compliant Message Format (EML)

Messages can also be loaded specifying Load Options for preferred options. It can export email in different formats too:

• RFC 822 Compliant Message Format (EML)
• Microsoft Html Format (MHT) //Office Automation

To import email in different formats, follow these steps:* Create an instance of the MailMessage class.* Load an email in the instance of the MailMessage class by specifying the file name and message format in the MessageFormat class.

Importing Emails


//Code Samples: Importing Microsoft Outlook Message Format Emails

//C# Code Samples

//Create an instance of the MailMessage class
MailMessage msg = new MailMessage();

//Import an Outlook file
msg = MailMessage.Load(@"e:\test.msg", MailMessageLoadOptions.DefaultMsg);

//VB.NET Code Samples

'Create an instance of the MailMessage class
Dim msg As MailMessage = New MailMessage()

'Import an Outlook file
msg = MailMessage.Load("e:\test.msg", MailMessageLoadOptions.DefaultMsg)

// Importing EML Emails

//In the code given below, we have imported an email message in RFC 822 Compliant Message Format.

//C# Code Samples

//Import from EML format
msg = MailMessage.Load(@"e:\test.eml", MailMessageLoadOptions.DefaultEml);

//VB.NET Code Samples

'Import an EML file
msg = MailMessage.Load("e:\test.eml", MailMessageLoadOptions.DefaultEml)

// Importing MHT Emails

//In the code given below, we have imported an email message in Microsoft HTML format.

//C# Code Samples

MailMessageLoadOptions options = new MailMessageLoadOptions();
options.MessageFormat = MessageFormat.Mht;
// Load MHT file
msg = MailMessage.Load(@"e:\test.mht", options);

//VB.NET Code Samples

Dim options As New MailMessageLoadOptions()
options.MessageFormat = MessageFormat.Mht
'Load MHT file
msg = MailMessage.Load("e:\test.Mht", options)

//Code Sample: Loading a Message with Load Options

//C# Code Samples

// loading with default options

// Load from eml
MailMessage eml = MailMessage.Load("test.eml", new EmlLoadOptions());

// Load from html
MailMessage eml = MailMessage.Load("test.html", new HtmlLoadOptions());

// load from mhtml
MailMessage eml = MailMessage.Load("test.mhtml", new MhtmlLoadOptions());

// load from msg
MailMessage eml = MailMessage.Load("test.msg", new MsgLoadOptions());

// load from thef fomat
MailMessage eml = MailMessage.Load("winmail.dat", new TnefLoadOptions());

// loading with custom options
EmlLoadOptions opt = new EmlLoadOptions
{
PrefferedTextEncoding = Encoding.UTF8,
PreserveTnefAttachments = true
};

MailMessage eml = MailMessage.Load("test.html", opt);

HtmlLoadOptions opt = new HtmlLoadOptions
{
PrefferedTextEncoding = Encoding.UTF8,
ShouldAddPlainTextView = true,
PathToResources = @"D:\images"
};

MailMessage eml = MailMessage.Load("test.html", opt);

//VB.NET Code Samples

' loading with default options

' Load from eml
Dim eml As MailMessage = MailMessage.Load("test.eml", New EmlLoadOptions())

' Load from html
Dim eml As MailMessage = MailMessage.Load("test.html", New HtmlLoadOptions())

' load from mhtml
Dim eml As MailMessage = MailMessage.Load("test.mhtml", New MhtmlLoadOptions())

' load from msg
Dim eml As MailMessage = MailMessage.Load("test.msg", New MsgLoadOptions())

' load from thef fomat
Dim eml As MailMessage = MailMessage.Load("winmail.dat", New TnefLoadOptions())

' loading with custom options
Dim opt As New EmlLoadOptions() With { _
Key .PrefferedTextEncoding = Encoding.UTF8, _
Key .PreserveTnefAttachments = True _
}

Dim eml As MailMessage = MailMessage.Load("test.html", opt)

Dim opt As New HtmlLoadOptions() With { _
Key .PrefferedTextEncoding = Encoding.UTF8, _
Key .ShouldAddPlainTextView = True, _
Key .PathToResources = "D:\images" _
}

Dim eml As MailMessage = MailMessage.Load("test.html", opt)



Exporting Emails

//To export an email in different formats, follow these steps:

// C# Code Samples

//Create an instance of the MailMessage class
MailMessage msg = new MailMessage();

//Export to MHT format
msg.Save(@"e:\test.mht", SaveOptions.DefaultMhtml);

// VB.NET Code Samples

'Create an instance of the MailMessage class
Dim msg As MailMessage = New MailMessage()

'Export to MHT format
msg.Save("e:\test.mht", SaveOptions.DefaultMhtml)

//Code Samples: Exporting Email to MHT with customized TimeZone

MailMessage class provides the TimeZoneOffset property to set customized Timezone while exporting to MHT. Following is an example in this regard.

// C# Code Samples

MailMessage eml = MailMessage.Load(fileName);

// Set the local time for message date.
eml.TimeZoneOffset = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now);

// or set custom time zone offset for message date (-0800)
// eml.TimeZoneOffset = new TimeSpan(-8,0,0);

// The dates will be rendered by local system time zone.
eml.Save(outFileName, MessageFormat.Mht, MailMessageSaveOptions.WriteHeaderToMht);

// VB.NET Code Samples

MailMessage eml = MailMessage.Load(fileName)

'Set the local time for message date.
eml.TimeZoneOffset = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now)

'or set custom time zone offset for message date (-0800)
'eml.TimeZoneOffset = new TimeSpan(-8,0,0)

'The dates will be rendered by local system time zone.
eml.Save(outFileName, MessageFormat.Mht, MailMessageSaveOptions.WriteHeaderToMht)

// Exporting Email to EML

// C# Code Samples

//Export an EML file
msg.Save(@"e:\test.eml", SaveOptions.DefaultEml);

// VB.NET Code Samples

'Export an EML file
msg.Save("e:\test.eml", SaveOptions.DefaultEml)



Overview: Aspose.Email for .NET

Aspose.Email for .NET is a set of components allowing developers to easily implement email functionality within their ASP.NET web applications, web services & Windows applications. It Supports Outlook PST, EML, MSG & MHT formats. It allows developers to work with SMTP, POP3, FTP & MS Exchange servers. It supports mail merge, iCalendar, customized header & body, header information, embedded files, Twitter & many more. It makes it easy to work with HTML or plain text emails & their attachments.

- Homepage of Aspose.Email for .NET: http://www.aspose.com/.net/email-component.aspx

- Download Aspose.Email for .NET: http://www.aspose.com/community/files/51/.net-components/aspose.email-for-.net/default.aspx



Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts