How to send an Email in JAVA

How to send an Email in Java

Quite often you will need to send an email as part of your program. Maybe it will be to confirm creation of an account or to remind users of a forgotten password. Because it is such a common requirement, there are loads of tutorials out there on how to send an email using Java. Unfortunately, many of these can be quite confusing and come with a lot of unnecessarily long code.

This tutorial gives a simplified version of how to send an email in java. You will learn how to use the JavaMail API using only 6 steps.

NOTE: It is assumed you are familiar with Java web application development but do not have the API set up in your development environment. If it is already set up and you just need the code, feel free to skip step one.

Ok, let’s begin!

1. Download appropriate jar files

To get the API running, you will need to download the latest JavaMail API and add the jar files to your CLASSPATH. If you are using Eclipse, you can just drop them in your WEB-INF/lib folder. We are particularly interested in the following files:

  • smtp.jar
  • mail.jar
  • pop3.jar

If you’re using an older java version (1.6 or lower) you will also need the activation.jar.

Thats it, you’re all set up.

2. Configure web deployment descriptor

Next, you have to configure the settings for SMTP server in the web deployment descriptor file (web.xml). In Eclipse, the web.xml file is usually found directly under the WEB-INF folder. Configure this file as shown below.

  • Change the value of EMAIL ADDRESS to the first part of your email address (what comes before the ‘@’ symbol).
  • Change the value of the PASSWORD to this emails password.
  • You may also want to change the value of the ‘host’ and ‘port’. There are quite a number of different smtp and pop3 servers that you can use and each of them has a different port. The full list is found here. In this case, I will be using smtp.gmail.com which runs on port 587.
  • Configuring the welcome-file-list is optional. It simply means that when you run the application, it will directly run the sendEmail.jsp file.

3. Enable POP3 access in your email account.

This step is not necessary for all server types. If you are not using Office365.com or Gmail.com, you can skip this step.

  • In Gmail, you need to enable less secure apps through your account settings.
  • In Office365, you need to enable POP access via “Settings” > “Options” > “Account” > “My Account” > “Settings for POP and IMAP Access”.

This is not usually a requirement for other providers, but if the application does not seem to work for your account, just ensure that POP access is enabled for your account by changing your email account settings.

4. Create your Form in JSP.

Create a form that will get the email address to send an email to. You can also use this form to get the message body but in this example, the message is hardcoded so we really only need the email address.

Your form should look something like this:

form

5. Create your EmailUtility class.

Next create the EmailUtility class (based on this article) and add it to your src folder.

6. Write your email-sending Servlet.

Lastly, write the email-sending Servlet.

This is the email I receive when I run the application with my credentials.

That was easy right? Feel free to use this code in your work.

Seda
Seda Kunda is a web designer and developer with a degree in Computer Science and a great passion for code. Besides code, she enjoys pepperoni pizza, watching the bachelor and sleeping in on Saturdays.
Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn