1 package org.apache.fulcrum.template; 2 3 56 57 import java.io.StringWriter ; 58 import java.util.ArrayList ; 59 import java.util.Iterator ; 60 import java.util.List ; 61 62 import javax.mail.internet.InternetAddress ; 63 64 import org.apache.commons.lang.WordUtils; 65 import org.apache.commons.mail.SimpleEmail; 66 import org.apache.fulcrum.ServiceException; 67 68 137 public class TemplateEmail 138 { 139 140 private String toName = null; 141 142 143 private String toEmail = null; 144 145 146 private String fromName = null; 147 148 149 private String fromEmail = null; 150 151 152 private String ccName = null; 153 154 155 private String ccEmail = null; 156 157 158 private String subject = null; 159 160 161 private List toList = null; 162 163 164 private List ccList = null; 165 166 167 private List replyToList = null; 168 169 private List headersList; 170 171 172 private int wordWrap = 0; 173 174 178 private String template = null; 179 180 183 private TemplateContext context = null; 184 185 188 private String charset = null; 189 190 193 public TemplateEmail() 194 { 195 this(null); 196 } 197 198 201 public TemplateEmail(TemplateContext context) 202 { 203 this.context = context; 204 } 205 206 public String getCharSet() 207 { 208 return charset; 209 } 210 211 public String getTemplate() 212 { 213 return template; 214 } 215 216 public int getWordWrap() 217 { 218 return wordWrap; 219 } 220 221 public List getToList() 222 { 223 return toList == null ? toList = new ArrayList () : toList; 224 } 225 226 public void setToList(List v) 227 { 228 toList = v; 229 } 230 231 public List getCCList() 232 { 233 return ccList == null ? ccList = new ArrayList () : ccList; 234 } 235 236 public List getReplyToList() 237 { 238 return replyToList == null ? replyToList = new ArrayList (3) : replyToList; 239 } 240 241 public List getHeadersList() 242 { 243 return headersList == null ? headersList = new ArrayList (3) : headersList; 244 } 245 246 public String getToName() 247 { 248 return toName; 249 } 250 251 public String getToEmail() 252 { 253 return toEmail; 254 } 255 256 public String getFromName() 257 { 258 return fromName; 259 } 260 261 public String getFromEmail() 262 { 263 return fromEmail; 264 } 265 266 public String getCCName() 267 { 268 return ccName; 269 } 270 271 public String getCCEmail() 272 { 273 return ccEmail; 274 } 275 276 281 public void addTo(String email, String name) 282 throws Exception 283 { 284 try 285 { 286 if ((name == null) || (name.trim().equals(""))) 287 { 288 name = email; 289 } 290 291 if (getCharSet() != null) 292 { 293 getToList().add(new InternetAddress (email, name, getCharSet())); 294 } 295 else 296 { 297 getToList().add(new InternetAddress (email, name)); 298 } 299 } 300 catch (Exception e) 301 { 302 throw new Exception ("Cannot add 'To' recipient: " + e); 303 } 304 } 305 306 312 public void addCc(String email, String name) 313 throws Exception 314 { 315 try 316 { 317 if ((name == null) || (name.trim().equals(""))) 318 { 319 name = email; 320 } 321 322 if (getCharSet() != null) 323 { 324 getCCList().add(new InternetAddress (email, name, getCharSet())); 325 } 326 else 327 { 328 getCCList().add(new InternetAddress (email, name)); 329 } 330 } 331 catch (Exception e) 332 { 333 throw new Exception ("Cannot add 'CC' recipient: " + e); 334 } 335 } 336 337 338 344 public void setCharset(String charset) 345 { 346 this.charset = charset; 347 } 348 349 356 public TemplateEmail setTo(String to, 357 String email) 358 { 359 this.toName = to; 360 this.toEmail = email; 361 return (this); 362 } 363 364 371 public TemplateEmail setFrom(String from, 372 String email) 373 { 374 this.fromName = from; 375 this.fromEmail = email; 376 return (this); 377 } 378 379 386 public TemplateEmail setCC(String cc, 387 String email) 388 { 389 this.ccName = cc; 390 this.ccEmail = email; 391 return (this); 392 } 393 394 395 403 public TemplateEmail addReplyTo( String name, String email) 404 { 405 String [] emailName = new String [2]; 406 emailName[0] = email; 407 emailName[1] = name; 408 getReplyToList().add(emailName); 409 return this; 410 } 411 412 public TemplateEmail addHeader(String name, String value) 413 { 414 String [] pair = new String [2]; 415 pair[0] = name; 416 pair[1] = value; 417 getHeadersList().add(pair); 418 return this; 419 } 420 421 public String getSubject() 422 { 423 return this.subject; 424 } 425 426 432 public TemplateEmail setSubject(String subject) 433 { 434 if (subject == null) 435 { 436 this.subject = ""; 437 } 438 else 439 { 440 this.subject = subject; 441 } 442 return (this); 443 } 444 445 452 public TemplateEmail setTemplate(String template) 453 { 454 this.template = template; 455 return (this); 456 } 457 458 469 public TemplateEmail setWordWrap(int wordWrap) 470 { 471 this.wordWrap = wordWrap; 472 return (this); 473 } 474 475 482 public TemplateEmail setContext(TemplateContext context) 483 { 484 this.context = context; 485 return (this); 486 } 487 488 494 public TemplateContext getContext() 495 { 496 return this.context; 497 } 498 499 503 public void send() 504 throws Exception 505 { 506 if (getToEmail() == null || getToName() == null) 507 { 508 throw new Exception ("Must set a To:"); 509 } 510 511 setToList(null); 514 addTo(toEmail, toName); 515 sendMultiple(); 516 } 517 518 protected String handleRequest() 519 throws ServiceException 520 { 521 StringWriter sw = new StringWriter (); 522 TurbineTemplate.handleRequest(getContext(),getTemplate(),sw); 523 return sw.toString(); 524 } 525 526 529 public void sendMultiple() 530 throws Exception 531 { 532 if (getToList() == null || getToList().isEmpty()) 533 { 534 throw new Exception ("Must set a To:"); 535 } 536 537 String body = handleRequest(); 539 540 if (getWordWrap() > 0) 542 { 543 body = WordUtils.wrap(body, getWordWrap()); 544 } 545 546 SimpleEmail se = new SimpleEmail(); 547 if (getCharSet() != null) 548 { 549 se.setCharset(getCharSet()); 550 } 551 se.setFrom(getFromEmail(), getFromName()); 552 se.setTo(getToList()); 553 if (getCCList() != null && !getCCList().isEmpty()) 554 { 555 se.setCc(getCCList()); 556 } 557 addReplyTo(se); 558 se.setSubject(getSubject()); 559 se.setMsg(body); 560 561 if (getHeadersList() != null) 562 { 563 for (Iterator i = getHeadersList().iterator();i.hasNext();) 564 { 565 String [] pair = (String [])i.next(); 566 se.addHeader(pair[0], pair[1]); 567 } 568 } 569 570 se.send(); 571 } 572 573 579 private void addReplyTo(SimpleEmail se) 580 throws Exception 581 { 582 if (getReplyToList() != null) 583 { 584 for (Iterator i = getReplyToList().iterator();i.hasNext();) 585 { 586 String [] emailName = (String [])i.next(); 587 se.addReplyTo(emailName[0], emailName[1]); 588 } 589 } 590 } 591 592 598 public String toString() 599 { 600 try 601 { 602 send(); 603 } 604 catch (Exception e) 605 { 606 } 608 return ""; 609 } 610 } 611 | Popular Tags |