How to send mail using JavaMail API

How to send mail using JavaMail API

Sending emails is a common task in most of the real life applications. Java provides a robust API(JavaMail) that we can use to send emails using SMTP server. In this post we will see how to send mail using JavaMail API, with simple example programs.

JavaMail API

The JavaMail API defines classes which represent the components of a mail system. JavaMail does not implement an email server, instead it allows you to access an email server using a Java API. While the JavaMail API specification does not mandate support for specific protocols, JavaMail typically includes support for POP3, IMAP, and SMTP. JavaMail API supports both TLS and SSL authentication for sending emails.

JavaMail API is not part of standard JDK, so you will have to download it from it’s official website – JavaMail API. Download the latest version of the JavaMail reference implementation and include it in your project build path. The jar file name is javax.mail.jar.

If you are using Maven based project, add below dependency in your project.

 Sending Email in Java

To send an email in Java, you must have access to an SMTP server. You must know the host name, port number, and security settings for your SMTP server.

Send a Simple E-mail(Text only)

The below Java program sends a simple text email without any authentication.

If everything is setup correctly, when you run the above program, you should see “Mail sent successfully…” as an output in the console .

If you want to send mail to only one recipient, then you can use the below code to set the recipient.

Send HTML content in E-mail

Here is an example program to send an HTML e-mail. This example is very similar to the previous one, except that here we are using setContent() method (instead of setText()) to set content whose second argument is “text/html” which specifies that the HTML content is included in the message.

Sending Attachment in E-mail

Here is an example program to send an e-mail with attachment. To send a file as an attachment, we need to create an object of javax.mail.internet.MimeBodyPart and javax.mail.internet.MimeMultipart. First create a body part for the text message in the email and create another body part for the file. Use FileDataSource to attach the file in second body part. Now attach both the body parts to the multipart body.

That’s it. Mail will be sent with the file “sample.txt” as an attachment. You can extend the above program to attach multiple files too.

Send mail in Java with attached image inside the content

You must have seen so many emails that have image attachment and the same image is also used in the email body. In this case you can attach the image file like any other attachment and then set the Content-ID header for image file and then use the same content id in the email message body with <img src=’cid:Content-ID’> tag. Here is an example program.

Send Email in Java with TLS Authentication

Let us create a example program in Java to send mail with TLS authentication. The program is similar to previous examples except that we need to set some extra properties to enable TLS authentication.

You can set your username and  password in the above program and run for yourself since GMail SMTP server is accessible to all.

Send Email in Java with SSL Authentication

In this program we will use SSL authentication to send mail. This program is same as TLS program except that the properties to enable SSL authentication is different.

That’s all about sending mail in Java using JavaMail API. If you have any doubt, post it in the comments section.

Additional Information

Sending an Email using the JavaMail API

The following two tabs change content below.
Working as a Java developer since 2010. Passionate about programming in Java. I am a part time blogger.

Add Comment

Required fields are marked *. Your email address will not be published.