1 23 24 package org.infoglue.cms.util.mail; 25 26 import java.util.ArrayList ; 27 import java.util.List ; 28 29 import javax.activation.DataHandler ; 30 import javax.mail.Address ; 31 import javax.mail.Message ; 32 import javax.mail.MessagingException ; 33 import javax.mail.Session ; 34 import javax.mail.Transport ; 35 import javax.mail.internet.AddressException ; 36 import javax.mail.internet.InternetAddress ; 37 import javax.mail.internet.MimeMessage ; 38 39 import org.apache.commons.mail.HtmlEmail; 40 import org.apache.commons.mail.SimpleEmail; 41 import org.infoglue.cms.exception.Bug; 42 import org.infoglue.cms.exception.SystemException; 43 import org.infoglue.cms.util.CmsPropertyHandler; 44 45 public class MailService 46 { 47 48 private Session session; 50 51 52 55 56 public MailService(Session session) 57 { 58 this.session = session; 59 } 60 61 64 public MimeMessage createMessage() 65 { 66 return new MimeMessage (this.session); 67 } 68 69 72 public void send(final Message message) throws SystemException 73 { 74 try 75 { 76 Transport.send(message); 77 } 78 catch(MessagingException e) 79 { 80 e.printStackTrace(); 81 throw new SystemException("Unable to send message.", e); 82 } 83 84 } 85 86 94 public void send(String from, String to, String bcc, String subject, String content) throws SystemException 95 { 96 send(createMessage(from, to, bcc, subject, content)); 97 } 98 99 107 public void send(String from, String to, String subject, String content, String contentType, String encoding) throws SystemException 108 { 109 final Message message = createMessage(from, to, null, subject, content, contentType, encoding); 110 111 try 112 { 113 Transport.send(message); 114 } 115 catch(MessagingException e) 116 { 117 e.printStackTrace(); 118 throw new SystemException("Unable to send message.", e); 119 } 120 121 } 122 123 124 132 public void sendEmail(String from, String to, String bcc, String subject, String content, String encoding) throws SystemException 133 { 134 String contentType = CmsPropertyHandler.getMailContentType(); 135 if(contentType == null || contentType.length() == 0) 136 contentType = "text/html"; 137 138 if(contentType.equalsIgnoreCase("text/html")) 139 sendHTML(from, to, bcc, subject, content, encoding); 140 else 141 sendPlain(from, to, bcc, subject, content, encoding); 142 } 143 144 152 public void sendHTML(String from, String to, String bcc, String subject, String content, String encoding) throws SystemException 153 { 154 try 155 { 156 HtmlEmail email = new HtmlEmail(); 157 String mailServer = CmsPropertyHandler.getMailSmtpHost(); 158 String systemEmailSender = CmsPropertyHandler.getSystemEmailSender(); 159 160 email.setHostName(mailServer); 161 162 boolean needsAuthentication = false; 163 try 164 { 165 needsAuthentication = new Boolean (CmsPropertyHandler.getMailSmtpAuth()).booleanValue(); 166 } 167 catch (Exception ex) 168 { 169 needsAuthentication = false; 170 } 171 172 if (needsAuthentication) 173 { 174 final String userName = CmsPropertyHandler.getMailSmtpUser(); 175 final String password = CmsPropertyHandler.getMailSmtpPassword(); 176 177 email.setAuthentication(userName, password); 178 } 179 180 email.setBounceAddress(systemEmailSender); 181 email.setCharset(encoding); 182 183 email.addTo(to, to); 184 email.setFrom(from, from); 185 if(bcc != null) 186 email.setBcc(createInternetAddressesList(bcc)); 187 email.setSubject(subject); 188 189 email.setHtmlMsg(content); 190 191 email.setTextMsg("Your email client does not support HTML messages"); 192 193 email.send(); 194 } 195 catch (Exception e) 196 { 197 e.printStackTrace(); 198 throw new SystemException("An error occurred when we tried to send this mail:" + e.getMessage(), e); 199 } 200 201 214 } 215 216 217 225 public void sendPlain(String from, String to, String bcc, String subject, String content, String encoding) throws SystemException 226 { 227 try 228 { 229 SimpleEmail email = new SimpleEmail(); 230 String mailServer = CmsPropertyHandler.getMailSmtpHost(); 231 String systemEmailSender = CmsPropertyHandler.getSystemEmailSender(); 232 233 email.setHostName(mailServer); 234 235 boolean needsAuthentication = false; 236 try 237 { 238 needsAuthentication = new Boolean (CmsPropertyHandler.getMailSmtpAuth()).booleanValue(); 239 } 240 catch (Exception ex) 241 { 242 needsAuthentication = false; 243 } 244 245 if (needsAuthentication) 246 { 247 final String userName = CmsPropertyHandler.getMailSmtpUser(); 248 final String password = CmsPropertyHandler.getMailSmtpPassword(); 249 250 email.setAuthentication(userName, password); 251 } 252 253 email.setBounceAddress(systemEmailSender); 254 email.setCharset(encoding); 255 256 email.addTo(to, to); 257 email.setFrom(from, from); 258 email.setBcc(createInternetAddressesList(bcc)); 259 email.setSubject(subject); 260 email.setMsg(content); 261 262 email.send(); 263 } 264 catch (Exception e) 265 { 266 e.printStackTrace(); 267 } 268 269 282 } 283 284 287 private Message createMessage(String from, String to, String bcc, String subject, String content) throws SystemException 288 { 289 try 290 { 291 final Message message = new MimeMessage (this.session); 292 293 message.setContent(content, "text/html"); 294 message.setFrom(createInternetAddress(from)); 295 message.setRecipient(Message.RecipientType.TO, createInternetAddress(to)); 296 if(bcc != null) 297 message.setRecipients(Message.RecipientType.BCC, createInternetAddresses(bcc)); 298 message.setSubject(subject); 299 message.setText(content); 300 message.setDataHandler(new DataHandler (new StringDataSource(content, "text/html"))); 301 302 return message; 303 } 304 catch(MessagingException e) 305 { 306 throw new Bug("Unable to create the message.", e); 307 } 308 } 309 310 313 private Message createMessage(String from, String to, String bcc, String subject, String content, String contentType, String encoding) throws SystemException 314 { 315 try 316 { 317 final Message message = new MimeMessage (this.session); 318 String contentTypeWithEncoding = contentType+";charset="+encoding; 319 320 message.setFrom(createInternetAddress(from)); 322 message.setRecipient(Message.RecipientType.TO, createInternetAddress(to)); 323 if(bcc != null) 324 message.setRecipients(Message.RecipientType.BCC, createInternetAddresses(bcc)); 325 327 ((MimeMessage )message).setSubject(subject, encoding); 328 message.setDataHandler(new DataHandler (new StringDataSource(content, contentTypeWithEncoding, encoding))); 330 333 return message; 334 } 335 catch(MessagingException e) 336 { 337 throw new Bug("Unable to create the message.", e); 338 } 339 } 340 341 344 private Address createInternetAddress(String address) 345 { 346 try 347 { 348 return new InternetAddress (address); 349 } 350 catch(AddressException e) 351 { 352 throw new Bug("Badly formatted email address [" + address + "].", e); 353 } 354 } 355 356 359 private Address [] createInternetAddresses(String emailAddressString) throws SystemException 360 { 361 String [] emailAddresses = emailAddressString.split(";"); 362 363 Address [] addresses = new Address [emailAddresses.length]; 364 for(int i=0; i<emailAddresses.length; i++) 365 { 366 String email = emailAddresses[i]; 367 try 368 { 369 addresses[i] = new InternetAddress (email); 370 } 371 catch(AddressException e) 372 { 373 throw new SystemException("Badly formatted email address [" + email + "].", e); 374 } 375 } 376 377 return addresses; 378 } 379 380 383 private List createInternetAddressesList(String emailAddressString) throws SystemException 384 { 385 String [] emailAddresses = emailAddressString.split(";"); 386 387 List addresses = new ArrayList (); 388 for(int i=0; i<emailAddresses.length; i++) 389 { 390 String email = emailAddresses[i]; 391 try 392 { 393 addresses.add(new InternetAddress (email)); 394 } 395 catch(AddressException e) 396 { 397 throw new SystemException("Badly formatted email address [" + email + "].", e); 398 } 399 } 400 401 return addresses; 402 } 403 404 } | Popular Tags |