package org.appfuse.util;
import java.io.File;
import java.util.Properties;
import java.util.ResourceBundle;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.appfuse.Constants;
public class MailUtil extends Object {
private static Log log = LogFactory.getFactory().getInstance(MailUtil.class);
public static Session getSession() {
Session session = null;
try {
session = (Session) new InitialContext().lookup("java:comp/env/" +
Constants.JNDI_MAIL);
} catch (NamingException ex) {
if (log.isDebugEnabled()) {
log.info("error communicating with JNDI, assuming testcase");
}
ResourceBundle mailProps = ResourceBundle.getBundle("mail");
Properties props = new Properties();
props.setProperty("mail.debug", mailProps.getString("mail.debug"));
props.setProperty("mail.transport.protocol",
mailProps.getString("mail.transport.protocol"));
props.setProperty("mail.host", mailProps.getString("mail.host"));
props.setProperty("mail.user", mailProps.getString("mail.user"));
props.setProperty("mail.password",
mailProps.getString("mail.password"));
session = Session.getDefaultInstance(props, null);
}
return session;
}
public static void sendMessageWithAttachment(String from, String[] to,
String[] cc, String subject,
String content,
String mimeType,
File attachment)
throws MessagingException {
Message message = new MimeMessage(getSession());
if (!StringUtils.isEmpty(from)) {
InternetAddress sentFrom = new InternetAddress(from);
message.setFrom(sentFrom);
if (log.isDebugEnabled()) {
log.debug("e-mail from: " + sentFrom);
}
}
InternetAddress[] sendTo = new InternetAddress[to.length];
for (int i = 0; i < to.length; i++) {
sendTo[i] = new InternetAddress(to[i]);
if (log.isDebugEnabled()) {
log.debug("sending e-mail to: " + to[i]);
}
}
if ((cc.length > 0) && (cc[0] != null)) {
InternetAddress[] copyTo = new InternetAddress[cc.length];
for (int i = 0; i < cc.length; i++) {
copyTo[i] = new InternetAddress(cc[i]);
if (log.isDebugEnabled()) {
log.debug("copying e-mail to: " + cc[i]);
}
}
message.setRecipients(Message.RecipientType.CC, copyTo);
}
message.setRecipients(Message.RecipientType.TO, sendTo);
message.setSubject(subject);
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(content, mimeType);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(attachment);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(attachment.getName());
if (log.isDebugEnabled()) {
log.debug("attaching file named: " + attachment.getName());
log.debug("from location: " + attachment.getAbsolutePath());
}
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
Transport.send(message);
}
public static void sendMessage(String from, String[] to, String[] cc,
String subject, String content,
String mimeType) throws MessagingException {
Message message = new MimeMessage(getSession());
if (!StringUtils.isEmpty(from)) {
InternetAddress sentFrom = new InternetAddress(from);
message.setFrom(sentFrom);
if (log.isDebugEnabled()) {
log.debug("e-mail from: " + sentFrom);
}
}
InternetAddress[] sendTo = new InternetAddress[to.length];
for (int i = 0; i < to.length; i++) {
sendTo[i] = new InternetAddress(to[i]);
if (log.isDebugEnabled()) {
log.debug("sending e-mail to: " + to[i]);
}
}
if ((cc.length > 0) && (cc[0] != null)) {
InternetAddress[] copyTo = new InternetAddress[cc.length];
for (int i = 0; i < cc.length; i++) {
copyTo[i] = new InternetAddress(cc[i]);
if (log.isDebugEnabled()) {
log.debug("copying e-mail to: " + cc[i]);
}
}
message.setRecipients(Message.RecipientType.CC, copyTo);
}
message.setRecipients(Message.RecipientType.TO, sendTo);
message.setSubject(subject);
message.setContent(content, mimeType);
Transport.send(message);
}
public static void sendTextMessage(String from, String[] to, String[] cc,
String subject, String content)
throws MessagingException {
sendMessage(from, to, cc, subject, content, "text/plain");
}
public static void sendTextMessage(String from, String to, String cc,
String subject, String content)
throws MessagingException {
String[] recipient = { to };
String[] copy = { cc };
sendMessage(from, recipient, copy, subject, content, "text/plain");
}
public static void sendMessage(String from, String to, String cc,
String subject, String content,
String mimeType) throws MessagingException {
String[] recipient = { to };
String[] copy = { cc };
sendMessage(from, recipient, copy, subject, content, mimeType);
}
public static void sendMessage(String from, String to, String cc,
String subject, String content,
String mimeType, File attachment)
throws MessagingException {
String[] recipient = { to };
String[] copy = { cc };
sendMessageWithAttachment(from, recipient, copy, subject, content,
mimeType, attachment);
}
public static void sendHTMLMessage(String from, String[] to, String[] cc,
String subject, String content)
throws MessagingException {
sendMessage(from, to, cc, subject, content, "text/html");
}
public static void sendHTMLMessage(String from, String to, String cc,
String subject, String content)
throws MessagingException {
String[] recipient = { to };
String[] copy = { cc };
sendMessage(from, recipient, copy, subject, content, "text/html");
}
}