Create and Send Outlook Email Messages using Java

Create and Send Outlook Emails Java

Aspose.Email for Java is a powerful email processing API that provides all the basic as well as advanced features for email management. It allows you to create Outlook email messages, read existing emails, and convert MSG, EML, PST, OST, MBOX, and MHT messages to other formats from within your Java applications. In this article, I’ll demonstrate some basic yet important features of how to create and send Outlook emails using Java.

I am going to cover the following features of creating and sending emails in this article:

Java Email API – Installation

Aspose.Email for Java can be downloaded from the Downloads section or installed using the following Maven configurations.

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 create email messages and save them in your desired email format including EML, EMLX, MSG, and MHTML. The following are the steps to create an email message and save it as a file.

  • Create an instance of MailMessage class.
  • Set the message’s properties such as Subject, Body, To, CC, etc.
  • Add attachments if required.
  • Save the email message in your desired format using MailMessage.save() method.

The following code sample shows how to create an Outlook email message using Java.

Create an Email with HTML Body using Java

In the previous example, we simply created a plain text email and saved it as an email file. However, most of the emails today are created with HTML body to well organize and present the email’s content. In this case, HTML tags are used to specify the layout of the email. Let’s see how to create an email with an HTML body using Aspose.Email for Java.

The following code sample shows how to create an email with an HTML body using Java.

Create an Email with a Particular Encoding using Java

Aspose.Email for Java also allows you to set the desired text encoding for the email messages. The encoding standard is used to tell the email application or browser about how to interpret the characters in the email’s body. The following are the steps to create an email with a particular encoding standard.

The following code sample shows how to create an email with a particular encoding standard using Java.

Send Outlook Emails using Java

Once you have created an email, you can send it to its recipients using Simple Mail Transfer Protocol (SMTP). The following are the steps to send an email message using Aspose.Email for Java.

  • Create a new email message or load an existing one using the MailMessage class.
  • Create an instance of the SmtpClient class and set its host, username, password, and port.
  • Send email using SmtpClient.send() method.

The following code sample shows how to send an email via SMTP client in Java.

Send Emails Asynchronously using Java

Aspose.Email for Java also lets you send emails asynchronously. For this, you can use the SmtpClient.beginSend() method. The following code sample shows how to send emails asynchronously using Java.

Send Emails with a Read Receipt using Java

You can also add a request for a read receipt to the email messages. This feature allows you to receive a notification after the email has been received. The following are the steps to create and send an email message containing a request for read receipt.

The following code sample shows how to send an email containing a read receipt using Java.

Send Bulk Emails using Java

There might be the case when you need to send a bulk of different emails. In that case, you can simply add all the email messages to a MailMessageCollection object and pass it to SMTP client for sending. The following code sample shows how to send a bulk of emails using Java.

Live Demos

Conclusion

In this post, I have shown you how to create Outlook email messages and save them as EML, MSG, and MTH formats using Java. In addition, different ways of sending emails in Java using the STMP client are also discussed. You can learn more about Aspose’s Java Email Library using the documentation.

See Also