Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active April 26, 2021 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/29940e46a37422a80882208f20218ee1 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/29940e46a37422a80882208f20218ee1 to your computer and use it in GitHub Desktop.
Send Word Document as Email in Java
// Load the document
Document doc = new Document("Document.doc");
// Save to an output stream in MHTML format.
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
doc.save(outputStream, SaveFormat.MHTML);
// Load the MHTML stream back into an input stream to use with Aspose.Email.
ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
// Create an Aspose.Email MIME email message from the stream.
MailMessage message = MailMessage.load(inputStream);
message.setFrom(new MailAddress("your_from@email.com"));
message.getTo().add("your_to@email.com");
message.setSubject("Aspose.Words + Aspose.Email MHTML Test Message");
// Save the message in Outlook MSG format.
message.save("Message.msg", SaveOptions.getDefaultMsg());
// Send the message using Aspose.Email
SmtpClient client = new SmtpClient();
client.setHost("your_smtp.com");
client.send(message);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment