1 16 17 package org.apache.taglibs.mailer; 18 19 import java.io.IOException ; 20 import java.util.ArrayList ; 21 import java.util.Properties ; 22 import javax.mail.Session ; 23 import javax.mail.internet.MimeBodyPart ; 24 import javax.mail.internet.MimeMessage ; 25 import javax.mail.internet.MimePartDataSource ; 26 import javax.naming.Context ; 27 import javax.naming.InitialContext ; 28 import javax.naming.NamingException ; 29 import javax.servlet.jsp.JspException ; 30 import javax.servlet.jsp.tagext.BodyTagSupport ; 31 32 108 109 public class MailTag extends BodyTagSupport { 110 111 114 protected final static String TO_ADDRESS_STRING = "to"; 115 protected final static String CC_ADDRESS_STRING = "cc"; 116 protected final static String BCC_ADDRESS_STRING = "bcc"; 117 118 121 protected final static int TO_ADDRESS = 1; 122 protected final static int CC_ADDRESS = 2; 123 protected final static int BCC_ADDRESS = 3; 124 125 128 private String to = null; 129 private StringBuffer addTO = new StringBuffer (); 130 131 134 private String from = null; 135 private String reset_from = null; 136 137 140 private String cc = null; 141 private StringBuffer addCC = new StringBuffer (); 142 143 146 private String bcc = null; 147 private StringBuffer addBCC = new StringBuffer (); 148 149 152 private String subject = ""; 153 private String reset_subject; 154 155 158 private String body = null; 159 160 163 private String server = "localhost"; 164 private String reset_server = null; 165 166 169 private String port = "25"; 170 private String reset_port = null; 171 172 175 private String type = "text/plain"; 176 177 180 private String charset = null; 181 182 185 private String session = null; 186 187 190 private String mimemessage = null; 191 192 195 private String user = null; 196 197 200 private String password = null; 201 202 205 private Session sessionobj = null; 206 207 210 private String replyto = null; 211 private String reset_replyto = null; 212 213 216 private ArrayList name = new ArrayList (10); 217 218 221 private ArrayList value = new ArrayList (10); 222 223 226 private ArrayList bodyparts = new ArrayList (10); 227 228 231 private boolean attachments = false; 232 233 236 private boolean error = false; 237 238 243 private boolean authentication = false; 244 245 256 public int doStartTag() throws JspException { 257 addTO.setLength(0); 259 if (to != null) { 260 addTO.append(to); 261 } 262 addCC.setLength(0); 263 if (cc != null) { 264 addCC.append(cc); 265 } 266 addBCC.setLength(0); 267 if (bcc != null) { 268 addBCC.append(bcc); 269 } 270 reset_from = null; 271 reset_replyto = null; 272 reset_subject = null; 273 reset_server = null; 274 reset_port = null; 275 name.clear(); 276 value.clear(); 277 bodyparts.clear(); 278 attachments = false; 279 sessionobj = null; 280 281 return EVAL_BODY_TAG; } 286 287 298 public int doEndTag() throws JspException { 299 300 if (bodyContent.getString().trim().length() == 0) 302 return EVAL_PAGE; 303 else { 304 try { 306 pageContext.getOut().write(bodyContent.getString()); 307 } catch (IOException ie) { 308 pageContext.getServletContext().log("Mailer taglib: Error tag" 309 + ": unable to write out to jsp."); 310 } 311 return SKIP_PAGE; 312 } 313 } 314 315 321 public MimeMessage getMessage() throws JspException { 322 MimeMessage message; 323 if (mimemessage != null) { 324 try { 325 Context ctx = new InitialContext (); 327 328 MimePartDataSource mds = 330 (MimePartDataSource )ctx.lookup(mimemessage); 331 sessionobj = mds.getMessageContext().getSession(); 332 message = new MimeMessage (sessionobj); 333 } catch (NamingException ne) { 334 throw new JspException ("Naming Exception " + 335 ne.getExplanation()); 336 } 337 } else if (session != null) { 338 try { 339 Context ctx = new InitialContext (); 341 342 sessionobj = (Session )ctx.lookup(session); 345 message = new MimeMessage (sessionobj); 346 } catch (NamingException ne) { 347 throw new JspException ("Naming Exception " + 348 ne.getExplanation()); 349 } 350 } else { 351 355 Properties props = new Properties (); 357 if (reset_server != null) { 359 props.put("mail.smtp.host", reset_server); 360 } else { 361 props.put("mail.smtp.host", server); 362 } 363 if (reset_port != null) { 365 props.put("mail.smtp.port", reset_port); 366 } else { 367 props.put("mail.smtp.port", port); 368 } 369 props.put("mail.smtp.sendpartial", "true"); 372 props.put("mail.smtp.dsn.notify", "FAILURE"); 374 props.put("mail.smtp.dsn.ret", "FULL"); 376 379 if (authentication) { 380 props.put("mail.smtp.auth", "true"); 383 sessionobj = Session.getDefaultInstance(props, 384 new MailAuthenticator(user, password)); 385 } else 386 sessionobj = Session.getDefaultInstance(props, null); 387 388 message = new MimeMessage (sessionobj); 389 } 390 return message; 391 } 392 393 399 public ArrayList getBodyParts() { 400 return bodyparts; 401 } 402 403 409 public boolean getAttachments() { 410 return attachments; 411 } 412 413 419 public String getTo() { 420 if (addTO.length() > 0 ) { 421 return addTO.toString(); 422 } 423 return null; 424 } 425 426 432 public String getReplyTo() { 433 if (reset_replyto != null) { 434 return reset_replyto; 435 } 436 return replyto; 437 } 438 439 445 public String getFrom() { 446 if (reset_from != null) { 447 return reset_from; 448 } 449 return from; 450 } 451 452 459 public String getCc() { 460 if (addCC.length() > 0 ) { 461 return addCC.toString(); 462 } 463 return null; 464 } 465 466 473 public String getBcc() { 474 if (addBCC.length() > 0 ) { 475 return addBCC.toString(); 476 } 477 return null; 478 } 479 480 486 public String getSubject() { 487 if (reset_subject != null) { 488 return reset_subject; 489 } 490 return subject; 491 } 492 493 499 public ArrayList getHeaderName() { 500 return name; 501 } 502 503 509 public ArrayList getHeaderValue() { 510 return value; 511 } 512 513 519 public String getBody() { 520 return body; 521 } 522 523 524 530 public Session getSessionObj() { 531 return sessionobj; 532 } 533 534 540 public String getType() { 541 return type; 542 } 543 544 550 public String getCharset() { 551 return charset; 552 } 553 554 560 public void setError(boolean value) { 561 error = value; 562 } 563 564 570 public void setTo(String value) { 571 to = value; 572 } 573 574 580 public void setReplyTo(String value) { 581 replyto = value; 582 } 583 584 590 public void setFrom(String value) { 591 from = value; 592 } 593 594 601 public void setCc(String value) { 602 cc = value; 603 } 604 605 612 public void setBcc(String value) { 613 bcc = value; 614 } 615 616 622 public void setSubject(String value) { 623 subject = value; 624 } 625 626 632 protected void setHeader(String name, String value) { 633 this.name.add(name); 634 this.value.add(value); 635 } 636 637 643 public void setMessage(String value) { 644 body = value; 645 } 646 647 653 public void setBodyParts(MimeBodyPart mbp) { 654 bodyparts.add(mbp); 655 656 if (attachments == false) 660 attachments = true; 661 } 662 663 669 public void setUser(String value) { 670 user = value; 671 } 672 673 679 public void setPassword(String value) { 680 password = value; 681 } 682 683 689 public void setServer(String value) { 690 server = value; 691 } 692 693 699 public void setPort(String value) { 700 port = value; 701 } 702 703 709 public void setMimeMessage(String value) { 710 mimemessage = value; 711 } 712 713 719 public void setSession(String value) { 720 session = value; 721 } 722 723 731 public void setAuthenticate(String value) { 732 authentication = new Boolean (value).booleanValue(); 733 } 734 735 741 public void setType(String value) { 742 if (value.equalsIgnoreCase("html")) 743 type = "text/html"; 744 else 745 type = "text/plain"; 746 } 747 748 754 public void setCharset(String value) { 755 charset = value; 756 } 757 758 764 protected void addTo(String value) { 765 if (addTO.length() > 0) { 766 addTO.append(","); 767 } 768 addTO.append(value); 769 } 770 771 778 protected void addCc(String value) { 779 if (addCC.length() > 0) { 780 addCC.append(","); 781 } 782 addCC.append(value); 783 } 784 785 792 protected void addBcc(String value) { 793 if (addBCC.length() > 0) { 794 addBCC.append(","); 795 } 796 addBCC.append(value); 797 } 798 799 804 protected void resetTo(String value) { 805 addTO.setLength(0); 806 addTO.append(value); 807 } 808 809 814 protected void resetCc(String value) { 815 addCC.setLength(0); 816 addCC.append(value); 817 } 818 819 824 protected void resetBcc(String value) { 825 addBCC.setLength(0); 826 addBCC.append(value); 827 } 828 829 834 protected void resetFrom(String value) { 835 reset_from = value; 836 } 837 838 843 protected void resetReplyTo(String value) { 844 reset_replyto = value; 845 } 846 847 852 protected void resetSubject(String value) { 853 reset_subject = value; 854 } 855 856 861 protected void resetServer(String value) { 862 reset_server = value; 863 } 864 865 870 protected void resetPort(String value) { 871 reset_port = value; 872 } 873 874 } 875 876 | Popular Tags |