After you have configured e-mail services on your iSeries server and created a JavaMail Session object with the WebSphere administrative console, you can write applications that interface with these services through the Session object. According to the J2EE specification, each instance of the javax.mail.Session class is treated as a JNDI resource factory. To use the JavaMail APIs in an enterprise application component, such as a servlet, perform these steps:
Use the WebSphere Development Studio Client to declare mail resource references in your application. For more information, see the WebSphere Development Studio Client Help.
Configure each JavaMail session object used by your Web component. This is done during the process of deploying the component.
Use a JNDI lookup to get a reference to a Session object.
See Example: Look up a JavaMail session for more information.
Example: JavaMail code
The following code illustrates how an application component sends a message and saves it to the mail account's Sent folder:
... // obtain JNDI initial context javax.naming.InitialContext ctx = new javax.naming.InitialContext(); // use JNDI lookup to obtain Session mail_session = (javax.mail.Session) ctx.lookup ("java:comp/env/mail/MailSession"); // create new mail message object using Session MimeMessage msg = new MimeMessage(mail_session); // set message properties msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse ("somebody@some_domain.net")); msg.setFrom(new InternetAddress("me@my_company.com")); msg.setSubject("Important message from me"); String msg_text = new String("Hello!"); msg.setText(msg_text); // get Session object's store Store store = mail_session.getStore(); // connect to store store.connect(); // obtain reference to "Sent" folder Folder f = store.getFolder("Sent"); // create "Sent" folder if it does not exist if (!f.exists()) f.create(Folder.HOLDS_MESSAGES); // add message to "Sent" folder f.appendMessages(new Message[] {msg});
Note: The preceding example uses the IMAP protocol for receiving e-mail messages. On the iSeries platform, it is supported by the Domino e-mail server, but not the TCP/IP Connectivity Utilities e-mail services, which only provides access to received messages through the POP3 protocol.