Using a Microsoft Word Document as the Message Body and Sending Email

This article shows you how to use a Microsoft Word document as the email body and send it to recipients. The sample document is a sales invoice from the Northwind database sample, exported to Microsoft Word format. Aspose.Email for Java deals with network protocols and Microsoft Outlook features and cannot handle Microsoft Word documents. To overcome this, the samples in this article use Aspose.Words for Java to load the Word document and convert it to MHTML format. Aspose.Email for Java uses the MHTML document in the email body.

Using Microsoft Word Documents as Email Body

The programming samples below illustrate how to send a Word document as an email body using Aspose.Words for Java and Aspose.Email for Java:

  1. Load a Microsoft Word document using Aspose.Word for Java’s Document class.
  2. Save it in MHTML format.
  3. Load the MHTML document using Aspose.Email for Java’s MailMessage class to set the email body.
  4. Set other message properties using different MailMessage class properties and methods.
  5. Send the email using Aspose.Email for Java’s SMTPClient class.

The source document, a sales invoice exported to Microsoft Word from the Microsoft Northwind sample can be seen below.


When the message has been sent and received in Microsoft Outlook, it looks like the message below.


The HTML formatting and images are preserved as in the original source document when viewed in either Outlook or a web email client like Gmail or Hotmail. Below is a screenshot of the message when opened with Gmail in a Chrome browser.


The following code snippet shows you how to use a Microsoft Word document as the message body and send an email by using SmtpClient class instance.

// The path to the File directory
String dataDir = "data/";

// Load a Word document from disk and save it to stream as MHTML
Document wordDocument = new Document(dataDir + "Test.doc");
ByteArrayOutputStream mhtmlStream = new ByteArrayOutputStream();, SaveFormat.MHTML);

// Load the MHTML in a MailMessage object
MailMessage message = MailMessage.load(new ByteArrayInputStream(mhtmlStream.toByteArray()), new MhtmlLoadOptions());
message.setSubject("Sending Invoice in Email");
message.setFrom(new MailAddress(""));

// Save the message in MSG format to disk + "WordDocAsEmailBody_out.msg", SaveOptions.getDefaultMsgUnicode());

    // Send the email message
try (SmtpClient client = new SmtpClient("", 587, "", "pwd")) {