1 16 17 package org.apache.taglibs.mailer; 18 19 import java.util.ArrayList ; 20 import java.util.Date ; 21 import java.util.ListIterator ; 22 import javax.mail.Message ; 23 import javax.mail.MessagingException ; 24 import javax.mail.Transport ; 25 import javax.mail.internet.AddressException ; 26 import javax.mail.internet.InternetAddress ; 27 import javax.mail.internet.MimeBodyPart ; 28 import javax.mail.internet.MimeMessage ; 29 import javax.mail.internet.MimeMultipart ; 30 import javax.servlet.jsp.JspException ; 31 import javax.servlet.jsp.tagext.BodyTagSupport ; 32 33 48 49 public class SendTag extends BodyTagSupport { 50 51 ArrayList error = null; 53 64 public int doStartTag() throws JspException { 65 66 error = null; 67 int i = 0; MimeMessage message; MimeMultipart multipart; ListIterator iterate; String to = null; String cc = null; String bcc = null; 77 MailTag myparent = (MailTag)findAncestorWithClass(this, MailTag.class); 79 80 if (myparent == null) 81 throw new JspException ("send tag not nested within mail tag"); 82 83 message = myparent.getMessage(); 85 86 ArrayList name = myparent.getHeaderName(); 88 ArrayList value = myparent.getHeaderValue(); 89 90 try { 91 if (name.size() > 0) 93 for (i = 0; i < name.size(); i++) 94 message.addHeader((String )name.get(i), 95 (String )value.get(i)); 96 } catch (MessagingException me) { 97 throw new JspException ("Header " + name.get(i).toString() 98 + " was not able to be set"); 99 } 100 101 if ((to = myparent.getTo()) != null) { 103 104 try { 105 message.setRecipients(Message.RecipientType.TO, 108 InternetAddress.parse(to)); 109 } catch (AddressException ae) { 110 String ref = ae.getRef(); 112 113 if (ref.indexOf(',') != -1) { 115 int pos = ref.substring(0, ae.getPos()).indexOf(',') + 1; 117 ref = ref.substring(pos, ref.indexOf(',')); 119 } 120 121 if (error == null) 123 error = new ArrayList (); 124 125 String errorinput = "The to address " + ref + " is not in" 126 + " the proper format."; 127 128 error.add(errorinput); 129 } catch (MessagingException me) { 130 if (error == null) 132 error = new ArrayList (); 133 134 String errorinput = "Messaging Exception: To address/es could" 137 + " not be set in the message." + me.getMessage(); 138 139 error.add(errorinput); 140 } 141 } else { 142 String errorinput = "A to address must be supplied."; 144 145 if (error == null) 147 error = new ArrayList (); 148 149 error.add(errorinput); 150 } 151 152 if (myparent.getReplyTo() != null) { 154 155 try { 156 message.setReplyTo(InternetAddress.parse(myparent.getReplyTo())); 157 } catch (AddressException ae) { 158 if (error == null) 160 error = new ArrayList (); 161 162 String errorinput = "The Reply-To address was incorrectly set"; 164 165 error.add(errorinput); 166 } catch (MessagingException me) { 167 if (error == null) 169 error = new ArrayList (); 170 171 String errorinput = "Messaging Exception: Reply-To address/es" 174 + " could not be set in the message." + me.getMessage(); 175 176 error.add(errorinput); 177 } 178 } 179 180 try { 181 182 String from = myparent.getFrom(); 184 185 if ((from == null) || (from.length() < 2)) { 188 189 if (myparent.getSessionObj().getProperty("mail.from") != null) 191 192 message.setFrom(new InternetAddress ( 193 myparent.getSessionObj().getProperty("mail.from"))); 194 } 195 else 196 message.setFrom(new InternetAddress (from)); 197 198 } catch (MessagingException me) { 199 if (error == null) 201 error = new ArrayList (); 202 203 String errorinput = "The from address was not set or is not in" 205 + " the proper format for an email address."; 206 207 error.add(errorinput); 208 } 209 210 if ((cc = myparent.getCc()) != null) { 212 213 try { 214 message.setRecipients(Message.RecipientType.CC, 215 InternetAddress.parse(cc)); 216 } catch (AddressException ae) { 217 String ref = ae.getRef(); 219 220 if (ref.indexOf(',') != -1) { 222 int pos = ref.substring(0, ae.getPos()).indexOf(',') + 1; 224 ref = ref.substring(pos, ref.indexOf(',')); 226 } 227 228 if (error == null) 230 error = new ArrayList (); 231 232 String errorinput = "The cc address " + ref + " is not in" 234 + " the proper format."; 235 236 error.add(errorinput); 237 } catch (MessagingException me) { 238 if (error == null) 240 error = new ArrayList (); 241 242 String errorinput = "Messaging Exception: Some cc address/es" 245 + " could not be set in the message." 246 + me.getMessage(); 247 248 error.add(errorinput); 249 } 250 } 251 252 if ((bcc = myparent.getBcc()) != null) { 254 255 try { 256 message.setRecipients(Message.RecipientType.BCC, 257 InternetAddress.parse(bcc)); 258 } catch (AddressException ae) { 259 String ref = ae.getRef(); 261 262 if (ref.indexOf(',') != -1) { 264 int pos = ref.substring(0, ae.getPos()).indexOf(',') + 1; 266 ref = ref.substring(pos, ref.indexOf(',')); 268 } 269 270 if (error == null) 272 error = new ArrayList (); 273 274 String errorinput = "The bcc address " + ref + " is not in" 276 + " the proper format."; 277 278 error.add(errorinput); 279 } catch (MessagingException me) { 280 if (error == null) 282 error = new ArrayList (); 283 284 String errorinput = "Messaging Exception: Some bcc address/es" 287 + " could not be set in the message." 288 + me.getMessage(); 289 290 error.add(errorinput); 291 } 292 } 293 294 try { 295 message.setSubject(myparent.getSubject()); 297 298 message.setSentDate(new Date ()); 300 } catch (MessagingException me) { 301 } 303 304 if (myparent.getAttachments()) { 306 multipart = new MimeMultipart (); 309 310 try { 311 312 MimeBodyPart mbp = new MimeBodyPart (); 314 315 mbp.setContent(myparent.getBody(), getContentType(myparent)); 317 318 multipart.addBodyPart(mbp); 320 321 iterate = myparent.getBodyParts().listIterator(); 323 324 while (iterate.hasNext()) { 327 multipart.addBodyPart((MimeBodyPart ) iterate.next()); 328 } 329 330 message.setContent(multipart); 332 333 } catch (MessagingException me) { 334 throw new JspException ("An error occured while trying to add" + 337 "the attachments to the e-mail, please" 338 +" to send the e-mail again."); 339 } 340 } else { 341 try { 342 message.setContent(myparent.getBody(), getContentType(myparent)); 344 } catch (MessagingException me) { 345 throw new JspException ("The message could not be set in " + 347 "the e-mail, please back up and try again."); 348 } 349 } 350 if (error != null) 352 return EVAL_BODY_TAG; 354 else { 357 Mail mail = new Mail(message, pageContext.getServletContext(), to); 359 360 mail.start(); 362 return SKIP_BODY; 363 } 364 } 365 366 377 public int doEndTag() throws JspException { 378 379 if (error != null) { 381 try { 383 if (bodyContent != null) 384 bodyContent.writeOut(bodyContent.getEnclosingWriter()); 385 } catch(java.io.IOException e) { 386 throw new JspException ("IO Error: " + e.getMessage()); 387 } 388 error = null; } 390 return EVAL_PAGE; 391 } 392 393 399 public ArrayList getError() { 400 return error; 401 } 402 403 408 public String getContentType(MailTag parent) { 409 String type = parent.getType(); 410 String charset = parent.getCharset(); 411 412 if (charset == null) { 413 return type; 414 } 415 416 return type + ";charset=" + charset; 417 } 418 } 419 420 429 class Mail extends Thread { 430 431 private MimeMessage message = null; private javax.servlet.ServletContext sc = null; 434 String mailto; 436 Mail (MimeMessage mail, javax.servlet.ServletContext servletcontext, 437 String to) { 438 message = mail; 439 sc = servletcontext; 440 mailto = to; 441 } 442 443 public void run() { 444 445 try { 446 Transport.send(message); 448 449 } catch (MessagingException me) { 450 sc.log("Could not send the e-mail sent to " + mailto + ": " + 459 me.getMessage()); 460 } 461 } 462 } 463 | Popular Tags |