Eml to Msg conversion was never easy before. You just need 3 lines of code for the conversion. We can load the existing eml file in MailMessage class instance. Then we convert it into MapiMessage instance using FromMailMessage() method. Once, we get the MapiMessage instance, we can call the Save() method to save it as msg file.
Eml to Msg Conversion From an existing Eml File
The following code snippet converts eml file to msg. The source eml file is shown in the figure below. As you can see that the eml file also contains an image attachment. This attachment will also be imported to the msg file during the conversion.
[C#]
//load mail message
MailMessage eml = MailMessage.Load("test.eml");
//convert to MapiMessage
MapiMessage msg = MapiMessage.FromMailMessage(eml);
//save to msg file
msg.Save("test.msg");
[VB.NET]
'load mail message
Dim eml As MailMessage = MailMessage.Load("test.eml")
'convert to MapiMessage
Dim msg As MapiMessage = MapiMessage.FromMailMessage(eml)
'save to msg file
msg.Save("test.msg")
After converting, the msg file is shown below in the screenshot.
Creating A New Eml File and Then Converting It To Msg File
Instead of loading an existing eml file, you can create a new eml in the runtime using the MailMessage class and then later on convert this eml file into msg file. In the example below, we will create an instance of MailMessage class to create an eml file. We will set its properties like Subject, From, To, CC, Body, add an attachment and save it as an eml file. Then we will convert the MailMessage instance into MapiMessage instance and save it as an msg file.
[C#]
// Create an instance of MailMessage class
MailMessage emlMsg = new MailMessage();
// Set from, to, subject and body properties
emlMsg.From = "from@domain.com";
emlMsg.To = "to@domain.com";
emlMsg.Subject = "converting from eml to msg file";
emlMsg.TextBody = "This eml file will be converted to msg file using Aspose.Network.";
// add an attachment
emlMsg.Attachments.Add(new Aspose.Network.Mail.Attachment(@"E:\Data\Aspose\temp\1.bmp"));
// save the eml file
string strEmlFile = "E:\\Data\\Aspose\\temp\\test.eml";
emlMsg.Save(strEmlFile, MessageFormat.Eml);
// Create an instance of MapiMessage class and pass MailMessage as argument
string strMsgFile = "E:\\Data\\Aspose\\temp\\test.msg";
MapiMessage outlookMsg = MapiMessage.FromMailMessage(emlMsg);
// save as msg file
outlookMsg.Save(strMsgFile);
[VB.NET]
' Create an instance of MailMessage class
Dim emlMsg As MailMessage = New MailMessage()
' Set from, to, subject and body properties
emlMsg.From = "from@domain.com"
emlMsg.To = "to@domain.com"
emlMsg.Subject = "converting from eml to msg file"
emlMsg.TextBody = "This eml file will be converted to msg file using Aspose.Network."
' add an attachment
emlMsg.Attachments.Add(New Aspose.Network.Mail.Attachment("E:\Data\Aspose\temp\1.bmp"))
' save the eml file
Dim strEmlFile As String = "E:\Data\Aspose\temp\test.eml"
emlMsg.Save(strEmlFile, MessageFormat.Eml)
' Create an instance of MapiMessage class and pass MailMessage as argument
Dim strMsgFile As String = "E:\Data\Aspose\temp\test.msg"
Dim outlookMsg As MapiMessage = MapiMessage.FromMailMessage(emlMsg)
' save as msg file
outlookMsg.Save(strMsgFile)