1 17 18 21 package org.quartz.jobs.ee.mail; 22 23 import java.util.Date ; 24 import java.util.Properties ; 25 26 import javax.mail.Address ; 27 import javax.mail.Message ; 28 import javax.mail.MessagingException ; 29 import javax.mail.Session ; 30 import javax.mail.Transport ; 31 import javax.mail.internet.InternetAddress ; 32 import javax.mail.internet.MimeMessage ; 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 49 public class SendMailJob implements Job { 50 51 private final Log log = LogFactory.getLog(getClass()); 52 53 60 61 64 public static final String PROP_SMTP_HOST = "smtp_host"; 65 66 69 public static final String PROP_RECIPIENT = "recipient"; 70 71 74 public static final String PROP_CC_RECIPIENT = "cc_recipient"; 75 76 79 public static final String PROP_SENDER = "sender"; 80 81 84 public static final String PROP_REPLY_TO = "reply_to"; 85 86 89 public static final String PROP_SUBJECT = "subject"; 90 91 94 public static final String PROP_MESSAGE = "message"; 95 96 99 public static final String PROP_CONTENT_TYPE = "content_type"; 100 101 108 109 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 mimeMessage = prepareMimeMessage(mailInfo); 123 124 Transport.send(mimeMessage); 125 } catch (MessagingException 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 prepareMimeMessage(MailInfo mailInfo) 137 throws MessagingException { 138 Session session = getMailSession(mailInfo); 139 140 MimeMessage mimeMessage = new MimeMessage (session); 141 142 Address [] toAddresses = InternetAddress.parse(mailInfo.getTo()); 143 mimeMessage.setRecipients(Message.RecipientType.TO, toAddresses); 144 145 if (mailInfo.getCc() != null) { 146 Address [] ccAddresses = InternetAddress.parse(mailInfo.getCc()); 147 mimeMessage.setRecipients(Message.RecipientType.CC, ccAddresses); 148 } 149 150 mimeMessage.setFrom(new InternetAddress (mailInfo.getFrom())); 151 152 if (mailInfo.getReplyTo() != null) { 153 mimeMessage.setReplyTo(new InternetAddress []{new InternetAddress (mailInfo.getReplyTo())}); 154 } 155 156 mimeMessage.setSubject(mailInfo.getSubject()); 157 158 mimeMessage.setSentDate(new Date ()); 159 160 setMimeMessageContent(mimeMessage, mailInfo); 161 162 return mimeMessage; 163 } 164 165 protected void setMimeMessageContent(MimeMessage mimeMessage, MailInfo mailInfo) 166 throws MessagingException { 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 getMailSession(MailInfo mailInfo) throws MessagingException { 175 Properties properties = new Properties (); 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 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 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 getRequiredParm(JobDataMap data, String property, String constantName) { 203 String value = getOptionalParm(data, property); 204 205 if (value == null) { 206 throw new IllegalArgumentException (constantName + " not specified."); 207 } 208 209 return value; 210 } 211 212 protected String getOptionalParm(JobDataMap data, String property) { 213 String 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 smtpHost; 224 private String to; 225 private String from; 226 private String subject; 227 private String message; 228 private String replyTo; 229 private String cc; 230 private String contentType; 231 232 public String toString() { 233 return "'" + getSubject() + "' to: " + getTo(); 234 } 235 236 public String getCc() { 237 return cc; 238 } 239 240 public void setCc(String cc) { 241 this.cc = cc; 242 } 243 244 public String getContentType() { 245 return contentType; 246 } 247 248 public void setContentType(String contentType) { 249 this.contentType = contentType; 250 } 251 252 public String getFrom() { 253 return from; 254 } 255 256 public void setFrom(String from) { 257 this.from = from; 258 } 259 260 public String getMessage() { 261 return message; 262 } 263 264 public void setMessage(String message) { 265 this.message = message; 266 } 267 268 public String getReplyTo() { 269 return replyTo; 270 } 271 272 public void setReplyTo(String replyTo) { 273 this.replyTo = replyTo; 274 } 275 276 public String getSmtpHost() { 277 return smtpHost; 278 } 279 280 public void setSmtpHost(String smtpHost) { 281 this.smtpHost = smtpHost; 282 } 283 284 public String getSubject() { 285 return subject; 286 } 287 288 public void setSubject(String subject) { 289 this.subject = subject; 290 } 291 292 public String getTo() { 293 return to; 294 } 295 296 public void setTo(String to) { 297 this.to = to; 298 } 299 } 300 } 301 | Popular Tags |