001 package com.technoetic.xplanner.mail; 002 003 import com.technoetic.xplanner.XPlannerProperties; 004 import com.technoetic.xplanner.db.hibernate.ThreadSession; 005 import com.technoetic.xplanner.domain.Person; 006 import net.sf.hibernate.HibernateException; 007 import org.apache.commons.lang.StringUtils; 008 import org.apache.log4j.Logger; 009 010 import javax.activation.DataHandler; 011 import javax.activation.FileDataSource; 012 import javax.mail.Message; 013 import javax.mail.MessagingException; 014 import javax.mail.Session; 015 import javax.mail.Transport; 016 import javax.mail.internet.AddressException; 017 import javax.mail.internet.InternetAddress; 018 import javax.mail.internet.MimeBodyPart; 019 import javax.mail.internet.MimeMessage; 020 import javax.mail.internet.MimeMultipart; 021 import java.io.File; 022 import java.io.PrintWriter; 023 import java.io.StringWriter; 024 import java.util.ArrayList; 025 import java.util.Date; 026 import java.util.Iterator; 027 import java.util.Properties; 028 029 public class EmailMessageImpl implements EmailMessage { 030 protected Logger log = Logger.getLogger(getClass()); 031 private Message msg; 032 private StringWriter bodyWriter; 033 private ArrayList attachments = new ArrayList(); 034 035 /*package scope*/ 036 EmailMessageImpl() throws MessagingException { 037 Properties transportProperties = new Properties(); 038 transportProperties.put("mail.smtp.host", new XPlannerProperties().get().get("xplanner.mail.smtp.host")); 039 Session session = Session.getDefaultInstance(transportProperties, null); 040 session.setDebug(log.isDebugEnabled()); 041 msg = new MimeMessage(session); 042 msg.setSentDate(new Date()); 043 } 044 045 public void setFrom(String from) throws AddressException, MessagingException { 046 msg.setFrom(new InternetAddress(from)); 047 } 048 049 public void setRecipient(int personId) throws MessagingException { 050 try { 051 Person person = getPerson(personId); Rate052 if (StringUtils.isEmpty(person.getEmail())) { 053 throw new MessagingException("no email address for user: uid=" + person.getUserId() + ",id=" + person.getId()); 054 } 055 setRecipients(person.getEmail()); 056 } catch (MessagingException e) { 057 throw e; 058 } catch (Exception e) { 059 throw new MessagingException("error setting recipient", e); 060 } 061 } 062 063 private Person getPerson(int personId) throws HibernateException { 064 return (Person)ThreadSession.get().load(Person.class, new Integer(personId)); 065 } 066 067 public void setRecipients(String recipients) throws AddressException, MessagingException { 068 if (StringUtils.isNotEmpty(recipients)) { 069 setRecipients(Message.RecipientType.TO, recipients.split(",")); 070 } 071 } 072 073 public void setRecipients(String[] recipients) throws AddressException, MessagingException { 074 InternetAddress[] addresses = new InternetAddress[recipients.length]; 075 for (int i = 0; i < recipients.length; i++) { 076 addresses[i] = new InternetAddress(recipients[i]); 077 } 078 msg.setRecipients(Message.RecipientType.TO, addresses); 079 } 080 081 public void setCcRecipients(String recipients) throws MessagingException, AddressException { 082 if (StringUtils.isNotEmpty(recipients)) { 083 setRecipients(Message.RecipientType.CC, recipients.split(",")); 084 } 085 } 086 087 private void setRecipients(Message.RecipientType recipientType, String[] recipients) throws AddressException, MessagingException { 088 if (recipients.length > 0) { 089 InternetAddress[] addresses = new InternetAddress[recipients.length]; 090 for (int i = 0; i < recipients.length; i++) { 091 addresses[i] = new InternetAddress(recipients[i]); 092 } 093 msg.setRecipients(recipientType, addresses); 094 } 095 } 096 097 public void setBody(String body) throws MessagingException { 098 bodyWriter = new StringWriter(); 099 bodyWriter.write(body); 100 } 101 102 public PrintWriter getBodyWriter() { 103 bodyWriter = new StringWriter(); 104 return new PrintWriter(bodyWriter); 105 } 106 107 public void setSubject(String subject) throws MessagingException { 108 msg.setSubject(subject); 109 } 110 111 public void setSentDate(Date sentDate) throws MessagingException { 112 msg.setSentDate(sentDate); 113 } 114 115 public void addAttachment(String filename) throws MessagingException { 116 File file = new File(filename); 117 addAttachment(file.getName(), file); 118 } 119 120 public void addAttachment(String filename, File file) throws MessagingException { 121 MimeBodyPart part = new MimeBodyPart(); 122 FileDataSource fds = new FileDataSource(file); 123 part.setDataHandler(new DataHandler(fds)); 124 part.setFileName(filename); 125 attachments.add(part); 126 } 127 128 public void send() throws MessagingException { 129 MimeMultipart parts = new MimeMultipart(); 130 if (bodyWriter == null) { 131 setBody(""); 132 } 133 MimeBodyPart bodyPart = new MimeBodyPart(); 134 bodyPart.setText(bodyWriter.toString()); 135 parts.addBodyPart(bodyPart); 136 Iterator iter = attachments.iterator(); 137 while (iter.hasNext()) { 138 parts.addBodyPart((MimeBodyPart)iter.next()); 139 } 140 msg.setContent(parts); 141 Transport.send(msg); 142 } 143 }