dev.kanngard.net make sure you visit my new blog at: johankanngard.net

Short tip: Sending a SMTP mail from an agent with JavaMail

by Johan Känngård / [Java] / 2002-12-18 / #4


Sometimes, Domino's mail capabilities is not enough, or you want to have more control of the SMTP communication. I recently bumped into the JavaMail API, and it is a framework for handling mails. Sun even have a reference implementation of the mail standards like SMTP, IMAP and POP3.

The JavaMail JAR files loads the reference classes with a method that is not supported in Notes / Domino, so you have to put them in the JavaUserClasses in your notes.ini.

Try the following simple agent:

import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.SendFailedException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import lotus.domino.AgentBase;

/**
 * Tests the Java Mail API and the SMTP provider.
 *
 * @author Johan Känngård, http://dev.kanngard.net
 */
public class JavaMailTester extends AgentBase {

    /**
     * The Domino Agent Manager executes this method when starting this agent.
     */
    public void NotesMain() {

        try {
            Properties p = new Properties();
            p.put("mail.smtp.host", "my.server.com");
            
            Session session = Session.getDefaultInstance(p, null);
            session.setDebug(true);

            try {

                MimeMessage msg = new MimeMessage(session);
                msg.setFrom(new InternetAddress("john.doe@server.com"));
                InternetAddress[] address =
                        {new InternetAddress("jane.doe@server.com")};
                msg.setRecipients(Message.RecipientType.TO, address);
                msg.setSubject("JavaMail APIs Test");
                msg.setSentDate(new Date());
                msg.setText("Test of the Java Mail API");
                Transport.send(msg);
            } catch (MessagingException mex) {
                mex.printStackTrace();
                System.out.println();
                Exception ex = mex;
                
                do {
                    if (ex instanceof SendFailedException) {
                        SendFailedException sfex = (SendFailedException) ex;
                        Address[] invalid = sfex.getInvalidAddresses();
                        
                        if (invalid != null) {
                            for (int i = 0; i < invalid.length; i++) {
                                System.out.println("Invalid address: " +
                                        invalid[i]);
                            }
                        }
                        Address[] validUnsent =
                                sfex.getValidUnsentAddresses();
                        
                        if (validUnsent != null) {
                            for (int i = 0; i < validUnsent.length; i++) {
                                System.out.println("Valid unsent address: " +
                                        validUnsent[i]);
                            }
                        }
                        Address[] validSent = sfex.getValidSentAddresses();
                        if (validSent != null) {
                            for (int i = 0; i < validSent.length; i++) {
                                System.out.println("Valid sent address: " +
                                        validSent[i]);
                            }
                        }
                    }

                    if (ex instanceof MessagingException) {
                        ex = ((MessagingException) ex).getNextException();
                    } else {
                        ex = null;
                    }
                } while (ex != null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}