01: /*
02: * Copyright 2005-2007 The Kuali Foundation.
03: *
04: *
05: * Licensed under the Educational Community License, Version 1.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * http://www.opensource.org/licenses/ecl1.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 edu.iu.uis.eden.mail;
18:
19: import java.util.Properties;
20:
21: import org.apache.log4j.Logger;
22: import org.kuali.rice.core.Core;
23: import org.springframework.beans.factory.InitializingBean;
24:
25: import edu.iu.uis.eden.exception.WorkflowRuntimeException;
26:
27: public class DefaultEmailService implements EmailService,
28: InitializingBean {
29: private static final Logger LOG = Logger
30: .getLogger(DefaultEmailService.class);
31:
32: public static final String USERNAME_PROPERTY = "mail.smtp.username";
33: public static final String PASSWORD_PROPERTY = "mail.smtp.password";
34:
35: private Mailer mailer;
36:
37: public void afterPropertiesSet() throws Exception {
38: String username = Core.getCurrentContextConfig().getProperty(
39: USERNAME_PROPERTY);
40: String password = Core.getCurrentContextConfig().getProperty(
41: PASSWORD_PROPERTY);
42: if (username != null && password != null) {
43: mailer = new Mailer(getConfigProperties(), username,
44: password);
45: } else {
46: mailer = new Mailer(getConfigProperties());
47: }
48: }
49:
50: /**
51: * Retrieves the Properties used to configure the Mailer. The property names configured in the
52: * Workflow configuration should match those of Java Mail.
53: * @return
54: */
55: private Properties getConfigProperties() {
56: return Core.getCurrentContextConfig().getProperties();
57: }
58:
59: public void sendEmail(EmailFrom from, EmailTo to,
60: EmailSubject subject, EmailBody body, boolean htmlMessage) {
61: if (to.getToAddress() == null) {
62: LOG
63: .warn("No To address specified; refraining from sending mail");
64: return;
65: }
66: try {
67: mailer.sendMessage(from.getFromAddress(),
68: to.getToAddress(), subject.getSubject(), body
69: .getBody(), htmlMessage);
70: } catch (Exception e) {
71: throw new WorkflowRuntimeException(e);
72: }
73: }
74:
75: }
|