Java Mail API: Transforming Mail into Data Carriers
Java Mail is eminently adaptable. Indeed, looking at its API, we find that it helps us provide an alternative solution to an old problem: how to transfer large amounts of data between two enterprise applications. In this article, you will discover how to adapt Java Mail to play the role of a data carrier.
Java Mail API: Transforming Mail into Data Carriers - Java Mail Core Classes Continued (Page 2 of 5 )
The foundation classes of the core of Java Mail deal with connection and authentication. The remaining three classes form the layer that deals with transportation of the mail and data. These classes are:
Without much ado, here are the details.
The Message class represents an email message. Each message is tightly coupled with a session. This is done by passing an object of the Session class. But the Message class is abstract. Hence to work with the Message class, its concrete subclass has to be used -- the MimeMessage class. A MimeMessage represents an email message that can understand MIME types and headers. As stated earlier, a message is coupled with a session by passing a Session object to the Message class. So to create an object, the MimeMessage class passes an object of the Session class to a constructor of MimeMessage, thus:
MimeMessage message = new MimeMessage(session);
In the real world messages can be both plain as well as HTML flavored. To achieve this end, Java Mail provides Part and MimePart interfaces, which have been implemented by Message and MimeMessage respectively. To start working with Message, the next thing to do after retrieving an object is to set the content type. That is done by calling the setContent() method of the Message object. The setContent() method takes two parameters -- content and MIME type –- in the form of Strings. In code it would be:
Since most of the time the object of MimeMessage would be used, just calling setContent() with the content as the default value for MIME type in this case means ‘text/plain’ only. Similarly, the subject of the message can also be set using the setSubject() method of the Session object.
The message has been written. What is needed to send it? An address. The Address class encapsulates an Internet address. Again this class is abstract. So one must work with the InternetAddress class, which is a concrete subclass of the Address class. To get an object of the InternetAddress class, pass the receiver's address to the constructor as shown:
Address address= new InternetAddress(“email@example.com”);
To show the name of the user as well, pass it as the second argument:
Address address= new InternetAddress(“firstname.lastname@example.org”,”Raj”);
The next query that should arise is how are Address and Message objects connected? The answer is through the methods of the Message class. The Message class has three methods: setFrom(), setReplyTo() and addFrom(). The first two methods accept an object of Address class as parameter whereas the last one takes an array of Address. For example,
sets the address of the message to “email@example.com”. Now to actually send the message, the recipient's address has to be provided to the message. This is done using the addRecepient() method. It takes recipient type and address as parameters. The recipient type is one of the following-
The address is an object of Address class. So to send a message to Mr. Raj whose email-id is firstname.lastname@example.org, the code would be :
Address address= new InternetAddress(“email@example.com”,”Raj”); message.addRecipient(Message.RecipientType.TO, address);
This is the last of the core classes. It deals with transmission of messages. Like the Message and Address classes, Transport is also an abstract class. This class communicates with the server in protocol specific language for sending the message. Generally, the protocol is SMTP. The easiest way to send message is to use the send() method of the Transport class, like this:
But this is not the best of methods. The reason is that whenever the send() method is used, it creates a separate connection for each method call. The better way to send multiple messages is:
message.saveChanges(); // implicit with send() Transport transport = session.getTransport("smtp"); transport.connect(host, username, password); transport.sendMessage(message, message.getAllRecipients()); transport.close();
The trick is to get a specific object of Transport for the current session and connect to the server. Then send the message using sendMessage() method and close the connection. What happens here is that, by calling Transport particular to a session, the connection to the server is kept alive. Hence creating a new connection for each send request is avoided.
So that’s all you need to know about the core classes. Using these classes, a simple to complex emailing environment could be created with ease. But the goal of this discussion is to use the Mail APIs to transfer huge amounts of data. To actually understand the concept of using the Mail API in that way, I will be developing a class that will encapsulate the logic of using the API as data carriers. And that brings us to the next section.