1 31 32 package org.opencms.mail; 33 34 import org.opencms.main.CmsLog; 35 import org.opencms.main.CmsRuntimeException; 36 import org.opencms.main.OpenCms; 37 import org.opencms.util.CmsStringUtil; 38 39 import javax.mail.AuthenticationFailedException ; 40 import javax.mail.MessagingException ; 41 import javax.mail.SendFailedException ; 42 43 import org.apache.commons.logging.Log; 44 import org.apache.commons.mail.SimpleEmail; 45 46 59 public class CmsSimpleMail extends SimpleEmail { 60 61 62 private static final Log LOG = CmsLog.getLog(CmsSimpleMail.class); 63 64 71 public CmsSimpleMail() { 72 73 super(); 75 CmsMailHost host = OpenCms.getSystemInfo().getMailSettings().getDefaultMailHost(); 77 setHostName(host.getHostname()); 78 79 String userName = host.getUsername(); 81 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(userName)) { 82 setAuthentication(userName, host.getPassword()); 84 } 85 try { 86 setFrom(OpenCms.getSystemInfo().getMailSettings().getMailFromDefault()); 88 } catch (MessagingException e) { 89 LOG.error(Messages.get().getBundle().key(Messages.LOG_INVALID_SENDER_ADDRESS_0), e); 91 } 92 } 93 94 111 public void send() throws MessagingException , SendFailedException { 112 113 try { 114 super.send(); 115 } catch (SendFailedException sf) { 116 MessagingException me = (MessagingException )sf.getNextException(); 118 CmsMailHost host = OpenCms.getSystemInfo().getMailSettings().getDefaultMailHost(); 119 if (me instanceof AuthenticationFailedException ) { 120 CmsRuntimeException rte = new CmsRuntimeException(Messages.get().container( 122 Messages.ERR_SEND_EMAIL_AUTHENTICATE_2, 123 host.getUsername(), 124 host.getHostname())); 125 sf.initCause(rte); 126 throw sf; 127 } 128 CmsRuntimeException rte = new CmsRuntimeException(Messages.get().container( 130 Messages.ERR_SEND_EMAIL_HOSTNAME_1, 131 host.getHostname()), me); 132 sf.initCause(rte); 133 throw sf; 134 135 } 136 } 137 } 138
| Popular Tags
|