Sicos1977 / MsgKit

A .NET library to make MSG files without the need for Outlook
199 stars 55 forks source link

Convert EML file with MSG attachment to MSG file, the attachment becomes EML format #90

Closed sang2357 closed 2 years ago

sang2357 commented 2 years ago

I use EWS to export email from inbox, as EWS can only save in EML format, I use MsgKit to convert it into MSG format. In general it works perfect. But for email with attachment, the attachment in the converted MSG file is in EML format instead of MSG format.

The code is like below.

var email = EmailMessage.Bind(....);

string emlFile = "test.eml";
string msgFile = "test.msg";

FileStream emlStream = new FileStream(emlFile, FileMode.Create);
emlStream.Write(email.MimeContent.Content, 0, email.MimeContent.Content.Length);
emlStream.Close();

MsgKit.Converter.ConvertEmlToMsg(emlStream, msgStream);
Sicos1977 commented 2 years ago

This is works as designed. The ConvertEmlToMsg method does exactly what it is supposed to do. Convert the e-mail to MSG format. It does not touch the attachments it just keeps them the way they are. So if an attachment is in EML format it stays in EML format.

The reason why it does not touch the attachments is because in theory the attached EML can also have other attachments that are in EML format and so on.

If you want to convert all the EML attachments to MSG format you have to write the code for it your self.

It should not be to hard to extend the method to do what you want.

sang2357 commented 2 years ago

@Sicos1977, please find my elaboration as below, the attachment is changed from MSG format into EML format after calling ConvertEmlToMsg

**the email has attachment in MSG format**
var email = EmailMessage.Bind(....);  

string emlFile = "test.eml";
string msgFile = "test.msg";

FileStream emlStream = new FileStream(emlFile, FileMode.Create);
emlStream.Write(email.MimeContent.Content, 0, email.MimeContent.Content.Length);
emlStream.Close();
**the email is exported as EML file and the attachment is in MSG format**

MsgKit.Converter.ConvertEmlToMsg(emlStream, msgStream);
**the EML is converted to MSG file but the attachment now is in EML format**