How to Import & Export MSG, EML, MHT Email Messages inside .NET Apps
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, such as Microsoft Outlook Email Message Format (MSG), Microsoft HTML Format (MHT) and RFC 822 Compliant Message Format (EML). It can export email in different formats too such as RFC 822 Compliant Message Format (EML) and Microsoft Html Format (MHT).
//your code here...//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)
Url: http://www.aspose.com/.net/email-component.aspx
Language: C# | User: Sheraz Khan | Created: Oct 14, 2015 | Tags: Import EML Emails import Outlook Email Message Format Export MSG format messages Import MHT Emails Loading Message with Load Options Export to MHT format .NET Email API