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

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.

  • Download the JavaMail API 1.3 and unpack / install it
  • Download the Java Activation Framework 1.0.2 (JAF) and unpack / install it
  • Put the following into your notes.ini (in the Notes or Domino program directory, remember that JavaUserClasses must not be the last entry in notes.ini and that the line has a maximum size of 255 characters):
    JavaUserClasses=.;JavaMail directorymail.jar;JAF directoryactivation.jar
  • Restart the client or server

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, */ 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("", ""); Session session = Session.getDefaultInstance(p, null); session.setDebug(true); try { MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress("")); InternetAddress[] address = {new InternetAddress("")}; 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(); } } }