Java mail

JavaMail Projects

JavaMail Project 1

JavaMailAPI In Using Reviewing Core Classes
Previous Home Next

In this phase walks to through the core classes that make up the API: Session, Message , Address , Authenticator, Transport, Store and Folder. All these classes is implementing in the JavMailAPI , and this package is known as javax.mail , and it's package import in the Java Mail API. You'll frequently find this classes and using in the javax.mail.internet package.


The Session class defines a basic mail session. Actually Session is the object and this takes the advantages of this object get information n like mail server, username, password, and other information that can be shared across your entire application. The constructor of this class are private. If you can access this class then you can get a single default session can be shared with the getDefaultInstance () method:

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

If you can create a unique Session with getInstance ();

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


If you have your Session object, then you send the Message . Then this time type the Message. Actually the Message is the Abstract Class, and you are working to subclass, in most cases using javax.mail.internet.MimeMessage . The MimeMessage is an e-mail message that's understand to MIME types and headers , as defined in the different RFCs. The message headers are restricted to US-ASCII characters only.

To create a Message, pass along the Session object to the MimeMessage constructor:

MimeMessage message = new MimeMessage(session);

If you have a Message then after you set the parts, as Message implements the part interface (Then MimeMessage implementing MimePart ). And the basic mechanism to set the content is the setContent() method.

message.setContent("r4r", "text/plain");

And when time you are working with a MimeMessage, and if your message is write on plain text then you can using setText () method , which only requires the actual content, defaulting to the MIME type of text/plain:message.setText("r4r"); For setting the subject then you using setsubject () method message.setSubject("hello r4r");


when you set Session And Message. because you set the Message field then filled the Message with content , so it's essential to using the Address class. The Address class like as the Message class ,it means is a Abstract class. then you use the javax.mail.internet.InternetAddress class. And you create the only e-mail address , pass the e-mail address to the constructor.

Address address = new InternetAddress("");

If you want to show the name next to e mail then using to pass that along to the constructor too;

Address address = new InternetAddress("", "RAJ");

You will need to create address objects for the message's from field as well as the to field. Unless your mail server prevents you, there is nothing stopping you from sending a message that appears to be from anyone. When you have created the Addresses then you can connect to the message for one of two ways.

For identifying the sender, you using the setForm() And setReplyTo() methods. message.setFrom(address)

If your message needs to show multiple from addresses, use the addFrom() method:Address address[] = ...; message.addFrom(address);

you identifying the message recipients, you use the addRecipient () method .

This Method Requires a Message.RecipientType besides the address.message.addRecipient (type, address)

Most Probably there are three predefined type of address are:

  1. Message.RecipientType.TO
  2. Message.RecipientType.CC
  3. Message.RecipientType.BCC


Address toAddress = new InternetAddress("");
Address ccAddress = new InternetAddress("");
message.addRecipient(Message.RecipientType.TO, toAddress);
message.addRecipient(Message.RecipientType.CC, ccAddress);


The classes, The JavaMail can take advantage of an Authenticator .The Authenticator access the protected resources via a username and password. For the JavaMail API is resources is mail server. The Authenticator found the javax.mail package and is different from the class of the same name. The two don't share the same Authenticator as the JavaMail API works with Java 1.1, which didn't have the variety.

Authenticator you using your subclass the abstract class and return a Password Authentication instance for the getPasswordAuthentication () method. When you created the Session then you must register the Authenticator. Then your Authenticator will be notified when Authentication is necessary. Then you could pop the window and read a username and password from a configuration file. it's not encrypted then it's not secure. and returning them to caller and PasswordAuthetication objects.

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