View Javadoc
1 package org.apache.turbine.util.mail; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Turbine" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * "Apache Turbine", nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>;. 55 */ 56 57 import java.io.UnsupportedEncodingException; 58 import java.util.Enumeration; 59 import java.util.Hashtable; 60 import java.util.Properties; 61 import java.util.StringTokenizer; 62 import java.util.Vector; 63 import javax.mail.Message; 64 import javax.mail.MessagingException; 65 import javax.mail.Session; 66 import javax.mail.Transport; 67 import javax.mail.internet.AddressException; 68 import javax.mail.internet.InternetAddress; 69 import javax.mail.internet.MimeMessage; 70 71 /*** 72 * Creates a very simple text/plain message and sends it. 73 * 74 * <p>MailMessage creates a very simple text/plain message and sends 75 * it. It can be used like this:<br> 76 * <pre> 77 * MailMessage sm = new MailMessage("mail.domain.net", 78 * "toYou@domain.net", 79 * "fromMe@domain", 80 * "this is the subject", 81 * "this is the body"); 82 * </pre> 83 * 84 * Another example is:<br> 85 * <pre> 86 * MailMessage sm = new MailMessage(); 87 * sm.setHost("mail.domain.net"); 88 * sm.setHeaders("X-Mailer: Sendmail class, X-Priority: 1(Highest)"); 89 * sm.setFrom("Net Freak1 user1@domain.com"); 90 * sm.setReplyTo("Net Freak8 user8@domain.com"); 91 * sm.setTo("Net Freak2 user2@domain.com, Net Freak3 user3@domain.com"); 92 * sm.setCc("Net Freak4 user4@domain.com, Net Freak5 user5@domain.com"); 93 * sm.setBcc("Net Freak6 user6@domain.com, Net Freak7 user7@domain.com"); 94 * sm.setSubject("New Sendmail Test"); 95 * sm.setBody("Test message from Sendmail class, more features to be added. 96 * Like multipart messages, html, binary files..."); 97 * sm.setDebug(true); 98 * sm.send(); 99 * </pre> 100 * 101 * @author <a href="mailto:david@i2a.com">David Duddleston</a> 102 * @version $Id: MailMessage.java,v 1.2 2002/07/11 16:53:20 mpoeschl Exp $ 103 */ 104 public class MailMessage 105 { 106 /*** 107 * The host name of the mail server to use. 108 */ 109 protected String host; 110 111 /*** 112 * Used to specify the mail headers. Example: 113 * 114 * X-Mailer: Sendmail, X-Priority: 1(highest) 115 * or 2(high) 3(normal) 4(low) and 5(lowest) 116 * Disposition-Notification-To: returnR user@domain.net 117 */ 118 protected Hashtable headers; 119 120 /*** 121 * The email address that the mail is being sent from. 122 */ 123 protected InternetAddress from; 124 125 /*** 126 * The email address used for replies to this message. 127 */ 128 protected InternetAddress[] replyTo; 129 130 /*** 131 * The email address or addresses that the email is being sent to. 132 */ 133 protected InternetAddress[] to; 134 135 /*** 136 * The email address or addresses that the email is being 137 * carbon-copied to. 138 */ 139 protected InternetAddress[] cc; 140 141 /*** 142 * The email address or addresses that the email is being 143 * blind-carbon-copied to. 144 */ 145 protected InternetAddress[] bcc; 146 147 /*** 148 * The subject of the email message. 149 */ 150 protected String subject; 151 152 /*** 153 * The body of the email message. 154 */ 155 protected String body; 156 157 /*** 158 * Displays debug information when true. 159 */ 160 protected boolean debug; 161 162 /*** 163 * Default constructor. Must use the setHost, setTo, and other 164 * set functions to properly send an email. <b>host</b>, 165 * <b>to</b>, <b>cc</b>, <b>bcc</b>, and <b>from</b> are set to 166 * null. <b>subject</b>, and <b>body</b> are set to empty 167 * strings. <b>debug</b> is set to false. 168 */ 169 public MailMessage() 170 { 171 this(null, null, null, null, null, "", "", false); 172 } 173 174 /*** 175 * Constructor used to specify <b>host</b>, <b>to</b>, 176 * <b>from</b>, <b>subject</b>, and <b>body</b>. 177 * 178 * @param h A String with the host. 179 * @param t A String with the TO. 180 * @param f A String with the FROM. 181 * @param s A String with the SUBJECT. 182 * @param b A String with the BODY. 183 */ 184 public MailMessage(String h, 185 String t, 186 String f, 187 String s, 188 String b) 189 { 190 this(h, t, null, null, f, s, b, false); 191 } 192 193 /*** 194 * Constructor used to specify <b>host</b>, <b>to</b>, <b>cc</b>, 195 * <b>bcc</b>, <b>from</b>, <b>subject</b>, <b>body</b>, and 196 * <b>debug</b>. 197 * 198 * @param h A String with the host. 199 * @param t A String with the TO. 200 * @param c A String with the CC. 201 * @param bc A String with the BCC. 202 * @param f A String with the FROM. 203 * @param s A String with the SUBJECT. 204 * @param b A String with the BODY. 205 * @param d True if debugging is wanted. 206 */ 207 public MailMessage(String h, 208 String t, 209 String c, 210 String bc, 211 String f, 212 String s, 213 String b, 214 boolean d) 215 { 216 host = h; 217 to = (t == null?null:parseAddressField(t)); 218 cc = (cc == null?null:parseAddressField(c)); 219 bcc = (bc == null?null:parseAddressField(bc)); 220 from = (f == null?null:parseInternetAddress(f)); 221 subject = s; 222 body = b; 223 debug = d; 224 } 225 226 /*** 227 * Adds a header (name, value) to the headers Hashtable. 228 * 229 * @param name A String with the name. 230 * @param value A String with the value. 231 */ 232 public void addHeader(String name, 233 String value) 234 { 235 if (headers == null) 236 { 237 headers = new Hashtable(); 238 } 239 headers.put(name,value); 240 } 241 242 /*** 243 * Parse an address field. 244 * 245 * @param str A String with the address. 246 * @return An InternetAddress[]. 247 */ 248 public static InternetAddress[] parseAddressField(String str) 249 { 250 String[] addressList; 251 if (str.indexOf(",") != -1) 252 { 253 Vector v = new Vector(); 254 StringTokenizer st = new StringTokenizer(str, ",", false); 255 while (st.hasMoreTokens()) 256 { 257 v.addElement(st.nextToken()); 258 } 259 addressList = new String[v.size()]; 260 for (int i = 0; i < v.size(); i++) 261 { 262 addressList[i] = (String)v.elementAt(i); 263 } 264 } 265 else 266 { 267 addressList = new String[1]; 268 addressList[0] = str; 269 } 270 return parseAddressList(addressList); 271 } 272 273 /*** 274 * Parse an address list. 275 * 276 * @param aList A String[] with the addresses. 277 * @return An InternetAddress[]. 278 */ 279 public static InternetAddress[] parseAddressList(String[] aList) 280 { 281 InternetAddress[] ia = new InternetAddress[aList.length]; 282 283 for (int i = 0; i < aList.length; i++) 284 { 285 ia[i] = parseInternetAddress(aList[i]); 286 } 287 288 return ia; 289 290 } 291 292 /*** 293 * Parse a header. 294 * 295 * @param str A String with the header. 296 * @param headers A Hashtable with the current headers. 297 */ 298 public static void parseHeader(String str, 299 Hashtable headers) 300 { 301 String name = null; 302 String value = null; 303 304 str = str.trim(); 305 int sp = str.lastIndexOf(":"); 306 name = str.substring(0, sp); 307 value = (str.substring(sp + 1)).trim(); 308 309 headers.put(name, value); 310 } 311 312 /*** 313 * Parse a header field. 314 * 315 * @param str A String with the header field. 316 * @return A Hashtable with the parsed headers. 317 */ 318 public static Hashtable parseHeaderField(String str) 319 { 320 String[] headerList; 321 if (str.indexOf(",") != -1) 322 { 323 Vector v = new Vector(); 324 StringTokenizer st = new StringTokenizer(str, ",", false); 325 while( st.hasMoreTokens() ) 326 { 327 v.addElement(st.nextToken()); 328 } 329 headerList = new String[v.size()]; 330 for (int i = 0; i < v.size(); i++) 331 { 332 headerList[i] = (String) v.elementAt(i); 333 } 334 } 335 else 336 { 337 headerList = new String[1]; 338 headerList[0] = str; 339 } 340 return parseHeaderList(headerList); 341 } 342 343 /*** 344 * Parse a header list. 345 * 346 * @param hList A String[] with the headers. 347 * @return A Hashtable with the parsed headers. 348 */ 349 public static Hashtable parseHeaderList(String[] hList) 350 { 351 Hashtable headers = new Hashtable(); 352 353 for (int i = 0; i < hList.length; i++) 354 { 355 // headers.put("one", new Integer(1)); 356 parseHeader(hList[i], headers); 357 } 358 359 return headers; 360 } 361 362 /*** 363 * Parse an Internet address. 364 * 365 * @param str A String with the address. 366 * @return An InternetAddress. 367 */ 368 public static InternetAddress parseInternetAddress(String str) 369 { 370 String address = null; 371 String personal = null; 372 373 str = str.trim(); 374 if (str.indexOf(" ") == -1) 375 { 376 address = str; 377 } 378 else 379 { 380 int sp = str.lastIndexOf(" "); 381 address = str.substring(sp+1); 382 personal = str.substring(0, sp); 383 } 384 return parseInternetAddress(address, personal); 385 } 386 387 /*** 388 * Parse an Internet address. 389 * 390 * @param address A String with the address. 391 * @param personal A String. 392 * @return An InternetAddress. 393 */ 394 public static InternetAddress parseInternetAddress(String address, 395 String personal) 396 { 397 InternetAddress ia = null; 398 try 399 { 400 ia = new InternetAddress(address); 401 402 if (personal != null) 403 { 404 ia.setPersonal(personal); 405 } 406 } 407 catch (AddressException e) 408 { 409 e.printStackTrace(); 410 System.out.println(); 411 } 412 catch (UnsupportedEncodingException e) 413 { 414 e.printStackTrace(); 415 System.out.println(); 416 } 417 418 return ia; 419 } 420 421 /*** 422 * Send the message. The to, from, subject, host, and body should 423 * be set prior to using this method. 424 * 425 * @return True is message was sent. 426 */ 427 public boolean send() 428 { 429 // Create some properties and get the default Session. 430 Properties props = new Properties(); 431 props.put("mail.smtp.host", host); 432 433 Session session = Session.getInstance(props, null); 434 session.setDebug(debug); 435 436 try 437 { 438 // Create a message. 439 Message msg = new MimeMessage(session); 440 441 // Set the email address that the message is from. 442 msg.setFrom(from); 443 444 // Set the email addresses that the message is to. 445 msg.setRecipients(Message.RecipientType.TO, to); 446 447 // Set the email addresses that will be carbon-copied. 448 if (cc != null) 449 { 450 msg.setRecipients(Message.RecipientType.CC, cc); 451 } 452 453 // Set the email addresses that will be 454 // blind-carbon-copied. 455 if (bcc != null) 456 { 457 msg.setRecipients(Message.RecipientType.BCC, bcc); 458 } 459 460 // Set the email addresses that reply-to messages are 461 // sent. 462 if (replyTo != null) 463 { 464 msg.setReplyTo(replyTo); 465 } 466 467 // Set the subject of the email message. 468 msg.setSubject(subject); 469 470 // Set the body of the message. If the desired charset is 471 // known, use setText(text, charset). 472 msg.setText(body); 473 474 // msg.addHeader("X-Mailer", "com.i2a.util.mail.Sendmail"); 475 476 if (headers != null) 477 { 478 Enumeration e = headers.keys(); 479 while (e.hasMoreElements()) 480 { 481 String name = (String) e.nextElement(); 482 String value = (String) headers.get(name); 483 msg.addHeader(name, value); 484 } 485 } 486 487 // Send the message. 488 Transport.send(msg); 489 } 490 catch (MessagingException mex) 491 { 492 mex.printStackTrace(); 493 Exception ex = null; 494 if ((ex = mex.getNextException()) != null) 495 { 496 ex.printStackTrace(); 497 } 498 return false; 499 } 500 return true; 501 } 502 503 /*** 504 * Used to specify the email address that the mail is being 505 * blind-carbon-copied to. 506 * 507 * @param bc An InternetAddress[]. 508 */ 509 public void setBcc(InternetAddress[] bc) 510 { 511 bcc = bc; 512 } 513 514 /*** 515 * Used to specify the email address that the mail is being 516 * blind-carbon-copied to. 517 * 518 * @param bc A String. 519 */ 520 public void setBcc(String bc) 521 { 522 bcc = parseAddressField(bc); 523 } 524 525 /*** 526 * Used to specify the body of the email message. 527 * 528 * @param b A String. 529 */ 530 public void setBody(String b) 531 { 532 body = b; 533 } 534 535 /*** 536 * Used to specify the email address that the mail is being sent 537 * to. 538 * 539 * @param c An InternetAddress[]. 540 */ 541 public void setCc(InternetAddress[] c) 542 { 543 cc = c; 544 } 545 546 /*** 547 * Used to specify the email address that the mail is being 548 * carbon-copied to. 549 * 550 * @param c A String. 551 */ 552 public void setCc(String c) 553 { 554 cc = parseAddressField(c); 555 } 556 557 /*** 558 * Setting to true will enable the display of debug information. 559 * 560 * @param str A String. 561 */ 562 public void setDebug(String str) 563 { 564 if (str.equals("1")) 565 { 566 debug = true; 567 } 568 else if (str.equals("0")) 569 { 570 debug = false; 571 } 572 else 573 { 574 debug = new Boolean(str).booleanValue(); 575 } 576 } 577 578 /*** 579 * Setting to true will enable the display of debug information. 580 * 581 * @param d A boolean. 582 */ 583 public void setDebug(boolean d) 584 { 585 debug = d; 586 } 587 588 /*** 589 * Used to specify the email address that the mail is being sent 590 * from. 591 * 592 * @param f A String. 593 */ 594 public void setFrom(String f) 595 { 596 from = parseInternetAddress(f); 597 } 598 599 /*** 600 * Used to specify the email address that the mail is being sent 601 * from. 602 * 603 * @param f An InternetAddress. 604 */ 605 public void setFrom(InternetAddress f) 606 { 607 from = f; 608 } 609 610 /*** 611 * Used to specify the mail headers. Example: 612 * 613 * X-Mailer: Sendmail, X-Priority: 1(highest) 614 * or 2(high) 3(normal) 4(low) and 5(lowest) 615 * Disposition-Notification-To: returnR user@domain.net 616 * 617 * @param h A String. 618 */ 619 public void setHeaders(String h) 620 { 621 headers = parseHeaderField(h); 622 } 623 624 /*** 625 * Used to specify the mail headers. Example: 626 * 627 * X-Mailer: Sendmail, X-Priority: 1(highest) 628 * or 2(high) 3(normal) 4(low) and 5(lowest) 629 * Disposition-Notification-To: returnR user@domain.net 630 * 631 * @param h A Hashtable. 632 */ 633 public void setHeaders(Hashtable h) 634 { 635 headers = h; 636 } 637 638 /*** 639 * Used to specify the mail server host. 640 * 641 * @param h A String. 642 */ 643 public void setHost(String h) 644 { 645 host = h; 646 } 647 648 /*** 649 * Used to specify the email address that the mail is being sent 650 * from. 651 * 652 * @param rt An InternetAddress[]. 653 */ 654 public void setReplyTo(InternetAddress[] rt) 655 { 656 replyTo = rt; 657 } 658 659 /*** 660 * Used to specify the email address that the mail is being sent 661 * from. 662 * 663 * @param rp A String. 664 */ 665 public void setReplyTo(String rp) 666 { 667 replyTo = parseAddressField(rp); 668 } 669 670 /*** 671 * Used to specify the subject of the email message. 672 * 673 * @param s A String. 674 */ 675 public void setSubject(String s) 676 { 677 subject = s; 678 } 679 680 /*** 681 * Used to specify the email address that the mail is being sent 682 * to. 683 * 684 * @param t An InternetAddress[]. 685 */ 686 public void setTo(InternetAddress[] t) 687 { 688 to = t; 689 } 690 691 /*** 692 * Used to specify the email address that the mail is being sent 693 * to. 694 * 695 * @param t A String. 696 */ 697 public void setTo(String t) 698 { 699 to = parseAddressField(t); 700 } 701 }

This page was automatically generated by Maven