1 13 package info.magnolia.cms.util; 14 15 import java.util.Properties ; 16 17 import javax.mail.Message ; 18 import javax.mail.MessagingException ; 19 import javax.mail.Session ; 20 import javax.mail.Transport ; 21 import javax.mail.internet.AddressException ; 22 import javax.mail.internet.InternetAddress ; 23 import javax.mail.internet.MimeMessage ; 24 25 import org.apache.log4j.Logger; 26 27 28 34 public class MailHandler { 35 36 39 private static Logger log = Logger.getLogger(MailHandler.class); 40 41 private String body; 42 43 private String subject; 44 45 private String from; 46 47 private InternetAddress [] toList; 48 49 private InternetAddress [] ccList; 50 51 private Session session; 52 53 public MailHandler(String mailHost, int toListLength, int ccListLength) throws Exception { 54 toList = new InternetAddress [toListLength]; 55 ccList = new InternetAddress [ccListLength]; 56 Properties props = System.getProperties(); 57 props.put("mail.smtp.host", mailHost); 58 session = Session.getInstance(props, null); 59 } 60 61 public void sendMail() throws MessagingException { 62 try { 63 MimeMessage message = new MimeMessage (session); 64 message.setFrom(new InternetAddress (this.getFrom())); 65 message.setRecipients(Message.RecipientType.TO, this.getToList()); 66 message.setRecipients(Message.RecipientType.CC, this.getCcList()); 67 message.setSubject(subject); 68 69 message.setContent(body, "text/plain"); 70 message.setHeader("Content-Type", "text/plain; charset=UTF-8"); 71 72 Transport.send(message); 73 log.info("Mail has been sent to: [" + addressesToString(this.getToList()) + "]"); 74 } 75 catch (MessagingException e) { 76 log.error("Email to: [" 77 + addressesToString(this.getToList()) 78 + "] was not sent because of the following error: " 79 + e.getMessage(), e); 80 throw e; 81 } 82 catch (RuntimeException e) { 83 log.error("Email to: [" 86 + addressesToString(this.getToList()) 87 + "] was not sent because of the following error: " 88 + e.getMessage(), e); 89 throw e; 90 } 91 } 92 93 98 public String addressesToString(InternetAddress [] addresses) { 99 if (addresses == null) 100 return ""; 101 int length = addresses.length; 102 StringBuffer sb = new StringBuffer (); 103 for (int i = 0; i < length; i++) { 104 if (i != 0) 105 sb.append(","); 106 sb.append(addresses[i].getAddress()); 107 } 108 return sb.toString(); 109 } 110 111 public void setBody(String body) { 112 this.body = body; 113 } 114 115 public String getBody() { 116 return this.body; 117 } 118 119 public void setSubject(String subject) { 120 this.subject = subject; 121 } 122 123 public String getSubject() { 124 return this.subject; 125 } 126 127 public void setFrom(String from) { 128 this.from = from; 129 } 130 131 public String getFrom() { 132 return this.from; 133 } 134 135 public void setToList(String to) throws AddressException { 136 String [] toObj = to.split("\n"); 137 for (int i = 0; i < toObj.length; i++) { 138 this.toList[i] = new InternetAddress (toObj[i]); 139 } 140 } 141 142 public InternetAddress [] getToList() { 143 return this.toList; 144 } 145 146 public void setCcList(String to) throws AddressException { 147 String [] toObj = to.split("\n"); 148 for (int i = 0; i < toObj.length; i++) { 149 this.ccList[i] = new InternetAddress (toObj[i]); 150 } 151 } 152 153 public InternetAddress [] getCcList() { 154 return this.ccList; 155 156 } 157 } 158 | Popular Tags |