KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > util > SendEmail


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.workflow.util;
6
7 import com.opensymphony.module.propertyset.PropertySet;
8
9 import com.opensymphony.workflow.FunctionProvider;
10
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13
14 import java.util.*;
15
16 import javax.mail.*;
17 import javax.mail.internet.InternetAddress JavaDoc;
18 import javax.mail.internet.MimeMessage JavaDoc;
19
20
21 /**
22  * Sends an email to a group of users. The following arguments are expected:
23  *
24  * <ul>
25  * <li>to - comma seperated list of email addresses</li>
26  * <li>from - single email address</li>
27  * <li>subject - the message subject</li>
28  * <li>cc - comma seperated list of email addresses (optional)</li>
29  * <li>message - the message body</li>
30  * <li>smtpHost - the SMTP host that will relay the message</li>
31  * <li>parseVariables - if 'true', then variables of the form ${} in subject,
32  * message, to, and cc fields will be parsed</li>
33  * </ul>
34  *
35  * @author <a HREF="mailto:plightbo@hotmail.com">Pat Lightbody</a>
36  * @version $Revision: 1.4 $
37  */

38 public class SendEmail implements FunctionProvider {
39     //~ Static fields/initializers /////////////////////////////////////////////
40

41     private static final Log log = LogFactory.getLog(SendEmail.class);
42
43     //~ Methods ////////////////////////////////////////////////////////////////
44

45     public void execute(Map transientVars, Map args, PropertySet ps) {
46         String JavaDoc to = (String JavaDoc) args.get("to");
47         String JavaDoc from = (String JavaDoc) args.get("from");
48         String JavaDoc subject = (String JavaDoc) args.get("subject");
49         String JavaDoc cc = (String JavaDoc) args.get("cc");
50         String JavaDoc m = (String JavaDoc) args.get("message");
51         String JavaDoc smtpHost = (String JavaDoc) args.get("smtpHost");
52         boolean parseVariables = "true".equals(args.get("parseVariables"));
53
54         try {
55             Properties props = new Properties();
56             props.put("mail.smtp.host", smtpHost);
57
58             Session sendMailSession = Session.getInstance(props, null);
59             Transport transport = sendMailSession.getTransport("smtp");
60             Message JavaDoc message = new MimeMessage JavaDoc(sendMailSession);
61
62             message.setFrom(new InternetAddress JavaDoc(from));
63
64             Set toSet = new HashSet();
65             StringTokenizer st = new StringTokenizer(parseVariables ? ScriptVariableParser.translateVariables(to, transientVars, ps).toString() : to, ", ");
66
67             while (st.hasMoreTokens()) {
68                 String JavaDoc user = st.nextToken();
69                 toSet.add(new InternetAddress JavaDoc(user));
70             }
71
72             message.setRecipients(Message.RecipientType.TO, (InternetAddress JavaDoc[]) toSet.toArray(new InternetAddress JavaDoc[toSet.size()]));
73
74             Set ccSet = null;
75
76             if (cc != null) {
77                 ccSet = new HashSet();
78
79                 if (parseVariables) {
80                     cc = ScriptVariableParser.translateVariables(cc, transientVars, ps).toString();
81                 }
82
83                 st = new StringTokenizer(cc, ", ");
84
85                 while (st.hasMoreTokens()) {
86                     String JavaDoc user = st.nextToken();
87                     ccSet.add(new InternetAddress JavaDoc(user));
88                 }
89             }
90
91             if ((ccSet != null) && (ccSet.size() > 0)) {
92                 message.setRecipients(Message.RecipientType.CC, (InternetAddress JavaDoc[]) ccSet.toArray(new InternetAddress JavaDoc[ccSet.size()]));
93             }
94
95             message.setSubject(parseVariables ? ScriptVariableParser.translateVariables(subject, transientVars, ps).toString() : subject);
96             message.setSentDate(new Date());
97             message.setText(parseVariables ? ScriptVariableParser.translateVariables(m, transientVars, ps).toString() : m);
98             message.saveChanges();
99
100             transport.connect();
101             transport.sendMessage(message, message.getAllRecipients());
102             transport.close();
103         } catch (MessagingException e) {
104             log.error("Error sending email:", e);
105         }
106     }
107 }
108
Popular Tags