Sun Java System Application Server Platform Edition 8.2 Developer's Guide

ProcedureTo read a message using JavaMail

  1. Import the packages that you need.

    import java.util.*;
    import javax.activation.*;
    import javax.mail.*;
    import javax.mail.internet.*;
    import javax.naming.*;
  2. Look up the JavaMail session.

    InitialContext ic = new InitialContext();
    String snName = "java:comp/env/mail/MyMailSession";
    Session session = (javax.mail.Session)ic.lookup(snName);

    For more information, see Looking Up a JavaMail Session.

  3. Override the JavaMail session properties if necessary.

    For example:

    Properties props = session.getProperties();
    props.put("mail.from", "");
  4. Get a Store object from the Session, then connect to the mail server using the Store object’s connect() method.

    You must supply a mail server name, a mail user name, and a password.

    Store store = session.getStore();
    store.connect("MailServer", "MailUser", "secret");
  5. Get the INBOX folder.

    Folder folder = store.getFolder("INBOX");
  6. It is efficient to read the Message objects (which represent messages on the server) into an array.

    Message[] messages = folder.getMessages();