Thursday, July 15, 2010

Useful Java Classes and Interfaces For Mailing


  • A Session object authenticates the user and controls access to the mail servers. The Session class is a final concrete class. It cannot be subclassed. By calling the appropriate factory method on a Session object, you can obtain Transport and Store objects which support specific protocols to send or fetch e-mail respectively.
  • You convert e-mail address strings such as "user@domain.com" into an InternetAddress object to use it with JavaMail. The InternetAddress class extends the abstract Address class to come up with an "rfc822" type address.
    public class InternetAddress extends Address implements Cloneable
    where:
    public abstract class Address extends Object implements Serializable
  • You construct an e-mail message as an object of the MimeMessage class.
    public class MimeMessage extends Message implements MimePart
    where
    public abstract class Message extends Object implements Part
    public interface MimePart extends Part
    public interface Part

    All Java e-mails are of Message type.

  • Each Message object has a content. The content can either be a multipart content or not.
  • A simple content is just some plain text, for example. There is no special class for simple (non-multipart) content. You just say msg.setText(textString).
  • MimeMultipart class handles multipart content which adheres to the MIME specifications.
    public class MimeMultipart extends Multipart
    where:
    public abstract class Multipart extends Object

    You create a new MimeMultipart object by invoking its default constructor.

    When you get content of an multipart e-mail, it always returns an object of this type.

  • MimeMultipart acts as a container for multiple parts to be sent in the e-mail. Each part is of type MimeBodyPart.
    public class MimeBodyPart extends BodyPart implements MimePart
    where:
    public abstract class BodyPart extends Object implements Part
    public interface Part

    MimeMultipart's getBodyPart(int index) method returns the MimeBodyPart object at the given index. Index starts at 0. The addBodyPart(...) method adds a new MimeBodyPart object to it as a step towards constructing a new multipart MimeMessage.

    Each Bodypart type object can contain either a JAF DataHandler object or another (nested) Multipart type object. So, check the content-type of each BodyPart before using it in your code.

    To extract a nested Multipart type object from a MimePart containing a ContentType attribute set to "multipart/*", use the MimePart's getContent() method. Its getSubType method returns the multipart message MIME subtype. The subtype defines the relationship among the individual body parts of a multipart message.

  • If the content of a Message object is an instance of the Multipart class, it means that you need to cast the content into a Multipart and look for BodyPart type objects within the resulting object to access each individual part.
  • Another Message object can not be contained directly in a Multipart object, it must be embedded in a BodyPart first.
  • Each Message or BodyPart type object also has some attributes.
  • A Message's attributes can be From, To, Subject, Reply-To etc. You can also add non-standard attributes as headers.
  • A BodyPart type object does not have attributes that set From, To, Subject, ReplyTo, or other address header fields.
  • Messages are stored in Folder objects which may also contain subfolders. All mail servers have the folder named "INBOX". The Folder class declares methods that fetch, append, copy and delete messages. A Folder object can also send events to components registered as event listeners.
    • "fldr.getType" returns whether a Folder can hold subfolders, messages, or both.
    • "store.getDefaultFolder" returns the root folder.
    • "fldr.list" returns all the subfolders under that folder.
    • "fldr.getFolder(folderName)" returns the named subfolder. This subfolder need not exist physically in the store.
    • "fldr.exists" indicates whether this folder exists.
    • "store.create" creates a folder
    • A closed Folder object allows deleting and renaming the folder, listing and creating subfolders, and monitoring for new messages.
    • "fldr.open" opens a Folder object (only if it can contain messages). You cannot call "open", "delete" and "renameTo" on an open folder.
  • Folders are stored in a database accessed by a Store class which requires username, and password to connect to the database. As a user, you will first connect to the mail server by calling one of the three connect(...) methods on a Store class, specifying the server to access, username and password.
  • Messages stored within a Folder object are sequentially numbered, starting at 1. Calling msg.getMessageNumber() returns its sequence number. You can reference a message either by its sequence number or by the corresponding Message object itself.

    When messages marked as deleted are actually purged, the remaining messages are renumbered. So there is no guarantee that a message will always have the same message number in a folder. Since a sequence number can change within a session, use references to Message objects rather than sequence numbers as cached references to messages.

  • Use FetchProfile to selectively retrieve message attributes/content for efficiency:
    Message[] msgs = folder.getMessages();

    FetchProfile fp = new FetchProfile();
    // ENVELOPE, CONTENT_INFO and/or FLAGS
    // ENVELOPE: From, To, Cc, Bcc, ReplyTo, Subject and Date
    // CONTENT_INFO: Type, Disposition, Description, Size and LineCount
    // FLAGS: Flags set on this message
    fp.add(FetchProfile.Item.ENVELOPE);
    // Add a specific header field to the list of attributes to be prefetched
    fp.add("X-mailer");

    folder.fetch(msgs, fp);
    for (int i = 0; i < folder.getMessageCount(); i++) {
    display(msgs[i].getFrom());
    display(msgs[i].getSubject());
    display(msgs[i].getHeader("X-mailer"));
    }

No comments:

Post a Comment