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.

How to write code for sending emails in Java

How to write code for sending emails in 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>22.12</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 for sending emails with plain text

Following are the steps to create and send an email message with plain text:

  • Create an object of MailMessage class.
  • Specify the subject, body, sender’s, and recipient’s addresses.
  • Create an object of SmtpClient class.
  • Set host, port, username, password, and security options.
  • 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:

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:

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:

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