16 Programming Jakarta Mail with WebLogic Server

Learn how to program Jakarta Mail with WebLogic Server to add email capabilities to your WebLogic Server applications.

This chapter includes the following sections:

Overview of Using Jakarta Mail with WebLogic Server Applications

WebLogic Server includes the Jakarta Mail API version 1.5 reference implementation. Using the Jakarta Mail API, you can add email capabilities to your WebLogic Server applications. Jakarta Mail provides access from Java applications to Internet Message Access Protocol (IMAP)- and Simple Mail Transfer Protocol (SMTP)-capable mail servers on your network or the Internet. It does not provide mail server functionality; you must have access to a mail server to use Jakarta Mail.

Documentation for using the Jakarta Mail API is available at https://jakarta.ee/specifications/mail/1.6/. This section describes how you can use Jakarta Mail in the WebLogic Server environment.

The weblogic.jar file contains the following Jakarta Mail API packages:

  • javax.mail

  • javax.mail.event

  • javax.mail.internet

  • javax.mail.search

The weblogic.jar also contains the Java Activation Framework (JAF) package, which Jakarta Mail requires.

The javax.mail package includes providers for Internet Message Access protocol (IMAP) and Simple Mail Transfer Protocol (SMTP) mail servers. There is a separate POP3 provider for Jakarta Mail, which is not included in weblogic.jar. You can download the POP3 provider at https://maven.java.net/content/repositories/releases/com/sun/mail/pop3 and add it to the WebLogic Server classpath if you want to use it.

Understanding Jakarta Mail Configuration Files

Jakarta Mail depends on configuration files that define the mail transport capabilities of the system. The weblogic.jar file contains the standard configuration files which enable IMAP and SMTP mail servers for Jakarta Mail and define the default message types Jakarta Mail can process.

Unless you want to extend Jakarta Mail to support additional transports, protocols, and message types, you do not have to modify any Jakarta Mail configuration files. If you do want to extend Jakarta Mail, see https://javaee.github.io/javamail/ThirdPartyProducts. Then add your extended Jakarta Mail package in the WebLogic Server classpath in front of weblogic.jar.

Configuring Jakarta Mail for WebLogic Server

To configure Jakarta Mail for use in WebLogic Server, you create a mail session in the WebLogic Remote Console. This allows server-side modules and applications to access Jakarta Mail services with JNDI, using session properties you preconfigure for them.

For example, by creating a mail session, you can designate the mail hosts, transport and store protocols, and the default mail user in the WebLogic Remote Console so that modules that use Jakarta Mail do not have to set these properties. Applications that are heavy email users benefit because the mail session creates a single javax.mail.Session object and makes it available via JNDI to any module that needs it.

To use the WebLogic Remote Console to create a mail session, see Configure Access to JavaMail in the Oracle WebLogic Remote Console Online Help.

You can override any properties set in the mail session in your code by creating a java.util.Properties object containing the properties you want to override. See Sending Messages with Jakarta Mail. Then, after you look up the mail session object in JNDI, call the Session.getInstance() method with your Properties object to get a customized session.

Sending Messages with Jakarta Mail

You can send a message using Jakarta Mail within a WebLogic Server module.

