We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Convert docx to eml

Hello,
we are trying to make email message with body from docx. According to the article that should be easy, but when I implemented the same I discovered that html was added to email message as an attachment. But we need docx content to be as email body. I see that your library do this in a very good way here - https://products.aspose.app/email/conversion and I tried this I received expected results. So, now the question how can we do this from the code?

Here is my code:

Document wordDocument = new Document(“Word.docx”);
MemoryStream mhtmlStream = new MemoryStream();
wordDocument.Save(mhtmlStream, SaveFormat.Mhtml);
mhtmlStream.Position = 0;
MailMessage message = MailMessage.Load(mhtmlStream, new MhtmlLoadOptions());
message.Headers.Add(“X-Unsent”, “1”);
message.Subject = “Sending Invoice in Email”;
message.From = “from@gmail.com”;
message.To = “to@gmail.com”;
message.Save(“Email.eml”, SaveOptions.DefaultEml);

And the EML message I receive in attach.Email.zip (18.4 KB)

@coleso

Please use the latest version of Aspose.Words for .NET 22.6 and Aspose.Email for .NET 22.5 to avoid this issue. We suggest you please get 30 days temporary license and apply it before using your code example. Following articles show how to apply the license.

Aspose.Words : License Applying Options
Aspose.Email : Setting a License in Aspose.Email for .NET

Yes, once I used license file the code above works fine.
But, if I try to add attachment to a message then body goes to attachments again.

Aspose.Words.License license = new Aspose.Words.License();
            license.SetLicense("Aspose.Word.NET.lic");
            Aspose.Email.License emailLicense = new Aspose.Email.License();
            emailLicense.SetLicense("Aspose.Email.NET.lic");
            
            Document wordDocument = new Document("Word.docx");
            MemoryStream mhtmlStream = new MemoryStream();
            wordDocument.Save(mhtmlStream, SaveFormat.Mhtml);
            mhtmlStream.Position = 0;
            MailMessage message = MailMessage.Load(mhtmlStream, new MhtmlLoadOptions());
            message.Headers.Add("X-Unsent", "1");
            message.Subject = "Sending Invoice in Email";
            message.From = "from@gmail.com";
            message.To = "to@gmail.com";

            FileStream attachmentFile = new FileStream("Attach.docx", FileMode.Open, FileAccess.Read);
            message.Attachments.Add(new Attachment(attachmentFile, "Attach.docx"));
            message.Save("Email.eml", SaveOptions.DefaultEml);

Email.zip (13.5 KB)

Please advise.

@coleso

Could you please save the MHTML to disk and share it along with Word.docx? We will investigate this issue further and provide you more information on it.

Hello Tahir,
in order to make your investigation easier - have a look into simple project I prepared for you in attach.DocxToEml.zip (29.1 KB)
It has latest aspose library installed from nuget and temporary license. If you comment out line #29 in Program.cs you will see that .eml file created correctly. If line #29 uncommented - then .eml file does not have body - only 2 attachments.

Thank you.

@coleso

We have managed to reproduce the same issue at our end. However, when we convert Word.docx to MTHML using MS Word and save the MTHML to EML using Aspose.Email, the output is correct.

//Document wordDocument = new Document("Word.docx");
//MemoryStream mhtmlStream = new MemoryStream();
//wordDocument.Save(mhtmlStream, SaveFormat.Mhtml);
//mhtmlStream.Position = 0;
MailMessage message = MailMessage.Load("Word.mht", new MhtmlLoadOptions());
message.Headers.Add("X-Unsent", "1");
message.Subject = "Sending Docx in Email";
message.From = "from@gmail.com";
message.To = "to@gmail.com";

FileStream attachmentFile = new FileStream("Attach.docx", FileMode.Open, FileAccess.Read);
// If you comment out next line of code - the email body will be good.
message.Attachments.Add(new Attachment(attachmentFile, "Attach.docx"));
message.Save("outputMHTEmail.eml", SaveOptions.DefaultEml);

The issue is related to Aspose.Words. So, we are moving this forum thread to Aspose.Words’ forum where you will be guided appropriately.

@tahir.manzoor Aspose.Words generates a valid MHTML document, so it must be properly handled by Aspose.Email. I believe the issue must be handled by Aspose.Email team first, since we cannot guess what in Aspose.Words generated MHML is not properly handled by Aspose.Email.

@coleso

We have logged this problem in our issue tracking system as EMAILNET-40620. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

Hello @tahir.manzoor,
do you have any ETA for the fix?

Thank you.

@coleso

Currently, your issue is pending for analysis and is in the queue. Once we complete the analysis of your issue, we will then be able to provide you an estimate.

Hi @tahir.manzoor,

We would very much like to purchase and use the Aspose.email component but unfortunately this issue is holding us back from making the purchase. Do you perhaps have any timeframe as to when this will be resolved?

Thank you.

@coleso

It is to inform you that your issue has been resolved and its fix will be available in the next version of Aspose.Email for .NET 22.6. We will inform you via this forum thread once new version of Aspose.Email is released.