KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > jobs > ee > mail > SendMailJob


1 /*
2  * Copyright 2004-2005 OpenSymphony
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy
6  * of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  *
16  */

17
18 /*
19  * Previously Copyright (c) 2001-2004 James House
20  */

21 package org.quartz.jobs.ee.mail;
22
23 import java.util.Date JavaDoc;
24 import java.util.Properties JavaDoc;
25
26 import javax.mail.Address JavaDoc;
27 import javax.mail.Message JavaDoc;
28 import javax.mail.MessagingException JavaDoc;
29 import javax.mail.Session JavaDoc;
30 import javax.mail.Transport JavaDoc;
31 import javax.mail.internet.InternetAddress JavaDoc;
32 import javax.mail.internet.MimeMessage JavaDoc;
33
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36 import org.quartz.Job;
37 import org.quartz.JobDataMap;
38 import org.quartz.JobExecutionContext;
39 import org.quartz.JobExecutionException;
40
41 /**
42  * <p>
43  * A Job which sends an e-mail with the configured content to the configured
44  * recipient.
45  * </p>
46  *
47  * @author James House
48  */

49 public class SendMailJob implements Job {
50
51     private final Log log = LogFactory.getLog(getClass());
52
53     /*
54      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
55      *
56      * Constants.
57      *
58      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
59      */

60
61     /**
62      * The host name of the smtp server. REQUIRED.
63      */

64     public static final String JavaDoc PROP_SMTP_HOST = "smtp_host";
65
66     /**
67      * The e-mail address to send the mail to. REQUIRED.
68      */

69     public static final String JavaDoc PROP_RECIPIENT = "recipient";
70
71     /**
72      * The e-mail address to cc the mail to. Optional.
73      */

74     public static final String JavaDoc PROP_CC_RECIPIENT = "cc_recipient";
75
76     /**
77      * The e-mail address to claim the mail is from. REQUIRED.
78      */

79     public static final String JavaDoc PROP_SENDER = "sender";
80
81     /**
82      * The e-mail address the message should say to reply to. Optional.
83      */

84     public static final String JavaDoc PROP_REPLY_TO = "reply_to";
85
86     /**
87      * The subject to place on the e-mail. REQUIRED.
88      */

89     public static final String JavaDoc PROP_SUBJECT = "subject";
90
91     /**
92      * The e-mail message body. REQUIRED.
93      */

94     public static final String JavaDoc PROP_MESSAGE = "message";
95
96     /**
97      * The message content type. For example, "text/html". Optional.
98      */

99     public static final String JavaDoc PROP_CONTENT_TYPE = "content_type";
100
101     /*
102      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
103      *
104      * Interface.
105      *
106      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
107      */

108
109     /**
110      * @see org.quartz.Job#execute(org.quartz.JobExecutionContext)
111      */

112     public void execute(JobExecutionContext context)
113         throws JobExecutionException {
114
115         JobDataMap data = context.getMergedJobDataMap();
116
117         MailInfo mailInfo = populateMailInfo(data, createMailInfo());
118         
119         getLog().info("Sending message " + mailInfo);
120
121         try {
122             MimeMessage JavaDoc mimeMessage = prepareMimeMessage(mailInfo);
123             
124             Transport.send(mimeMessage);
125         } catch (MessagingException JavaDoc e) {
126             throw new JobExecutionException("Unable to send mail: " + mailInfo,
127                     e, false);
128         }
129
130     }
131
132     protected Log getLog() {
133         return log;
134     }
135
136     protected MimeMessage JavaDoc prepareMimeMessage(MailInfo mailInfo)
137         throws MessagingException JavaDoc {
138         Session JavaDoc session = getMailSession(mailInfo);
139
140         MimeMessage JavaDoc mimeMessage = new MimeMessage JavaDoc(session);
141
142         Address JavaDoc[] toAddresses = InternetAddress.parse(mailInfo.getTo());
143         mimeMessage.setRecipients(Message.RecipientType.TO, toAddresses);
144
145         if (mailInfo.getCc() != null) {
146             Address JavaDoc[] ccAddresses = InternetAddress.parse(mailInfo.getCc());
147             mimeMessage.setRecipients(Message.RecipientType.CC, ccAddresses);
148         }
149
150         mimeMessage.setFrom(new InternetAddress JavaDoc(mailInfo.getFrom()));
151         
152         if (mailInfo.getReplyTo() != null) {
153             mimeMessage.setReplyTo(new InternetAddress JavaDoc[]{new InternetAddress JavaDoc(mailInfo.getReplyTo())});
154         }
155         
156         mimeMessage.setSubject(mailInfo.getSubject());
157         
158         mimeMessage.setSentDate(new Date JavaDoc());
159
160         setMimeMessageContent(mimeMessage, mailInfo);
161
162         return mimeMessage;
163     }
164     
165     protected void setMimeMessageContent(MimeMessage JavaDoc mimeMessage, MailInfo mailInfo)
166         throws MessagingException JavaDoc {
167         if (mailInfo.getContentType() == null) {
168             mimeMessage.setText(mailInfo.getMessage());
169         } else {
170             mimeMessage.setContent(mailInfo.getMessage(), mailInfo.getContentType());
171         }
172     }
173
174     protected Session JavaDoc getMailSession(MailInfo mailInfo) throws MessagingException JavaDoc {
175         Properties JavaDoc properties = new Properties JavaDoc();
176         properties.put("mail.smtp.host", mailInfo.getSmtpHost());
177         
178         return Session.getDefaultInstance(properties, null);
179     }
180     
181     protected MailInfo createMailInfo() {
182         return new MailInfo();
183     }
184     
185     protected MailInfo populateMailInfo(JobDataMap data, MailInfo mailInfo) {
186         // Required parameters
187
mailInfo.setSmtpHost(getRequiredParm(data, PROP_SMTP_HOST, "PROP_SMTP_HOST"));
188         mailInfo.setTo(getRequiredParm(data, PROP_RECIPIENT, "PROP_RECIPIENT"));
189         mailInfo.setFrom(getRequiredParm(data, PROP_SENDER, "PROP_SENDER"));
190         mailInfo.setSubject(getRequiredParm(data, PROP_SUBJECT, "PROP_SUBJECT"));
191         mailInfo.setMessage(getRequiredParm(data, PROP_MESSAGE, "PROP_MESSAGE"));
192         
193         // Optional parameters
194
mailInfo.setReplyTo(getOptionalParm(data, PROP_REPLY_TO));
195         mailInfo.setCc(getOptionalParm(data, PROP_CC_RECIPIENT));
196         mailInfo.setContentType(getOptionalParm(data, PROP_CONTENT_TYPE));
197         
198         return mailInfo;
199     }
200     
201     
202     protected String JavaDoc getRequiredParm(JobDataMap data, String JavaDoc property, String JavaDoc constantName) {
203         String JavaDoc value = getOptionalParm(data, property);
204         
205         if (value == null) {
206             throw new IllegalArgumentException JavaDoc(constantName + " not specified.");
207         }
208         
209         return value;
210     }
211     
212     protected String JavaDoc getOptionalParm(JobDataMap data, String JavaDoc property) {
213         String JavaDoc value = data.getString(property);
214         
215         if ((value != null) && (value.trim().length() == 0)) {
216             return null;
217         }
218         
219         return value;
220     }
221     
222     protected static class MailInfo {
223         private String JavaDoc smtpHost;
224         private String JavaDoc to;
225         private String JavaDoc from;
226         private String JavaDoc subject;
227         private String JavaDoc message;
228         private String JavaDoc replyTo;
229         private String JavaDoc cc;
230         private String JavaDoc contentType;
231
232         public String JavaDoc toString() {
233             return "'" + getSubject() + "' to: " + getTo();
234         }
235         
236         public String JavaDoc getCc() {
237             return cc;
238         }
239
240         public void setCc(String JavaDoc cc) {
241             this.cc = cc;
242         }
243
244         public String JavaDoc getContentType() {
245             return contentType;
246         }
247
248         public void setContentType(String JavaDoc contentType) {
249             this.contentType = contentType;
250         }
251
252         public String JavaDoc getFrom() {
253             return from;
254         }
255
256         public void setFrom(String JavaDoc from) {
257             this.from = from;
258         }
259
260         public String JavaDoc getMessage() {
261             return message;
262         }
263
264         public void setMessage(String JavaDoc message) {
265             this.message = message;
266         }
267
268         public String JavaDoc getReplyTo() {
269             return replyTo;
270         }
271
272         public void setReplyTo(String JavaDoc replyTo) {
273             this.replyTo = replyTo;
274         }
275
276         public String JavaDoc getSmtpHost() {
277             return smtpHost;
278         }
279
280         public void setSmtpHost(String JavaDoc smtpHost) {
281             this.smtpHost = smtpHost;
282         }
283
284         public String JavaDoc getSubject() {
285             return subject;
286         }
287
288         public void setSubject(String JavaDoc subject) {
289             this.subject = subject;
290         }
291
292         public String JavaDoc getTo() {
293             return to;
294         }
295
296         public void setTo(String JavaDoc to) {
297             this.to = to;
298         }
299     }
300 }
301
Popular Tags