In this brief topic you will understand, how to send Word Document in email using Java. You can easily convert the source file to MHTML format and specify the email settings to send the email in any common operating system like MS Windows, Ubuntu, or macOS using simple API calls.
Steps to Send Word Document in Email using Java
- Add Aspose.Words and Aspose.Email JAR file reference from the Maven repository
- Instantiate a Document Class object to load the input document for sending email
- Specify different SaveOptions to save file in MHTML format to save data into a stream
- Load the file using MailMessage class
- Initialize SMTP client to configure email message
- Send email message containing the word document
In these steps, first of all we load the input Word document in DOCX or DOC file format. Then it is converted to MHTML file using the stream object, which is loaded as an input file for sending the email. Finally, we specify the configuration properties like sender, subject, port to send the email message.
Code to Send DOCX in Email with Java
import com.aspose.words.Document; | |
import com.aspose.words.License; | |
import com.aspose.words.SaveFormat; | |
import com.aspose.email.MailMessage; | |
import com.aspose.email.SmtpClient; | |
import com.aspose.email.SecurityOptions; | |
public class SendWordocumentInEmailJava { | |
public static void main(String[] args) throws Exception { // main method for sending word document in email using Java | |
// Set Aspose.Words license before converting sending word document in email using Java | |
License WordLicense = new License(); | |
WordLicense.setLicense("Aspose.Word.lic"); | |
License EmailLicense = new EmailLicense(); | |
EmailLicense.setLicense("Aspose.Email.lic"); | |
// Load the document for sending as email using Document class | |
Document EmaiDocument = new Document("EmailTest.docx"); | |
SaveOptions options = null; | |
options.setSaveFormat(SaveFormat.MHTML); | |
// Convert the document to MHTML format by using memory stream | |
ByteArrayOutputStream EmailStream = new ByteArrayOutputStream(); | |
EmaiDocument.save(EmailStream, options); | |
ByteArrayInputStream stream = new ByteArrayInputStream(EmailStream.toByteArray()); | |
// Create an Aspose.Email message from the saved stream | |
com.aspose.email.MailMessage EmailMessage = | |
com.aspose.email.MailMessage.load(stream, new com.aspose.email.MhtmlLoadOptions()); | |
// Set properties of email to send | |
EmailMessage.setFrom(com.aspose.email.MailAddress.to_MailAddress("sender@sender.com")); | |
EmailMessage.getTo().add("your_to_email@email.com"); | |
EmailMessage.setSubject("Test Message for testing Aspose.Words and Aspose.Email APIs"); | |
// Initialize SMTP client and it's properties to send email | |
SmtpClient SMTPClient = new SmtpClient(); | |
SMTPClient.setHost("smtp.gmail.com"); | |
SMTPClient.setUsername("YourEmail@gmail.com"); | |
SMTPClient.setPassword("Your Gamil Password"); | |
SMTPClient.setPort(587); | |
SMTPClient.setSecurityOptions(SSLExplicit); | |
// Send word email message | |
SMTPClient.send(EmailMessage); | |
} | |
} |
In the code snippet above, we have loaded the source DOCX file while using an object of the Document class. It is then converted to MHTML file using streams that is to be later processed by Aspose.Email. Then we set different preferences for sending the email message like email to, email from, subject and host settings to control email message properties. This enables you to automate email sending feature and send document in email with Java.
Previously, we learned about how to convert Word to Markdown using Java. Whereas, in this topic we focused on rendering DOCX to TIFF. Whereas, in this topic, we have learnt how using Java send Word Document in Email.