Last night I presented my article discussing how to use JavaMail to check a POP3 mail account and retrieve its messages. Tonight I am going to follow that up with a summary of my EmailDelivery class. This is a simple class I wrote to help me easily send emails from my Java applications. A few highlights of the class are that it:
- Supports relaying through SMTP servers that require authentication.
- Has convenience methods for easily adding file attachments.
- Has a method to set the message priority (high, normal, low).
- Allows you to easily set the to, from, cc, and bcc email addresses. Also supports passing in a comma separated list into any of these methods and have it parsed automatically for easy sending to multiple recipients.
- Allows you to easily add header name/value pairs in the message.
- Suppports easily setting the value stored in the Message-ID header tag of the message.
To use my classes you will first need to download the JavaBeans Activation Framework, and the Javamail libraries. Unzip the downloaded archives and put the appropriate jar file into your classpath. My example also uses Log4j to log its output. If you don’t want to configure log4j, then replace all of my log.info and log.debug calls with a call to System.out.println.
You also need Jakarta Commons Codec library from http://jakarta.apache.org/site/downloads/downloads_commons-codec.cgi. I downloaded the file labeled 1.3.zip Binary. Uncompress the zip file and put the commons codec jar file into your classpath. The name of the jar file in my example is commons-codec-1.3.jar. The Commons Codec jar file is what supports the Base 64 encoding of the file attachments.
Next, you will need to have my EmailMimeMessage class. Take the class below which is a simple extension of the standard JavaMail MimeMessage class. This class exists for the sole purpose of supporting a user defined Message-ID header tag in my examples.
package com.tima.edelivery.util;
import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import org.apache.log4j.*;
/**
* Extended MIME Message class with method to set the message ID
* header tag, and the updateHeaders method which ensures the
* Message ID gets set in the message.
*
* @author Tim Archer 09/30/2003
* @version $Revision: 1.1 $
*/
class EmailMimeMessage extends MimeMessage {
static Logger log = Logger.getLogger(EmailMimeMessage.class);
/** The value stored in the Message-ID header tag for the message.*/
protected String messageID = "";
/**
* Create a new EmailMimeMessage object.
* @param session The javax.mail.Session object the mail message is for.
*
*/
public EmailMimeMessage(javax.mail.Session session) {
super(session);
}
/**
* Set the value stored in the Message-ID header tag for the message.
* @param p_value The value of the Message-ID header tag.
*
*/
void setMessageID(String p_value) {
messageID = p_value;
}
/**
* Calls the super.updateHeaders() method, and also ensures
* that the Message-ID header tag
* gets set if the setMessageID method was called.
*
* @throws MessagingException If an error occurs.
*/
protected void updateHeaders() throws MessagingException {
super.updateHeaders();
if (messageID != null && messageID.length() > 0) {
setHeader("Message-ID", messageID);
}
}
}
Now that you have the EmailMimeMessage class, take the code below and create the EmailDelivery class. This class has the real functionality we're after.
package com.tima.edelivery.util;
import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import org.apache.commons.codec.binary.Base64;
import org.apache.log4j.*;
/**
* Create a multipart message with the second block of the
* message being the given file for an attachment.
*
* Has support for SMTP authentication to authenticate to an
* SMTP server to send the message.
* Utilizes the apache commons codec library to Base64 encode the file attachment.
*
* @author Tim Archer 09/30/2003
* @version $Revision: 1.1 $
*/
public class EmailDelivery {
static Logger log = Logger.getLogger(EmailDelivery.class);
/** Static variable representing a high priority message. */
public final static String HIGH_PRIORITY = "1";
/** Static variable representing a normal priority message. */
public final static String NORMAL_PRIORITY = "3";
/** Static variable representing a low priority message. */
public final static String LOW_PRIORITY = "5";
EmailMimeMessage msg = null;
Session session = null;
Properties props = null;
Multipart mp = null;
/** The SMTP host used to send the message. */
String smtp_host = new String("");
/** If using SMTP authentication, then this is the username to login to the SMTP server as. */
String smtp_username = null;
/** If using SMTP authentication, then this is the password for the username to login to the SMTP server as. */
String smtp_password = null;
/** The port the SMTP server is listening on. */
int smtp_port = 25;
/** If we are to use SMTP authentication when sending the message. */
boolean usingAuthentication = false;
/**
* Create a new object to send email messages.
*
*/
public EmailDelivery() {
props = System.getProperties();
session = Session.getDefaultInstance(props, null);
session.setDebug(false);
msg = new EmailMimeMessage(session);
mp = new MimeMultipart();
}
/**
* Set the SMTP host used for sending the mail message.
*
* @param p_host The SMTP host to use for sending the messgae.
* @throws MessagingException If an error occurs.
*
*/
public void setSMTPHost(String p_host) throws MessagingException {
this.setSMTPHost(p_host, null, null);
}
/**
* Set the SMTP host used for sending the mail message.
* This method allows a username and password to be specified for SMTP authentication.
*
* @param p_host The SMTP host to use for sending the messgae.
* @param p_username The username to log into the SMTP server with.
* @param p_password The password for the username to log into the SMTP server with.
* @throws MessagingException If an error occurs.
*
*/
public void setSMTPHost(String p_host, String p_username, String p_password) throws MessagingException {
log.debug("EmailDelivery in setSMTPHost Method. Host: "+p_host+" Username: "+p_username);
smtp_host = p_host;
smtp_username = p_username;
smtp_password = p_password;
props.put("mail.smtp.host", smtp_host);
//
//Determine if we are using authentication or not
//
if ((smtp_username != null && smtp_username.length() > 0) ||
(smtp_password != null && smtp_password.length() > 0)) {
props.put("mail.smtp.auth", "true");
usingAuthentication = true;
}
else {
usingAuthentication = false;
}
log.debug("EmailDelivery in setSMTPHost Method. UsingAuthenticaton: "+usingAuthentication);
}
/**
* Set the port that the SMTP server is listening on.
*
* @param p_port The port the SMTP server is listening on.
* @throws Exception If an error occurs.
*
*/
public void setSMTPPort(int p_port) throws Exception {
smtp_port = p_port;
props.put("mail.smtp.port", String.valueOf(smtp_port));
}
/**
* Set who the email message is to.
Recent comments
16 weeks 2 days ago
18 weeks 1 day ago
18 weeks 4 days ago
18 weeks 4 days ago
22 weeks 2 days ago
24 weeks 5 days ago
25 weeks 5 days ago
26 weeks 1 day ago
30 weeks 2 days ago
50 weeks 4 days ago