JavaScript is required to for searching.
Skip Navigation Links
Exit Print View
Oracle GlassFish Server 3.1 Application Development Guide
search filter icon
search icon

Document Information

Preface

Part I Development Tasks and Tools

1.  Setting Up a Development Environment

2.  Class Loaders

3.  Debugging Applications

Part II Developing Applications and Application Components

4.  Securing Applications

5.  Developing Web Services

6.  Using the Java Persistence API

7.  Developing Web Applications

8.  Using Enterprise JavaBeans Technology

9.  Using Container-Managed Persistence

10.  Developing Java Clients

11.  Developing Connectors

12.  Developing Lifecycle Listeners

13.  Developing OSGi-enabled Java EE Applications

Part III Using Services and APIs

14.  Using the JDBC API for Database Access

15.  Using the Transaction Service

16.  Using the Java Naming and Directory Interface

17.  Using the Java Message Service

18.  Using the JavaMail API

Introducing JavaMail

Creating a JavaMail Session

JavaMail Session Properties

Looking Up a JavaMail Session

Sending and Reading Messages Using JavaMail

To Send a Message Using JavaMail

To Read a Message Using JavaMail

Using Application-Scoped JavaMail Resources

Index

Sending and Reading Messages Using JavaMail

The following topics are addressed here:

To Send 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 = (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", "user2@mailserver.com");
  4. Create a MimeMessage.

    The msgRecipient, msgSubject, and msgTxt variables in the following example contain input from the user:

    Message msg = new MimeMessage(session);
    msg.setSubject(msgSubject);
    msg.setSentDate(new Date());
    msg.setFrom();
    msg.setRecipients(Message.RecipientType.TO, 
       InternetAddress.parse(msgRecipient, false));
    msg.setText(msgTxt);
  5. Send the message.
    Transport.send(msg);

To 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", "user2@mailserver.com");
  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();