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

Popular posts from this blog

Suppose that a data warehouse for Big-University consists of the following four dimensions: student, course, semester, and instructor, and two measures count and avg_grade. When at the lowest conceptual level (e.g., for a given student, course, semester, and instructor combination), the avg_grade measure stores the actual course grade of the student. At higher conceptual levels, avg_grade stores the average grade for the given combination. a) Draw a snowflake schema diagram for the data warehouse. b) Starting with the base cuboid [student, course, semester, instructor], what specific OLAP operations (e.g., roll-up from semester to year) should one perform in order to list the average grade of CS courses for each BigUniversity student. c) If each dimension has five levels (including all), such as “student < major < status < university < all”, how many cuboids will this cube contain (including the base and apex cuboids)?

Suppose that a data warehouse consists of the four dimensions; date, spectator, location, and game, and the two measures, count and charge, where charge is the fee that a spectator pays when watching a game on a given date. Spectators may be students, adults, or seniors, with each category having its own charge rate. a) Draw a star schema diagram for the data b) Starting with the base cuboid [date; spectator; location; game], what specific OLAP operations should perform in order to list the total charge paid by student spectators at GM Place in 2004?

Discuss classification or taxonomy of virtualization at different levels.