1 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 ; 18 import javax.mail.internet.MimeMessage ; 19 20 21 38 public class SendEmail implements FunctionProvider { 39 41 private static final Log log = LogFactory.getLog(SendEmail.class); 42 43 45 public void execute(Map transientVars, Map args, PropertySet ps) { 46 String to = (String ) args.get("to"); 47 String from = (String ) args.get("from"); 48 String subject = (String ) args.get("subject"); 49 String cc = (String ) args.get("cc"); 50 String m = (String ) args.get("message"); 51 String smtpHost = (String ) 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 message = new MimeMessage (sendMailSession); 61 62 message.setFrom(new InternetAddress (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 user = st.nextToken(); 69 toSet.add(new InternetAddress (user)); 70 } 71 72 message.setRecipients(Message.RecipientType.TO, (InternetAddress []) toSet.toArray(new InternetAddress [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 user = st.nextToken(); 87 ccSet.add(new InternetAddress (user)); 88 } 89 } 90 91 if ((ccSet != null) && (ccSet.size() > 0)) { 92 message.setRecipients(Message.RecipientType.CC, (InternetAddress []) ccSet.toArray(new InternetAddress [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 |