SunQuest
 
       Java
  Home arrow Java arrow Page 2 - Java Mail API: Transforming Mail into Data...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Actuate Whitepapers 
Moblin 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Java Mail API: Transforming Mail into Data Carriers
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2005-11-22

    Table of Contents:
  • Java Mail API: Transforming Mail into Data Carriers
  • Java Mail Core Classes Continued
  • Mails As Data Carriers: A Practical Approach
  • Sending
  • Receiving

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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:

    1. Message
    2. Address
    3. Transport

    Without much ado, here are the details.

    Message

    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:

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

    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.

    Address

    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(“norrinrad@hotmail.com”);

    To show the name of the user as well, pass it as the second argument:

    Address address= new InternetAddress(“norrinrad@hotmail.com”,”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,

    message.setReplyTo(address);

    sets the address of the message to “norrinrad@hotmail.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-

    Message.RecipientType.TO
    Message.RecipientType.CC
    Message.RecipientType.BCC

    The address is an object of Address class. So to send a message to Mr. Raj whose email-id is norrinrad@hotmail.com, the code would be :

    Address address= new InternetAddress(“norrinrad@hotmail.com”,”Raj”);
    message.addRecipient(Message.RecipientType.TO, address);

    Transport

    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:

    Transport.send(message);

    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.

    More Java Articles
    More By A.P.Rajshekhar


       · HiThank you for reading this article. It details about using Mail API as data...
     

    JAVA ARTICLES

    - Deploying Multiple Java Applets as One
    - Deploying Java Applets
    - Understanding Deployment Frameworks
    - Database Programming in Java Using JDBC
    - Extension Interfaces and SAX
    - Entities, Handlers and SAX
    - Advanced SAX
    - Conversions and Java Print Streams
    - Formatters and Java Print Streams
    - Java Print Streams
    - Wildcards, Arrays, and Generics in Java
    - Wildcards and Generic Methods in Java
    - Finishing the Project: Java Web Development ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway