1 package org.apache.turbine.util.mail; 2 3 18 19 import java.util.Date ; 20 import java.util.Properties ; 21 import java.util.Vector ; 22 23 import javax.mail.Message ; 24 import javax.mail.MessagingException ; 25 import javax.mail.Session ; 26 import javax.mail.Transport ; 27 28 import javax.mail.internet.InternetAddress ; 29 import javax.mail.internet.MimeMessage ; 30 31 import org.apache.commons.configuration.Configuration; 32 33 import org.apache.commons.lang.StringUtils; 34 35 import org.apache.torque.util.Criteria; 36 37 import org.apache.turbine.Turbine; 38 import org.apache.turbine.TurbineConstants; 39 40 55 public abstract class Email 56 { 57 58 public static final String SENDER_EMAIL = "sender.email"; 59 public static final String SENDER_NAME = "sender.name"; 60 public static final String RECEIVER_EMAIL = "receiver.email"; 61 public static final String RECEIVER_NAME = "receiver.name"; 62 public static final String EMAIL_SUBJECT = "email.subject"; 63 public static final String EMAIL_BODY = "email.body"; 64 public static final String CONTENT_TYPE = "content.type"; 65 66 67 public static final String MAIL_SERVER = TurbineConstants.MAIL_SERVER_KEY; 68 69 70 public static final String MAIL_SMTP_FROM = TurbineConstants.MAIL_SMTP_FROM; 71 72 73 public static final String MAIL_HOST = "mail.host"; 74 75 public static final String MAIL_TRANSPORT_PROTOCOL = "mail.transport.protocol"; 76 public static final String SMTP = "SMTP"; 77 public static final String TEXT_HTML = "text/html"; 78 public static final String TEXT_PLAIN = "text/plain"; 79 public static final String ATTACHMENTS = "attachments"; 80 public static final String FILE_SERVER = "file.server"; 81 82 public static final String KOI8_R = "koi8-r"; 83 public static final String ISO_8859_1 = "iso-8859-1"; 84 public static final String US_ASCII = "us-ascii"; 85 86 87 protected MimeMessage message; 88 89 90 protected String charset = null; 91 92 93 private Vector toList; 94 private Vector ccList; 95 private Vector bccList; 96 private Vector replyList; 97 98 103 public void setCharset(String charset) 104 { 105 this.charset = charset; 106 } 107 108 113 private Session getMailSession() 114 { 115 Configuration conf = Turbine.getConfiguration(); 116 Properties properties = System.getProperties(); 117 118 properties.put(MAIL_TRANSPORT_PROTOCOL, SMTP); 119 properties.put(MAIL_HOST, conf.getString(TurbineConstants.MAIL_SERVER_KEY, 120 TurbineConstants.MAIL_SERVER_DEFAULT)); 121 122 123 String mailSMTPFrom = conf.getString(TurbineConstants.MAIL_SMTP_FROM); 124 125 if (StringUtils.isNotEmpty(mailSMTPFrom)) 126 { 127 properties.put(TurbineConstants.MAIL_SMTP_FROM, mailSMTPFrom); 128 } 129 return Session.getDefaultInstance(properties, null); 130 } 131 132 141 protected void initialize(Criteria criteria) throws MessagingException 142 { 143 init(); 144 initCriteria(criteria); 145 } 146 147 155 protected void init() throws MessagingException 156 { 157 158 message = new MimeMessage (getMailSession()); 160 161 toList = new Vector (); 162 ccList = new Vector (); 163 bccList = new Vector (); 164 replyList = new Vector (); 165 166 setSentDate(new Date ()); 168 } 169 170 181 protected void initCriteria(Criteria criteria) throws MessagingException 182 { 183 if (criteria.containsKey(SENDER_EMAIL) 185 && criteria.containsKey(SENDER_NAME)) 186 { 187 setFrom(criteria.getString(SENDER_EMAIL), 188 criteria.getString(SENDER_NAME)); 189 } 190 191 if (criteria.containsKey(RECEIVER_EMAIL) 193 && criteria.containsKey(RECEIVER_NAME)) 194 { 195 addTo(criteria.getString(RECEIVER_EMAIL), 196 criteria.getString(RECEIVER_NAME)); 197 } 198 199 if (criteria.containsKey(EMAIL_SUBJECT)) 201 { 202 setSubject(criteria.getString(EMAIL_SUBJECT)); 203 } 204 else 205 { 206 setSubject("no subject available"); 207 } 208 } 209 210 218 public Email setFrom(String email, String name) throws MessagingException 219 { 220 try 221 { 222 if (name == null || name.trim().equals("")) 223 { 224 name = email; 225 } 226 message.setFrom(new InternetAddress (email, name)); 227 } 228 catch (Exception e) 229 { 230 throw new MessagingException ("cannot set from", e); 231 } 232 return this; 233 } 234 235 243 public Email addTo(String email, String name) throws MessagingException 244 { 245 try 246 { 247 if (name == null || name.trim().equals("")) 248 { 249 name = email; 250 } 251 toList.addElement(new InternetAddress (email, name)); 252 } 253 catch (Exception e) 254 { 255 throw new MessagingException ("cannot add to", e); 256 } 257 return this; 258 } 259 260 268 public Email addCc(String email, String name) throws MessagingException 269 { 270 271 try 272 { 273 if (name == null || name.trim().equals("")) 274 { 275 name = email; 276 } 277 ccList.addElement(new InternetAddress (email, name)); 278 } 279 catch (Exception e) 280 { 281 throw new MessagingException ("cannot add cc", e); 282 } 283 284 return this; 285 } 286 287 295 public Email addBcc(String email, String name) 296 throws MessagingException 297 { 298 try 299 { 300 if (name == null || name.trim().equals("")) 301 { 302 name = email; 303 } 304 bccList.addElement(new InternetAddress (email, name)); 305 } 306 catch (Exception e) 307 { 308 throw new MessagingException ("cannot add bcc", e); 309 } 310 311 return this; 312 } 313 314 322 public Email addReplyTo(String email, String name) 323 throws MessagingException 324 { 325 try 326 { 327 if (name == null || name.trim().equals("")) 328 { 329 name = email; 330 } 331 replyList.addElement(new InternetAddress (email, name)); 332 } 333 catch (Exception e) 334 { 335 throw new MessagingException ("cannot add replyTo", e); 336 } 337 return this; 338 } 339 340 347 public Email setSubject(String subject) 348 throws MessagingException 349 { 350 if (subject != null) 351 { 352 if (charset != null) 353 { 354 message.setSubject(subject, charset); 355 } 356 else 357 { 358 message.setSubject(subject); 359 } 360 } 361 return this; 362 } 363 364 371 public Email setSentDate(Date date) 372 throws MessagingException 373 { 374 if (date != null) 375 { 376 message.setSentDate(date); 377 } 378 return this; 379 } 380 381 389 public abstract Email setMsg(String msg) 390 throws MessagingException ; 391 392 397 public void send() 398 throws MessagingException 399 { 400 InternetAddress [] foo = new InternetAddress [0]; 401 message.setRecipients(Message.RecipientType.TO, 402 toInternetAddressArray(toList)); 403 message.setRecipients(Message.RecipientType.CC, 404 toInternetAddressArray(ccList)); 405 message.setRecipients(Message.RecipientType.BCC, 406 toInternetAddressArray(bccList)); 407 message.setReplyTo(toInternetAddressArray(replyList)); 408 Transport.send(message); 409 } 410 411 418 private InternetAddress [] toInternetAddressArray(Vector v) 419 { 420 int size = v.size(); 421 InternetAddress [] ia = new InternetAddress [size]; 422 for (int i = 0; i < size; i++) 423 { 424 ia[i] = (InternetAddress ) v.elementAt(i); 425 } 426 return ia; 427 } 428 } 429 | Popular Tags |