Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
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))
Directory.CreateDirectory(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());
htmlStream.Close();
//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);
htmlWriter.Write(htmlText);
htmlWriter.Close();
htmlFile.Close();
//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
sender.Send(mess);
mess.Dispose();
//Delete the temporary folder
Directory.Delete(tempDir, true);
}
I hope this could help you.
Best regards.