| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.kuali.rice.kew.mail.service.impl; |
| 18 | |
|
| 19 | |
import java.util.Properties; |
| 20 | |
|
| 21 | |
import org.apache.log4j.Logger; |
| 22 | |
import org.kuali.rice.core.config.ConfigContext; |
| 23 | |
import org.kuali.rice.kew.exception.WorkflowRuntimeException; |
| 24 | |
import org.kuali.rice.kew.mail.EmailBody; |
| 25 | |
import org.kuali.rice.kew.mail.EmailFrom; |
| 26 | |
import org.kuali.rice.kew.mail.EmailSubject; |
| 27 | |
import org.kuali.rice.kew.mail.EmailTo; |
| 28 | |
import org.kuali.rice.kew.mail.Mailer; |
| 29 | |
import org.kuali.rice.kew.mail.service.EmailService; |
| 30 | |
import org.springframework.beans.factory.InitializingBean; |
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
import org.kuali.rice.kew.mail.service.impl.EmailBcList; |
| 35 | |
import org.kuali.rice.kew.mail.service.impl.EmailCcList; |
| 36 | |
import org.kuali.rice.kew.mail.service.impl.EmailToList; |
| 37 | |
|
| 38 | |
|
| 39 | 0 | public class DefaultEmailService implements EmailService, InitializingBean { |
| 40 | 0 | private static final Logger LOG = Logger.getLogger(DefaultEmailService.class); |
| 41 | |
|
| 42 | |
public static final String USERNAME_PROPERTY = "mail.smtp.username"; |
| 43 | |
public static final String PASSWORD_PROPERTY = "mail.smtp.password"; |
| 44 | |
|
| 45 | |
protected Mailer mailer; |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
public void afterPropertiesSet() throws Exception { |
| 51 | 0 | if(getMailer() == null){ |
| 52 | 0 | mailer = createMailer(); |
| 53 | |
} |
| 54 | 0 | } |
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
private Properties getConfigProperties() { |
| 62 | 0 | return ConfigContext.getCurrentContextConfig().getProperties(); |
| 63 | |
} |
| 64 | |
|
| 65 | |
public void sendEmail(EmailFrom from, EmailTo to, EmailSubject subject, EmailBody body, boolean htmlMessage) { |
| 66 | 0 | if (to.getToAddress() == null) { |
| 67 | 0 | LOG.warn("No To address specified; refraining from sending mail"); |
| 68 | 0 | return; |
| 69 | |
} |
| 70 | |
try { |
| 71 | 0 | getMailer().sendMessage( |
| 72 | |
from.getFromAddress(), |
| 73 | |
to.getToAddress(), |
| 74 | |
subject.getSubject(), |
| 75 | |
body.getBody(), |
| 76 | |
htmlMessage); |
| 77 | 0 | } catch (Exception e) { |
| 78 | 0 | throw new WorkflowRuntimeException(e); |
| 79 | 0 | } |
| 80 | 0 | } |
| 81 | |
|
| 82 | |
|
| 83 | |
public void sendEmail(EmailFrom from, EmailToList to, EmailSubject subject, EmailBody body, EmailCcList cc, EmailBcList bc, boolean htmlMessage) { |
| 84 | 0 | if(getMailer() == null){ |
| 85 | |
try { |
| 86 | 0 | mailer = createMailer(); |
| 87 | 0 | } catch (Exception e) { |
| 88 | 0 | LOG.error("Error initializing mailer for multi-recipient email.", e); |
| 89 | 0 | } |
| 90 | |
} |
| 91 | 0 | if (to.getToAddresses().isEmpty()) { |
| 92 | 0 | LOG.error("List of To addresses must contain at least one entry."); |
| 93 | |
} else { |
| 94 | |
try { |
| 95 | 0 | getMailer().sendMessage( |
| 96 | |
from.getFromAddress(), |
| 97 | |
to.getToAddressesAsAddressArray(), |
| 98 | |
subject.getSubject(), |
| 99 | |
body.getBody(), |
| 100 | |
(cc == null ? null : cc.getToAddressesAsAddressArray()), |
| 101 | |
(bc == null ? null : bc.getToAddressesAsAddressArray()), |
| 102 | |
htmlMessage); |
| 103 | 0 | } catch (Exception e) { |
| 104 | 0 | LOG.error("Error sending email to multiple recipients.", e); |
| 105 | 0 | } |
| 106 | |
} |
| 107 | 0 | } |
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
public Mailer getMailer() { |
| 113 | 0 | return mailer; |
| 114 | |
} |
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
public void setMailer(Mailer mailer) { |
| 123 | 0 | this.mailer = mailer; |
| 124 | 0 | } |
| 125 | |
|
| 126 | |
protected Mailer createMailer(){ |
| 127 | 0 | Mailer cMailer = null; |
| 128 | 0 | String username = ConfigContext.getCurrentContextConfig().getProperty(USERNAME_PROPERTY); |
| 129 | 0 | String password = ConfigContext.getCurrentContextConfig().getProperty(PASSWORD_PROPERTY); |
| 130 | 0 | if (username != null && password != null) { |
| 131 | 0 | cMailer = new Mailer(getConfigProperties(), username, password); |
| 132 | 0 | LOG.info("Rice Mailer being used. Username and Pass were found"); |
| 133 | |
} else { |
| 134 | 0 | cMailer = new Mailer(getConfigProperties()); |
| 135 | 0 | LOG.info("Rice Mailer being used. Username and Pass were not found"); |
| 136 | |
} |
| 137 | 0 | return cMailer; |
| 138 | |
} |
| 139 | |
|
| 140 | |
} |