Sending a mail using Java Code.
Jul 13, 2015
Make sure to subscribe to our newsletter and be the first to know the news.
Jul 13, 2015
Make sure to subscribe to our newsletter and be the first to know the news.
Java Mail API provides the collection of classes and interfaces which models sending of mail.
For working with Java Mail we require two jar files to be set in classpath or imported in some IDE.
mail.jar
and
activation.jar
You can download both the jar files from above provided links.
In case you are new to setting class path you can check out with the following video.
Java Mail API consists of some very important classes and interfaces.
We will cover at least those required for sending a mail.
All the classes and interfaces required to work with Java Mail API are in package
1. javax.mail
Classes here models the mail system.
2. javax.mail.internet
Classes here are specific to internet mailing systems,
Session class represents the session acquired with the mail account.
For getting this session all the required properties as well as authentications have to be passed.public static Session getDefaultInstance(Properties prop, Authenicator auth)
getDefaultInstance() is the static method used of session to acquire the session with the mail account.
It requires the Properties object filled as well as an Authenticator object.
Let us have a look how to fill up that Properties object.
Properties props=new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.host","smtp.gmail.com");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
Authenticator auth=new SMTPAuthenticator();
.
.
.
.
public class SMTPAuthenticator extends Authenticator
{
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(email,password);
}
}
Now let us see how session object is created.
Session session=Session.getDefaultInstance(props,auth);
MimeMessage(Session session)
public void setSubject(String subject)
public void setFrom(Address address)
public void setContent(Object o, String type)
public void setContent(MultiPart multipart)
public void addRecipient(Message.RecipientType type, Address address)
Message.RecipientType.TO
Message.RecipientType.CC-- Carbon Copy
Message.RecipientType.BCC-- Blind Carbon Copy
MimeMessage msg = new MimeMessage(session);
msg.setSubject("My first mail");
msg.setText("This is message body");
msg.setFrom(new InternetAddress("youremail@gmail.com"));
msg.addRecipient(Message.RecipientType.TO,new InternetAddress("someone@gmail.com"));
Once set we need to send the message using Transport object
public static void send(Message msg)
Transport.send(msg);
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class SendMail
{
String myEmail,myPassword;
SendMail()
{
myEmail = "youremail@gmail.com";
myPassword="yourpassword";
String host="smtp.gmail.com";
String port="465";
String recAdd="someone@gmail.com";
Properties prop = new Properties();
prop.put("mail.smtp.auth","true");
prop.put("mail.smtp.socketFactory.port",port);
prop.put("mail.smtp.starttls.enable","true");
prop.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
prop.put("mail.smtp.socketFactory.fallback","false");
prop.put("mail.smtp.host",host);
SecurityManager manager=System.getSecurityManager();
try
{
Authenticator auth= new SMTPAuthenticator();
System.out.println("Authenticated");
Session session =Session.getDefaultInstance(prop,auth);
System.out.println("Session Acquired");
MimeMessage msg = new MimeMessage(session);
msg.setSubject("My first mail");
msg.setFrom(new InternetAddress(myEmail));
msg.setText("This is message body");
msg.setRecipient(Message.RecipientType.TO,new InternetAddress(recAdd));
System.out.println("Message ready ");
Transport.send(msg);
System.out.println("Mail Sent");
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
new SendMail();
}
private class SMTPAuthenticator extends Authenticator
{
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(myEmail,myPassword);
}
}
}