Email automation has become popular, specifically among marketers. It empowers them to send pre-made emails automatically to their target audience at the scheduled time.

Java Code for Sending Email

Sending email with Java

In this article, we explain how to write code for sending email in Java. We will see the steps to create and send emails with plain text or HTML body programmatically. In addition, we will demonstrate how to send emails in bulk and sending of stored emails from the disc using Java.

This article will cover the points given below:

Java library for sending emails

We will use Java email library, which allows you to implement your business logic for managing emails without knowing about network programming or format specifications. The Java library for sending emails can be installed directly by using the Maven configuration as given below:

<repository>
	<id>ConholdateJavaAPI</id>
	<name>Conholdate Java API</name>
	<url>https://releases.conholdate.com/java/repo</url>
</repository>        
<dependency>
	<groupId>com.conholdate</groupId>
	<artifactId>conholdate-total</artifactId>
	<version>23.6</version>
	<type>pom</type>
</dependency>

For configurations about other Java frameworks, please visit releases.conholdate.com.

How to write code to create and send an email in Java

The Java email library sends the email messages through an SMTP server for delivery. The sender, recipients, subject, and message body are specified using String objects. In this section, we will discuss how to write code samples in Java to send emails with plain text as well as HTML body.

Java code to send email with plain text

Following are the steps to create and send an email with SMTP:

  1. Create an object of MailMessage class.
  2. Specify the subject, body, sender’s, and recipient’s addresses.
  3. Create an object of SmtpClient class.
  4. Set host, port, username, password, and security options.
  5. Call SmtpClient.send(MailMessage arg) method to send email message.

The code sample below shows how to create and send an email message with plain text:

Sending emails with HTML body

Following are the steps to send an email message with an HTML body:

  1. Create an object of MailMessage class.
  2. Specify the subject, body, sender’s, and recipient’s addresses.
  3. Set body of the email using MailMessage.setHtmlBody(String value) method.
  4. Create an object of SmtpClient class.
  5. Set host, port, username, password, and security options.
  6. Call SmtpClient.send(MailMessage arg) method to send email message.

The code sample below shows how to send an email message with HTML body.

Sending stored emails from the disc in Java

The Java email library allows you to load the EML (Outlook Express Electronic Mail) files from the disc and send them via SMTP.

The steps to send the stored email messages from the disc are given below:

  1. Create an object of MailMessage class by using MailMessage.load(String path) method.
  2. Create an object of SmtpClient class.
  3. Set host, port, username, password, and security options.
  4. Call SmtpClient.send(MailMessage arg) method to send email message.

The code sample below demonstrates how to send the stored emails from a disc in Java.

Java code to send emails in bulk

The following are the steps for writing Java code for sending emails in bulk:

  1. Create multiple objects of MailMessage class and specify their necessary parameters or properties.
  2. Create an object of MailMessageCollection class.
  3. Add email message objects (as created in 1st step) to the collection using MailMessageCollection.addItem() method.
  4. Create an object of SmtpClient class. and set the host, port, username, and password.
  5. Send email using SmtpClient.send(MailMessageCollection arg) method.

The code sample below demonstrates how to send emails in bulk using Java.

Get a free license

You can get a free temporary license to try the API without evaluation limitations.

Summing up

In this article, you have gained knowledge about how to write code for sending emails in Java using a Java library for sending emails. Initially, you learned how to send an email with plain text or HTML body programmatically. Afterward, you saw how to send emails in bulk and sending of stored emails from the disc using Java.

You may visit the documentation to explore other features.

Lastly, we would suggest you stay tuned at conholdate.com for regular updates.

Ask a question

You can let us know your questions or queries on our forum.

See Also