What Java Mail API? How can you use this API to send email messages?
The JavaMail is an API that is used to compose, write and read electronic messages (emails). The JavaMail API provides a protocol-independent and platform-independent framework for sending and receiving mails. The java.mail and javax. mail. activation packages contain the core classes of JavaMail API. The JavaMail facility can be applied to many events. It can be used at the time of registering the user (sending notification such as thanks for your interest to my site), forgot password (sending password to the user's email id), sending notifications for important updates etc. So there can be various usage of java mail API.
We can use this API in three steps to send email message , they are described below:-
STEPS TO SEND EMAIL USING JAVAMAIL API
There are following three steps to send email using JavaMail.
They are as follows:
1. Get the session object
It stores all the information of host like host name, username, password etc. The javax.mail.Session class provides two methods to get the object of session, Session.getDefaultInstance() method and Session.getInstance() method. We can use any method to get the session object. Method of Session class are:
Methods
Session getDefaultInstance(Properties p)// Returns the default session
Session getDefaultInstance(Properties p, Authenticator a)// Returns the default session
Session getInstance(Properties p)// Returns the new session
Session getInstance(Properties p, Authenticator a)// Returns the new session
Example of getDefaultInstance() method
Properties properties=new Properties();
Session session=Session.getDefaultInstance(properties,null);
2. Compose the message
The javax.mail.Message class provides methods to compose the message. But it is an abstract class so its subclass javax.mail.internet.MimeMessage class is mostly used. To create the message, you need to pass session object in MimeMessage class constructor. For example,
MimeMessage message=new MimeMessage(session);
Now message object has been created but to store information in this object MimeMessage class provides many methods.
Let's see methods provided by the
Methods
setFrom (Address address)//It is used to set the from header field
addRecipient(Message.RecipientType Address address)//It is used to add the given address to the recipient type
addRecipients(Message.RecipientType type, Address] addresses)//It is used to add the given addresses to the recipient type.
set Subject(String subject)// It is used to set the subject header field.
setText(String textmessage)// It is used to set the text as the message content using text/plain MIME type.
Example to compose the message
MimeMessage message=new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.addRecipient (Message.RecipientType. To, new InternetAddress(" [email protected]"));
message.setHeader("Hi, everyone"); message.setText("Hi, This mail is to inform you...");
3. Send the message
The javax.mail.Transport class provides method to send the message. Commonly used methods of Transport class are:
Methods
send(Message message)//It is used send the message
send(Message message, Address[] address)// It is used send the message to the given addresses
Example to send the message
Transport.send (message);
Example of sending email using JavaMail API
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*; import javax.activation.*;
class SendEmail extends Authenticator
{
static String from = "[email protected]"; static String pass="arjun@2039";
InternetAddress to;
public static void main(String [] args) throws Messaging Exception
{
//Get the session object
Properties properties = System.getProperties(); properties.put ("mai 1.smtp.auth", "true"); properties.put("mail.smtp.starttls.enable", "true");
//starts TLS (tranport layer security)
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail. smtp.port", "587");
properties.put("mail. smtp.user", from); properties.put("mail.smtp.password", pass);
Session
session =Session.getDefaultInstance (properties);
//compose the message
MimeMessage msg = new MimeMessage(session);
msg.setFrom (new
InternetAddress("[email protected]"));
to= new InternetAddress("[email protected]");
msg.addRecipient (Message. RecipientType. TO, to);
msg.setSubject("Test Java Mail");
msg.setText("Hello, this is example of sending email");
// Send message
Transport transport = session.getTransport ("smtp");
transport.connect("smtp.gmail.com", from, pass); transport.sendMessage(msg, msg.getAllRecipients());
System.out.println("message sent successfully....");
}
}
Output
message sent successfully....
Comments
Post a Comment