001: package org.concern.library.mail;
002:
003: import org.concern.controller.AbstractSynchronousActivity;
004: import org.concern.controller.ActivityExecutionException;
005: import org.conform.format.TemplateFormat;
006:
007: import javax.mail.*;
008: import javax.mail.internet.*;
009: import java.util.*;
010: import java.sql.Timestamp;
011:
012: import ognl.Ognl;
013: import ognl.OgnlException;
014:
015: /**
016: * @author hengels
017: * @version $Revision$
018: */
019: public class SendMail extends AbstractSynchronousActivity {
020: private String from;
021: private String to;
022: private String cc;
023: private String subject;
024: private String body;
025: private String flag;
026: private String stamp;
027:
028: public String getFrom() {
029: return from;
030: }
031:
032: public void setFrom(String from) {
033: this .from = from;
034: }
035:
036: public String getTo() {
037: return to;
038: }
039:
040: public void setTo(String to) {
041: this .to = to;
042: }
043:
044: public String getCc() {
045: return cc;
046: }
047:
048: public void setCc(String cc) {
049: this .cc = cc;
050: }
051:
052: public String getSubject() {
053: return subject;
054: }
055:
056: public void setSubject(String subject) {
057: this .subject = subject;
058: }
059:
060: public String getBody() {
061: return body;
062: }
063:
064: public void setBody(String body) {
065: this .body = body;
066: }
067:
068: public String getFlag() {
069: return flag;
070: }
071:
072: public void setFlag(String flag) {
073: this .flag = flag;
074: }
075:
076: public String getStamp() {
077: return stamp;
078: }
079:
080: public void setStamp(String stamp) {
081: this .stamp = stamp;
082: }
083:
084: public void execute(Object subject)
085: throws ActivityExecutionException {
086: Transport transport = null;
087: try {
088: Map context = new HashMap();
089: context.put("subject", subject);
090: context.putAll(environment);
091:
092: javax.mail.Session mailSession = (javax.mail.Session) controller
093: .getResourceLocator().lookup("mailSession");
094: MimeMessage message = new MimeMessage(mailSession);
095: message.setFrom(from(context));
096: message
097: .setRecipients(Message.RecipientType.TO,
098: to(context));
099: message
100: .setRecipients(Message.RecipientType.CC,
101: cc(context));
102: message.setSubject(new TemplateFormat(getSubject())
103: .format(context));
104: message.setText(new TemplateFormat(getBody())
105: .format(context));
106:
107: if (message.getRecipients(Message.RecipientType.TO) != null
108: && message.getRecipients(Message.RecipientType.TO).length > 0) {
109: transport = mailSession.getTransport("smtp");
110: transport.connect();
111: transport.sendMessage(message, message
112: .getRecipients(Message.RecipientType.TO));
113: }
114:
115: mark(subject);
116: } catch (Exception e) {
117: e.printStackTrace();
118: } finally {
119: try {
120: if (transport != null)
121: transport.close();
122: } catch (MessagingException e) {
123: }
124: }
125: }
126:
127: protected InternetAddress from(Map context) throws AddressException {
128: try {
129: return new InternetAddress((String) Ognl.getValue(
130: getFrom(), context));
131: } catch (OgnlException e) {
132: return null;
133: }
134: }
135:
136: // TODO: multiple TOs
137: protected InternetAddress[] to(Map context) throws AddressException {
138: try {
139: return new InternetAddress[] { new InternetAddress(
140: (String) Ognl.getValue(getTo(), context)) };
141: } catch (OgnlException e) {
142: return null;
143: }
144: }
145:
146: // TODO: multiple CCs
147: protected InternetAddress[] cc(Map context) throws AddressException {
148: try {
149: return new InternetAddress[] { new InternetAddress(
150: (String) Ognl.getValue(getCc(), context)) };
151: } catch (OgnlException e) {
152: return null;
153: }
154: }
155:
156: protected void mark(Object subject)
157: throws ActivityExecutionException {
158: try {
159: if (flag != null)
160: Ognl.setValue(flag, subject, true);
161: } catch (OgnlException e) {
162: throw new ActivityExecutionException(flag + ": "
163: + e.getMessage());
164: }
165: try {
166: if (stamp != null)
167: Ognl.setValue(stamp, subject, new Timestamp(System
168: .currentTimeMillis()));
169: } catch (OgnlException e) {
170: throw new ActivityExecutionException(stamp + ": "
171: + e.getMessage());
172: }
173: }
174: }
|