1 package org.apache.turbine.util.mail; 2 3 18 19 import java.io.UnsupportedEncodingException ; 20 import java.util.Enumeration ; 21 import java.util.Hashtable ; 22 import java.util.Properties ; 23 import java.util.StringTokenizer ; 24 import java.util.Vector ; 25 import javax.mail.Message ; 26 import javax.mail.MessagingException ; 27 import javax.mail.Session ; 28 import javax.mail.Transport ; 29 import javax.mail.internet.AddressException ; 30 import javax.mail.internet.InternetAddress ; 31 import javax.mail.internet.MimeMessage ; 32 33 67 public class MailMessage 68 { 69 72 protected String host; 73 74 81 protected Hashtable headers; 82 83 86 protected InternetAddress from; 87 88 91 protected InternetAddress [] replyTo; 92 93 96 protected InternetAddress [] to; 97 98 102 protected InternetAddress [] cc; 103 104 108 protected InternetAddress [] bcc; 109 110 113 protected String subject; 114 115 118 protected String body; 119 120 123 protected boolean debug; 124 125 132 public MailMessage() 133 { 134 this(null, null, null, null, null, "", "", false); 135 } 136 137 147 public MailMessage(String h, 148 String t, 149 String f, 150 String s, 151 String b) 152 { 153 this(h, t, null, null, f, s, b, false); 154 } 155 156 170 public MailMessage(String h, 171 String t, 172 String c, 173 String bc, 174 String f, 175 String s, 176 String b, 177 boolean d) 178 { 179 host = h; 180 to = (t == null ? null : parseAddressField(t)); 181 cc = (cc == null ? null : parseAddressField(c)); 182 bcc = (bc == null ? null : parseAddressField(bc)); 183 from = (f == null ? null : parseInternetAddress(f)); 184 subject = s; 185 body = b; 186 debug = d; 187 } 188 189 195 public void addHeader(String name, 196 String value) 197 { 198 if (headers == null) 199 { 200 headers = new Hashtable (); 201 } 202 headers.put(name, value); 203 } 204 205 211 public static InternetAddress [] parseAddressField(String str) 212 { 213 String [] addressList; 214 if (str.indexOf(",") != -1) 215 { 216 Vector v = new Vector (); 217 StringTokenizer st = new StringTokenizer (str, ",", false); 218 while (st.hasMoreTokens()) 219 { 220 v.addElement(st.nextToken()); 221 } 222 addressList = new String [v.size()]; 223 for (int i = 0; i < v.size(); i++) 224 { 225 addressList[i] = (String ) v.elementAt(i); 226 } 227 } 228 else 229 { 230 addressList = new String [1]; 231 addressList[0] = str; 232 } 233 return parseAddressList(addressList); 234 } 235 236 242 public static InternetAddress [] parseAddressList(String [] aList) 243 { 244 InternetAddress [] ia = new InternetAddress [aList.length]; 245 246 for (int i = 0; i < aList.length; i++) 247 { 248 ia[i] = parseInternetAddress(aList[i]); 249 } 250 251 return ia; 252 253 } 254 255 261 public static void parseHeader(String str, 262 Hashtable headers) 263 { 264 String name = null; 265 String value = null; 266 267 str = str.trim(); 268 int sp = str.lastIndexOf(":"); 269 name = str.substring(0, sp); 270 value = (str.substring(sp + 1)).trim(); 271 272 headers.put(name, value); 273 } 274 275 281 public static Hashtable parseHeaderField(String str) 282 { 283 String [] headerList; 284 if (str.indexOf(",") != -1) 285 { 286 Vector v = new Vector (); 287 StringTokenizer st = new StringTokenizer (str, ",", false); 288 while (st.hasMoreTokens()) 289 { 290 v.addElement(st.nextToken()); 291 } 292 headerList = new String [v.size()]; 293 for (int i = 0; i < v.size(); i++) 294 { 295 headerList[i] = (String ) v.elementAt(i); 296 } 297 } 298 else 299 { 300 headerList = new String [1]; 301 headerList[0] = str; 302 } 303 return parseHeaderList(headerList); 304 } 305 306 312 public static Hashtable parseHeaderList(String [] hList) 313 { 314 Hashtable headers = new Hashtable (); 315 316 for (int i = 0; i < hList.length; i++) 317 { 318 parseHeader(hList[i], headers); 320 } 321 322 return headers; 323 } 324 325 331 public static InternetAddress parseInternetAddress(String str) 332 { 333 String address = null; 334 String personal = null; 335 336 str = str.trim(); 337 if (str.indexOf(" ") == -1) 338 { 339 address = str; 340 } 341 else 342 { 343 int sp = str.lastIndexOf(" "); 344 address = str.substring(sp + 1); 345 personal = str.substring(0, sp); 346 } 347 return parseInternetAddress(address, personal); 348 } 349 350 357 public static InternetAddress parseInternetAddress(String address, 358 String personal) 359 { 360 InternetAddress ia = null; 361 try 362 { 363 ia = new InternetAddress (address); 364 365 if (personal != null) 366 { 367 ia.setPersonal(personal); 368 } 369 } 370 catch (AddressException e) 371 { 372 e.printStackTrace(); 373 System.out.println(); 374 } 375 catch (UnsupportedEncodingException e) 376 { 377 e.printStackTrace(); 378 System.out.println(); 379 } 380 381 return ia; 382 } 383 384 390 public boolean send() 391 { 392 Properties props = new Properties (); 394 props.put("mail.smtp.host", host); 395 396 Session session = Session.getInstance(props, null); 397 session.setDebug(debug); 398 399 try 400 { 401 Message msg = new MimeMessage (session); 403 404 msg.setFrom(from); 406 407 msg.setRecipients(Message.RecipientType.TO, to); 409 410 if (cc != null) 412 { 413 msg.setRecipients(Message.RecipientType.CC, cc); 414 } 415 416 if (bcc != null) 419 { 420 msg.setRecipients(Message.RecipientType.BCC, bcc); 421 } 422 423 if (replyTo != null) 426 { 427 msg.setReplyTo(replyTo); 428 } 429 430 msg.setSubject(subject); 432 433 msg.setText(body); 436 437 439 if (headers != null) 440 { 441 Enumeration e = headers.keys(); 442 while (e.hasMoreElements()) 443 { 444 String name = (String ) e.nextElement(); 445 String value = (String ) headers.get(name); 446 msg.addHeader(name, value); 447 } 448 } 449 450 Transport.send(msg); 452 } 453 catch (MessagingException mex) 454 { 455 mex.printStackTrace(); 456 Exception ex = null; 457 if ((ex = mex.getNextException()) != null) 458 { 459 ex.printStackTrace(); 460 } 461 return false; 462 } 463 return true; 464 } 465 466 472 public void setBcc(InternetAddress [] bc) 473 { 474 bcc = bc; 475 } 476 477 483 public void setBcc(String bc) 484 { 485 bcc = parseAddressField(bc); 486 } 487 488 493 public void setBody(String b) 494 { 495 body = b; 496 } 497 498 504 public void setCc(InternetAddress [] c) 505 { 506 cc = c; 507 } 508 509 515 public void setCc(String c) 516 { 517 cc = parseAddressField(c); 518 } 519 520 525 public void setDebug(String str) 526 { 527 if (str.equals("1")) 528 { 529 debug = true; 530 } 531 else if (str.equals("0")) 532 { 533 debug = false; 534 } 535 else 536 { 537 debug = new Boolean (str).booleanValue(); 538 } 539 } 540 541 546 public void setDebug(boolean d) 547 { 548 debug = d; 549 } 550 551 557 public void setFrom(String f) 558 { 559 from = parseInternetAddress(f); 560 } 561 562 568 public void setFrom(InternetAddress f) 569 { 570 from = f; 571 } 572 573 582 public void setHeaders(String h) 583 { 584 headers = parseHeaderField(h); 585 } 586 587 596 public void setHeaders(Hashtable h) 597 { 598 headers = h; 599 } 600 601 606 public void setHost(String h) 607 { 608 host = h; 609 } 610 611 617 public void setReplyTo(InternetAddress [] rt) 618 { 619 replyTo = rt; 620 } 621 622 628 public void setReplyTo(String rp) 629 { 630 replyTo = parseAddressField(rp); 631 } 632 633 638 public void setSubject(String s) 639 { 640 subject = s; 641 } 642 643 649 public void setTo(InternetAddress [] t) 650 { 651 to = t; 652 } 653 654 660 public void setTo(String t) 661 { 662 to = parseAddressField(t); 663 } 664 } 665 | Popular Tags |