
Aspose.Email for Java is a comprehensive email processing API that enables developers to create, read, convert, and send Outlook email messages directly from Java applications. It supports MSG, EML, PST, OST, MBOX, and MHT formats and offers both synchronous and asynchronous sending via SMTP. In this guide, we’ll explore essential techniques for creating and sending Outlook emails using Java.
The article covers the following topics:
- Create Outlook email message using Java
- Set HTML body for email using Java
- Create an email with a particular encoding using Java
- Send Outlook emails using Java
- Asynchronously send emails using Java
- Send an email with a read receipt using Java
- Send bulk emails using Java
Java Email API - Installation
Aspose.Email for Java can be downloaded from the Downloads page or added to your project via Maven.
Repository:
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
Dependency:
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-email</artifactId>
<version>20.3</version>
<classifier>jdk16</classifier>
</dependency>
Create Outlook Email Messages using Java
Aspose.Email for Java lets you build email messages and store them in formats such as EML, MSG, and MHTML. Follow these steps to create a basic Outlook email and save it to disk:
- Instantiate the MailMessage class.
- Define essential properties like Subject, Body, To, and CC.
- Attach files when needed.
- Persist the message using the MailMessage.save() method.
The sample below demonstrates how to create and save an Outlook email message with Java.
Create an Email with HTML Body using Java
Most modern emails use HTML to provide rich formatting and interactive content. Using Aspose.Email for Java, you can easily set an HTML body for your message.
- Create a new MailMessage instance.
- Populate Subject, To, CC, and other standard fields.
- Apply HTML content with the MailMessage.setHtmlBody(string) method.
- Save the message using MailMessage.save().
The following example shows how to generate an email that contains an HTML body.
Create an Email with a Particular Encoding using Java
Correct character encoding ensures that recipients see the email content as intended, especially for non‑ASCII characters. Aspose.Email for Java allows you to specify the body encoding explicitly.
- Create a MailMessage object.
- Set standard properties such as Subject, To, and CC.
- Define the desired charset using MailMessage.setBodyEncoding().
- Save the email with MailMessage.save().
The code snippet below illustrates how to create an email with a specific text encoding.
Send Outlook Emails using Java
After constructing an email, you can deliver it through an SMTP server. The steps below outline how to send a message with Aspose.Email for Java.
- Load or create a MailMessage instance.
- Configure an SmtpClient with host, port, username, and password.
- Call SmtpClient.send() to transmit the email.
The example demonstrates sending an Outlook email via SMTP.
Send Emails Asynchronously using Java
For high‑throughput scenarios, sending emails asynchronously improves performance. Use the SmtpClient.beginSend() method to dispatch messages without blocking the calling thread.
The code below shows how to perform asynchronous email sending in Java.
Send Emails with a Read Receipt using Java
A read receipt notifies you when the recipient opens the message. Aspose.Email for Java lets you request this receipt by setting delivery notification options.
- Create a MailMessage object.
- Enable read receipt via MailMessage.setDeliveryNotificationOptions().
- Optionally add custom receipt headers.
- Send the message using the SmtpClient.
The following sample sends an email that requests a read receipt.
Send Bulk Emails using Java
When you need to dispatch many personalized messages, group them in a MailMessageCollection and pass the collection to the SMTP client. This approach reduces overhead and simplifies bulk sending.
The snippet below demonstrates sending a batch of emails in Java.
Live Demos
Conclusion
This guide showed how to create Outlook email messages in Java, customize them with HTML bodies or specific encodings, and send them synchronously, asynchronously, with read receipts, or in bulk using the Aspose.Email SMTP client. Explore the full capabilities of Aspose’s Java Email Library in the official documentation.