Here are the steps to send a message with Jakarta Mail:

  1. Import the JNDI (naming), JavaBean Activation, and Jakarta Mail packages. You will also need to import java.util.Properties:
    import java.util.*;
    import javax.activation.*;
    import javax.mail.*;
    import javax.mail.internet.*;
    import javax.naming.*;
    
  2. Look up the Mail Session in JNDI:
    InitialContext ic = new InitialContext();
    Session session = (Session) ic.lookup("myMailSession");
    
  3. If you need to override the properties you set for the Session in the WebLogic Remote Console, create a java.util.Properties object and add the properties you want to override. Then call getInstance() to get a new Session object with the new properties.
    Properties props = new Properties();
    props.put("mail.transport.protocol", "smtp");
    props.put("mail.smtp.host", "mailhost");
    // use mail address from HTML form for from address
    props.put("mail.from", emailAddress);
    Session session2 = session.getInstance(props);
    
  4. Construct a MimeMessage. In the following example, to, subject, and messageTxt are String variables containing input from the user.
    Message msg = new MimeMessage(session2);
    msg.setFrom();
    msg.setRecipients(Message.RecipientType.TO, 
                      InternetAddress.parse(to, false));
    msg.setSubject(subject);
    msg.setSentDate(new Date());
    // Content is stored in a MIME multi-part message
    // with one body part
    MimeBodyPart mbp = new MimeBodyPart();
    mbp.setText(messageTxt);
    Multipart mp = new MimeMultipart();
    mp.addBodyPart(mbp);
    msg.setContent(mp);
    
  5. Send the message.
    Transport.send(msg);
    

The JNDI lookup can throw a NamingException on failure. Jakarta Mail can throw a MessagingException if there are problems locating transport classes or if communications with the mail host fails. Be sure to put your code in a try block and catch these exceptions.

Reading Messages with Jakarta Mail

The Jakarta Mail API provides several options for reading messages, such as reading a specified message number or range of message numbers, or pre-fetching specific parts of messages into the folder's cache.

The Jakarta Mail API allows you to connect to a message store, which could be an IMAP server or POP3 server. Messages are stored in folders. With IMAP, message folders are stored on the mail server, including folders that contain incoming messages and folders that contain archived messages. With POP3, the server provides a folder that stores messages as they arrive. When a client connects to a POP3 server, it retrieves the messages and transfers them to a message store on the client.

Folders are hierarchical structures, similar to disk directories. A folder can contain messages or other folders. The default folder is at the top of the structure. The special folder name INBOX refers to the primary folder for the user, and is within the default folder. To read incoming mail, you get the default folder from the store, and then get the INBOX folder from the default folder.

The API provides several options for reading messages. See the Jakarta Mail API for more information.

Here are steps to read incoming messages on a POP3 server from within a WebLogic Server module:

  1. Import the JNDI (naming), JavaBean Activation, and Jakarta Mail packages. You will also need to import java.util.Properties:
    import java.util.*;
    import javax.activation.*;
    import javax.mail.*;
    import javax.mail.internet.*;
    import javax.naming.*;
    
  2. Look up the Mail Session in JNDI:
    InitialContext ic = new InitialContext();
    Session session = (Session) ic.lookup("myMailSession");
    
  3. If you need to override the properties you set for the Session in the WebLogic Remote Console, create a Properties object and add the properties you want to override. Then call getInstance() to get a new Session object with the new properties:
    Properties props = new Properties();
    props.put("mail.store.protocol", "pop3");
    props.put("mail.pop3.host", "mailhost");
    Session session2 = session.getInstance(props);
    
  4. Get a Store object from the Session and call its connect() method to connect to the mail server. To authenticate the connection, you need to supply the mailhost, user name, and password in the connect method:
    Store store = session.getStore();
    store.connect(mailhost, username, password);
    
  5. Get the default folder, then use it to get the INBOX folder:
    Folder folder = store.getDefaultFolder();
    folder = folder.getFolder("INBOX");
    
  6. Read the messages in the folder into an array of Messages:
    Message[] messages = folder.getMessages();
    
  7. Operate on messages in the Message array. The Message class has methods that allow you to access the different parts of a message, including headers, flags, and message contents.

Reading messages from an IMAP server is similar to reading messages from a POP3 server. With IMAP, however, the Jakarta Mail API provides methods to create and manipulate folders and transfer messages between them. If you use an IMAP server, you can implement a full-featured, Web-based mail client with much less code than if you use a POP3 server. With POP3, you must provide code to manage a message store via WebLogic Server, possibly using a database or file system to represent folders.