[ Team LiB ] Previous Section Next Section

JavaMail and the Global Auctions Application

The Global Auctions application makes use of JavaMail's functionality in a utility class that will send out an email to registered users notifying them when they have been outbid, or have won or lost on an item. It may also be used to send error messages to users.

The utility class makes use of the Mail Session configured in the Administration Console (defined earlier in this chapter). The class's constructor takes two parameters: a String of the recipient's email address and a String for the message.

Creating the AuctionMailer Utility Class

Now, let's get to the business of creating our utility class. In a text editor, create and save a new text file as AuctionMailer.java. First we import the classes needed:


import java.io.*;
import java.util.*;
import javax.activation.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.naming.*;

The AuctionMailer's constructor takes two String parameters representing the recipient's address and the text message:


private String to;
private String emailText;

public AuctionMailer(String to, String emailText) {
  this.to = to;
  this.emailText = emailText;
}

Our class defines one method, sendMail, to send messages. In the body of this method, we'll obtain the Mail Session object via a JNDI lookup, construct our message, and send it out. In the Global Auctions application, the Mail Session's JNDI name is MyMail. The body of the sendMail method is done within a try/catch block (shown in the complete file contents, later in the chapter):


InitialContext ctx = new InitialContext();
Session serverSession = (Session)ctx.lookup("MyMail");

Next we pass our environment variables, such as the username, password, and so forth:


Properties env = new Properties();
String host = "mycompany.com";
String username = "webMaster";
String password = "pwd";
String protocol = "smtp";
env.put("mail.store.protocol", protocol);
env.put("mail. smtp.host", host);
javax.mail.Session session = serverSession.getInstance(env);

Note that this class should be deployed on WebLogic Server and can be called by either a JSP/servlet or an EJB. It cannot be used by a standalone Java client because the Session object is not serializable.

Then we'll construct the message and add the recipient with our toAddress value:


Message message = new MimeMessage(session);
message.setSubject("Message from Global Auctions");
message.addRecipient(Message.RecipientType.TO, toAddress);

We'll define and set the sender address:


Address fromAddress = new InternetAddress("webmaster@globalauctions.com", "John Doe");
message.setFrom(fromAddress);

Here, we set the mail message:


message.setContent(emailText, "text/html");

Finally, we can send the message:


Transport.send(message);

Listing 13.6 outlines the entire contents of the AuctionMailer utility class.

Listing 13.6 AuctionMailer.java
import java.util.*;
import javax.activation.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.naming.*;

public class AuctionMailer {

  private String to;
  private String emailText;

  public AuctionMailer(String to, String emailText) {
    this.to = to;
    this.emailText = emailText;
  }

  public void sendMail() {
    try {
        Properties env = new Properties();
        String host = "mycompany.com";
        String username = "webMaster";
        String password = "pwd";
        String protocol = "smtp";
        env.put("mail.store.protocol", protocol);
        env.put("mail.smtp.host", host);
        //get passed parameters
        Address toAddress = new InternetAddress(to);

        //get the session
        InitialContext ctx = new InitialContext();
        Session serverSession = (Session) ctx.lookup("MyMail");
        javax.mail.Session session = serverSession.getInstance(env);
        //construct the message
        Message message = new MimeMessage(session);
        message.setSubject("Message from Global Auctions");
        message.addRecipient(Message.RecipientType.TO, toAddress);

        //define and set the sender address
        Address fromAddress =
          new InternetAddress("webmaster@globalauctions.com", "John Doe");
        message.setFrom(fromAddress);
        message.setContent(emailText, "text/html");

        //send the message
        Transport.send(message);
    } catch (NamingException ne) {
      ne.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
    [ Team LiB ] Previous Section Next Section