View Javadoc

1   /*
2    * Copyright 2005-2008 The Kuali Foundation
3    * 
4    * 
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    * 
9    * http://www.opensource.org/licenses/ecl2.php
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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  	 * This method is called by Spring on initialization.  
49  	 */
50  	public void afterPropertiesSet() throws Exception {
51  		if(getMailer() == null){
52  			mailer = createMailer();
53  		}
54  	}
55  
56  	/**
57  	 * Retrieves the Properties used to configure the Mailer.  The property names configured in the 
58  	 * Workflow configuration should match those of Java Mail.
59  	 * @return
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 	 * @return the mailer
111 	 */
112 	public Mailer getMailer() {
113 		return mailer;
114 	}
115 
116 	/**
117 	 * @param mailer the mailer to set
118 	 * 
119 	 * Note: If you want to test locally and not have a smtp server, you can, via spring,
120 	 * inject the MockMailer. That class just prints the email via Log.info
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 }