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 public class DefaultEmailService implements EmailService, InitializingBean {
40 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 if(getMailer() == null){
52 mailer = createMailer();
53 }
54 }
55
56
57
58
59
60
61 private Properties getConfigProperties() {
62 return ConfigContext.getCurrentContextConfig().getProperties();
63 }
64
65 public void sendEmail(EmailFrom from, EmailTo to, EmailSubject subject, EmailBody body, boolean htmlMessage) {
66 if (to.getToAddress() == null) {
67 LOG.warn("No To address specified; refraining from sending mail");
68 return;
69 }
70 try {
71 getMailer().sendMessage(
72 from.getFromAddress(),
73 to.getToAddress(),
74 subject.getSubject(),
75 body.getBody(),
76 htmlMessage);
77 } catch (Exception e) {
78 throw new WorkflowRuntimeException(e);
79 }
80 }
81
82
83 public void sendEmail(EmailFrom from, EmailToList to, EmailSubject subject, EmailBody body, EmailCcList cc, EmailBcList bc, boolean htmlMessage) {
84 if(getMailer() == null){
85 try {
86 mailer = createMailer();
87 } catch (Exception e) {
88 LOG.error("Error initializing mailer for multi-recipient email.", e);
89 }
90 }
91 if (to.getToAddresses().isEmpty()) {
92 LOG.error("List of To addresses must contain at least one entry.");
93 } else {
94 try {
95 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 } catch (Exception e) {
104 LOG.error("Error sending email to multiple recipients.", e);
105 }
106 }
107 }
108
109
110
111
112 public Mailer getMailer() {
113 return mailer;
114 }
115
116
117
118
119
120
121
122 public void setMailer(Mailer mailer) {
123 this.mailer = mailer;
124 }
125
126 protected Mailer createMailer(){
127 Mailer cMailer = null;
128 String username = ConfigContext.getCurrentContextConfig().getProperty(USERNAME_PROPERTY);
129 String password = ConfigContext.getCurrentContextConfig().getProperty(PASSWORD_PROPERTY);
130 if (username != null && password != null) {
131 cMailer = new Mailer(getConfigProperties(), username, password);
132 LOG.info("Rice Mailer being used. Username and Pass were found");
133 } else {
134 cMailer = new Mailer(getConfigProperties());
135 LOG.info("Rice Mailer being used. Username and Pass were not found");
136 }
137 return cMailer;
138 }
139
140 }