Sending email in Java

In this chapter we are discussing how to send email using gmail account. here we are using SMTP as the protocol.SMTP is the Simple Mail Transfer Protocol.SMTP can be used to send and receive mails.

Sending email in Java

We are looking into an application which sends a mail to a recipient mail id. The username , password, and other configurations are loading from a properties file.

Step1)We  need to create a dynamic web project in eclipse.(If we need to access mails from J2SE application , then we can download Java Mail API  from  here.Extract the zip file and include the jar files  in the project path).If jdk and eclipse is not installed, please install it.Eclipse Indigo is here for download. Java EE sdk  can be downloaded  from here.

Step2)Create the properties file from which the login credentials as well as other configurations are loading.

mailconfig.properties

#defines the various attributes like login credentials and mail ids
username=
password=
fromAddress=xxxxxxx@gmail.com
toAddress=yyyyyyyy@gmail.com
mail.smtp.auth=true
mail.smtp.starttls.enable=true
mail.smtp.host=smtp.gmail.com
mail.smtp.port=587

Step3)Create the SendMailSample.java

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMailSample {
private Properties properties = null;
private Session session = null;
private Message message = null;
String fromId;
String toId;
String configPath = "";

public SendMailSample() {

}

public void sendMail() {
properties = new Properties();
try {
properties.load(new FileInputStream(new File(configPath + "/"
+ "mailconfig.properties")));
fromId = properties.getProperty("fromAddress");
toId = properties.getProperty("toAddress");

} catch (FileNotFoundException e1) {

e1.printStackTrace();
} catch (IOException e1) {

e1.printStackTrace();
}
session = Session.getInstance(properties,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(properties
.getProperty("username"), properties
.getProperty("password"));
}
});
message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(fromId));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(toId));
message.setSubject("Sample mail");
message.setText("Hello This is a message");
Transport.send(message);
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}

System.out.println("Done");
}

public static void main(String[] args) {
SendMailSample sample = new SendMailSample();
sample.sendMail();
}

}

The application simply creates a session object using the credentials. Then creating a MimeMessage object and setting the attributes to it. Finally using the Transport.send() , the mail is sending to recipient email id.

Output

Provide the fromAddress ,toAddress,username and password values in mailconfig.properties. Compile and run the application.When the mail sent the ‘Done’ message will be displayed on the console.

See Related:

Java Mail API overview

SMTP

sending email with attachment

POP

Reading email using POP