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

Free Support Forum - aspose.com

Convert to MHTML and Send Email

I am using ASPOSE.Words to convert word document to mhtml stream as in your given example
‘Convert a Word Document to MHTML and Email’
in Documentation.
Now there I want to use MailMessage object of System.Net to send mail. So for that I have done following coding

Aspose.Words.Document doc = new Aspose.Words.Document(Server.MapPath("~/Sample.doc"));
doc.MailMerge.Execute(new string[] { "Name" }, new object[] { "Hardik" }); 
System.IO.Stream stream = new System.IO.MemoryStream();
doc.Save(stream, Aspose.Words.SaveFormat.Mhtml);

Now for sending mail using System.Net.MailMessage Object done following coding

stream.Position = 0;
System.IO.StreamReader readStream = new System.IO.StreamReader(stream);
readStream.BaseStream.Position = 0;
string strMailContent = readStream.ReadToEnd();
MailMessage message = new MailMessage();
message.To.Add(new MailAddress(strToEmailId, strToEmailName));
message.Subject = strSubject;
message.IsBodyHtml = true;
message.Body = strMailContent;

Now the mail which I got doesn’t contain Html Format instead its some content as below
MIME-Version: 1.0 Content-Type: text/html; charset=“utf-8” Content-Transfer-Encoding: quoted-printable Content-Location: document.html =EF=BB=BF
Dear Hardik,
instead of
Dear Hardik,
Pleae help me What should I do to get correct mhtml format content

Thank you for your interest in Aspose.Words.
What you are getting is MHTML format. You can compare it with output from web browser when you save any page in MHTML format.
Would you like to send plain HTML? Okay, you should specify SaveFormat.Html. Let me know if your message should also contain images or other supplementary data and you are experiencing difficulties with them. There are some tricks.

Thank you for quick reply,
Yes my message also contain images and thats why I was converting it into mhtml
Please help me if you have any solution

Thanks for your inquiry. Using standard MailMessage you can’t specify MHTML content of message. That’s why in the sample code we use Aspose.Network. This tool allow you to use MHTML content.
As a workaround you can use HTML content, but in this case you also need to embed images into the mail message. You can achieve this using “cid”. Please see the following code example.

public void Example001()
    // Create temporary folder.
    string tempDir = Path.Combine(Path.GetTempPath(), "AsposeMail");
    if (!Directory.Exists(tempDir))
    // Open word document.
    Document doc = new Document(@"C:\Temp\in.doc");
    // Specify folder where images will be saved.
    doc.SaveOptions.ExportImagesFolder = tempDir;
    // Save html to stream.
    MemoryStream htmlStream = new MemoryStream();
    doc.Save(htmlStream, SaveFormat.Html);
    // Read all text from stream.
    string htmlText = Encoding.UTF8.GetString(htmlStream.GetBuffer());
    // Get array of image names
    string[] fileNames = Directory.GetFiles(tempDir);
    // Rempace all image names like "myImage.jpg" with "cid:myImage".
    // This is needed because images will be storead in the mail message as embedded objects.
    for (int imgIndex = 0; imgIndex < fileNames.Length; imgIndex++)
        string imgName = fileNames[imgIndex];
        string replacement = string.Format("cid:{0}", Path.GetFileNameWithoutExtension(fileNames[imgIndex]));
        htmlText = htmlText.Replace(imgName, replacement);
    // Save proccessed html into the temporary folder.
    Stream htmlFile = new FileStream(Path.Combine(tempDir, "Message.html"), FileMode.Create);
    StreamWriter htmlWriter = new StreamWriter(htmlFile);
    // Information that is needed to send email message
    string smtp = "smtp.mail.ru"; //Your smtp server
    string emailFrom = "youremail@mail.ru"; //Your email 
    string password = "yourpassword"; //Your password
    string emailTo = "recipientemail@gmail.com"; //Recipient email
    string subject = "Aspose.Words email message test."; //Subject
                                                            // Create maildefination
    System.Web.UI.WebControls.MailDefinition mail = new System.Web.UI.WebControls.MailDefinition();
    mail.IsBodyHtml = true;
    mail.BodyFileName = Path.Combine(tempDir, "Message.html");
    mail.From = emailFrom;
    mail.Subject = subject;
    // Add images as embedded objects to email message
    for (int imgIndex = 0; imgIndex < fileNames.Length; imgIndex++)
        string imgFullName = fileNames[imgIndex];
        string imgName = Path.GetFileNameWithoutExtension(fileNames[imgIndex]);
        mail.EmbeddedObjects.Add(new System.Web.UI.WebControls.EmbeddedMailObject(imgName, imgFullName));
    // Create email message
    MailMessage mess = mail.CreateMailMessage(emailTo, new ListDictionary(), new System.Web.UI.Control());
    // Create smtp client
    SmtpClient sender = new SmtpClient(smtp);
    // Set credentials
    sender.Credentials = new NetworkCredential(emailFrom, password);
    // Send email message
    // Delete the temporary folder
    Directory.Delete(tempDir, true);

I hope this could help you.
Best regards.

Thanks for reply and this code.
This would help me a lot.
Now I have one more query.
Take case in which I want to take Html Document as input template for mail merge instead of word document.
Now for word document fields we are using '<> ’
what should I use for Html Docuement?
Could you please help me out?

Thanks for your inquiry. This is impossible to use HTML document as input template for mail merge. So you should use Word document as a template and convert output document to HTML.
Best regards,