New to Java - New to java tutorial

This page discusses - New to Java - New to java tutorial

New to Java - New to java tutorial

Technical description of Java Mail API

     

This section introduces you with the core concepts of Java Mail API. You must understand the Java Mail API before actually delving into the development of mail processing applications.

Core classes that makeup the Java Mail API
Lets understand the core Java Mail API classes.

Session

Session is the top-level entry class representing mail session. It uses java.util.Properties object to get information about mail server, username, password etc. This class has a private constructor and you can get session objects through getInstance() and getDefaultInstance() methods. getDefaultInstance() method provides default Session object which takes Properties and Authenticator objects as arguments.

Properties props = new Properties();
// code to fill props with any information
Session session = Session.getDefaultInstance(props, null);

Or create a unique session by getInstance() method…
Properties props = new Properties();
// code to fill props with any information
Session session = Session.getInstance(props, null);

Message

After creating session object, create message to send by using Message class. Because of message class is abstract class so we will use subclass javax.mail.internet.MimeMessage.

MimeMessage message = new MimeMessage(session)
// set the content of message by setContent() method
message.setContent(“www.roseindia.net”, “text/plain”);

We can also use setText() method to set the content.

Message.setText(“www.roseindia.net”);

To create subject line of sending message use setSubject() method.

Message.setSubject(“Its Urgent Message”);

Address

Now we will define address, Address class is also an abstract class so we will use here class javax.mail.internet.InternetAddress.

Address address = new InternetAddress("[email protected]");

To display a name next to the Email Address use one more argument for name.

Address address = new InternetAddress(“[email protected]”, ”Mahendra”);

After creating address connect with message by two ways:


1: By setFrom()method: message.setFrom(address);
1: By setReplyTo()method: When want to send reply to more addresses.

Address address[] = ...;
Message.setReplyTo(address[]);

To identify the recipient, add the recipients by using addRecipient() method….

message.addRecipient(type, address);

Address types are of three types:
* Message.RecipientType.TO: primary recipient
* Message.RecipientType.CC: carbon copy
* Message.RecipientType.BCC: blind carbon copy

To know more about address type see class Message.RecipientType.

Authenticator

The JavaMail Authenticator is found in the javax.mail package, and used as an authenticator to access protected resources by prompting the user for username and password.

Properties props = new Properties();
// fill props with any information
Authenticator auth = new MyAuthenticator();
Session session = Session.getDefaultInstance(props, auth);

Transpor

This is final part of sending Email, it is an abstract class. Default version of this class can be used by calling static send() method.

Transport.send(message);

Or can create a specific instance from the session for defined protocol….

message.saveChanges(); // implicit with send()
Transport transport = session.getTransport("smtp");
transport.connect(host, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();

Store and folder

After getting the session you connect to javax.mail.Store class with Authenticator or host, port, user and password information. Store object that implements the specified protocol can be created by by passing the protocol information to the getStore() method of the session object.

// create object of store class.
Store store = session.getStore("pop3");
store.connect(host, username, password);

After connecting to store you need to get a folder that holds messages. For POP3, the only folder available is the INBOX but with IMAP, you can have other folders available. For this you can use javax.mail.Folder class….

Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
Message message[] = folder.getMessages();

Once you have read messages, you need to close Store and Folder…

folder.close(booleanValue);
store.close();