1 16 package org.apache.cocoon.mail; 17 18 import org.apache.avalon.framework.CascadingRuntimeException; 19 import org.apache.avalon.framework.activity.Initializable; 20 import org.apache.avalon.framework.component.Component; 21 import org.apache.avalon.framework.configuration.Configurable; 22 import org.apache.avalon.framework.configuration.Configuration; 23 import org.apache.avalon.framework.configuration.ConfigurationException; 24 import org.apache.avalon.framework.logger.AbstractLogEnabled; 25 import org.apache.avalon.framework.service.ServiceException; 26 import org.apache.avalon.framework.service.ServiceManager; 27 import org.apache.avalon.framework.service.Serviceable; 28 29 import org.apache.cocoon.mail.datasource.FilePartDataSource; 30 import org.apache.cocoon.mail.datasource.SourceDataSource; 31 import org.apache.cocoon.servlet.multipart.Part; 32 33 import org.apache.excalibur.source.Source; 34 import org.apache.excalibur.source.SourceResolver; 35 36 import javax.activation.DataHandler ; 37 import javax.activation.DataSource ; 38 import javax.mail.Authenticator ; 39 import javax.mail.BodyPart ; 40 import javax.mail.Message.RecipientType; 41 import javax.mail.MessagingException ; 42 import javax.mail.Multipart ; 43 import javax.mail.PasswordAuthentication ; 44 import javax.mail.Session ; 45 import javax.mail.Transport ; 46 import javax.mail.internet.AddressException ; 47 import javax.mail.internet.InternetAddress ; 48 import javax.mail.internet.MimeBodyPart ; 49 import javax.mail.internet.MimeMessage ; 50 import javax.mail.internet.MimeMultipart ; 51 import java.io.IOException ; 52 import java.net.MalformedURLException ; 53 import java.util.ArrayList ; 54 import java.util.Date ; 55 import java.util.Iterator ; 56 import java.util.List ; 57 import java.util.Properties ; 58 59 75 public class MailMessageSender 76 extends AbstractLogEnabled 77 implements MailSender, Configurable, Serviceable, Initializable, Component { 78 79 private ServiceManager manager; 80 81 private Session session; 82 83 private String smtpHost; 84 private String smtpUser; 85 private String smtpPswd; 86 87 private String from; 88 private String to; 89 private String replyTo; 90 private String cc; 91 private String bcc; 92 private String subject; 93 private String charset; 94 private String src; 95 private String srcMimeType; 96 private String body; 97 private List attachments; 98 private Exception exception; 99 100 105 private static class Attachment { 106 private Object obj = null; 107 private String type = null; 108 private String name = null; 109 protected boolean isURL = false; 110 111 115 public Attachment(Object obj) { 116 this(obj, null, null); 117 } 118 119 125 public Attachment(Object obj, String type, String name) { 126 this(obj, type, name, false); 127 } 128 129 136 public Attachment(Object obj, String type, String name, boolean isURI) { 137 this.obj = obj; 138 this.type = type; 139 this.name = name; 140 this.isURL = isURI; 141 if (isNullOrEmpty(this.type)) 142 this.type = null; 143 if (isNullOrEmpty(this.name)) 144 this.name = null; 145 } 146 147 152 private boolean isNullOrEmpty(String str) { 153 return (str == null || "".equals(str) || "null".equals(str)); 154 } 155 156 160 public boolean isURL() { 161 return this.isURL; 162 } 163 164 169 public String getName(String name) { 170 return (this.name == null ? name : this.name); 171 } 172 173 178 public String getType(String type) { 179 return (this.type == null ? type : this.type); 180 } 181 182 185 public Object getObject() { 186 return this.obj; 187 } 188 } 189 190 public MailMessageSender() { 191 } 192 193 200 public MailMessageSender(String smtpHost) { 201 smtpHost = smtpHost.trim(); 202 setSmtpHost(smtpHost); 203 initialize(); 204 } 205 206 public void service(ServiceManager manager) { 207 this.manager = manager; 208 } 209 210 213 public void configure(Configuration config) throws ConfigurationException { 214 this.smtpHost = config.getChild("smtp-host").getValue(null); 215 this.smtpUser = config.getChild("smtp-user").getValue(null); 216 this.smtpPswd = config.getChild("smtp-password").getValue(null); 217 } 218 219 222 public void initialize() { 223 initSession(); 224 this.attachments = new ArrayList (); 225 } 226 227 private void initSession() { 228 Properties properties = new Properties (); 229 if (smtpHost == null || smtpHost.equals("") || smtpHost.equals("null")) { 230 properties.put("mail.smtp.host", "127.0.0.1"); 231 } else { 232 properties.put("mail.smtp.host", smtpHost); 233 } 234 235 if (smtpUser == null || smtpUser.equals("") || smtpUser.equals("null")) { 236 this.session = Session.getInstance(properties); 237 } else { 238 properties.put("mail.smtp.auth", "true"); 239 this.session = Session.getInstance(properties, new Authenticator () { 240 protected PasswordAuthentication getPasswordAuthentication() { 241 return new PasswordAuthentication (smtpUser, smtpPswd); 242 } 243 }); 244 } 245 } 246 247 250 public void setSmtpHost(String hostname) { 251 this.smtpHost = hostname; 252 initSession(); 253 } 254 255 public void setSmtpHost(String hostname, String username, String password) { 256 this.smtpUser = username; 257 this.smtpPswd = password; 258 setSmtpHost(hostname); 259 } 260 261 262 267 public void send() throws AddressException , MessagingException { 268 SourceResolver resolver = null; 269 try { 270 resolver = (SourceResolver) this.manager.lookup(SourceResolver.ROLE); 271 doSend(resolver); 272 } catch(ServiceException se) { 273 throw new CascadingRuntimeException("Cannot get Source Resolver to send mail", se); 274 } finally { 275 this.manager.release(resolver); 276 } 277 } 278 279 285 public void send(org.apache.cocoon.environment.SourceResolver resolver) 286 throws AddressException , MessagingException { 287 doSend(resolver); 289 } 290 291 private void doSend(SourceResolver resolver) 292 throws AddressException , MessagingException { 293 final List sourcesList = new ArrayList (); 294 295 final MimeMessage message = new MimeMessage (this.session); 296 297 if (this.from == null) { 298 throw new AddressException ("No from address"); 299 } else { 300 try { 301 message.setFrom(new InternetAddress (this.from)); 302 } catch (AddressException e) { 303 throw new AddressException ("Invalid from address: " + this.from + ": " + 304 e.getMessage()); 305 } 306 } 307 308 if (this.to == null) { 309 throw new AddressException ("no to address"); 310 } else { 311 try { 312 message.setRecipients(RecipientType.TO, 313 InternetAddress.parse(this.to)); 314 } catch (AddressException e) { 315 throw new AddressException ("Invalid to address: " + this.to + ": " + 316 e.getMessage()); 317 } 318 } 319 320 if (this.replyTo != null) { 321 try { 322 message.setReplyTo(InternetAddress.parse(this.replyTo)); 323 } catch (AddressException e) { 324 throw new AddressException ("Invalid replyTo address: " + this.replyTo + ": " + 325 e.getMessage()); 326 } 327 } 328 329 if (this.cc != null) { 330 try { 331 message.setRecipients(RecipientType.CC, 332 InternetAddress.parse(this.cc)); 333 } catch (AddressException e) { 334 throw new AddressException ("Invalid cc address: " + this.cc + ": " + 335 e.getMessage()); 336 } 337 } 338 339 if (this.bcc != null) { 340 try { 341 message.setRecipients(RecipientType.BCC, 342 InternetAddress.parse(this.bcc)); 343 } catch (AddressException e) { 344 throw new AddressException ("Invalid bcc address: " + this.bcc + ": " + 345 e.getMessage()); 346 } 347 } 348 349 if (this.subject != null) { 350 message.setSubject(this.subject); 351 } 352 353 message.setSentDate(new Date ()); 354 355 Attachment a = null; 356 try { 357 if (this.attachments.isEmpty()) { 358 if (this.src != null) { 359 DataSource ds = null; 360 361 Source source = resolver.resolveURI(this.src); 362 sourcesList.add(source); 363 if (source.exists()) { 364 ds = 365 new SourceDataSource( 366 source, 367 (this.srcMimeType == null 368 ? source.getMimeType() 369 : this.srcMimeType), 370 this.src.substring(this.src.lastIndexOf('/') + 1)); 371 } 372 373 message.setDataHandler(new DataHandler (ds)); 374 375 } else if (this.body != null) { 376 if (this.charset != null) { 377 message.setText(this.body, this.charset); 378 } else { 379 message.setText(this.body); 380 } 381 } 382 } else { 383 Multipart multipart = new MimeMultipart (); 384 BodyPart bodypart = new MimeBodyPart (); 385 multipart.addBodyPart(bodypart); 386 message.setContent(multipart); 387 388 if (this.src != null) { 389 DataSource ds = null; 390 391 Source source = resolver.resolveURI(this.src); 392 sourcesList.add(source); 393 if (source.exists()) { 394 ds = 395 new SourceDataSource( 396 source, 397 (this.srcMimeType == null 398 ? source.getMimeType() 399 : this.srcMimeType), 400 this.src.substring(this.src.lastIndexOf('/') + 1)); 401 } 402 403 bodypart.setDataHandler(new DataHandler (ds)); 404 bodypart.setFileName(ds.getName()); 405 406 } else if (this.body != null) { 407 bodypart.setText(this.body); 408 } 409 410 for (Iterator i = this.attachments.iterator(); i.hasNext();) { 411 a = (Attachment) i.next(); 412 DataSource ds = null; 413 if (a.isURL) { 414 String name = (String ) a.getObject(); 415 Source src = resolver.resolveURI(name); 416 sourcesList.add(src); 417 if (src.exists()) { 418 ds = 419 new SourceDataSource( 420 src, 421 a.getType(src.getMimeType()), 422 a.getName(name.substring(name.lastIndexOf('/') + 1))); 423 } 424 } else { 425 if (a.getObject() instanceof Part) { 426 Part part = (Part) a.getObject(); 427 ds = 428 new FilePartDataSource( 429 part, 430 a.getType(part.getMimeType()), 431 a.getName(part.getUploadName())); 432 } else { 433 throw new AddressException ("Not yet supported: " + a.getObject()); 435 } 436 } 437 438 bodypart = new MimeBodyPart (); 439 bodypart.setDataHandler(new DataHandler (ds)); 440 bodypart.setFileName(ds.getName()); 441 multipart.addBodyPart(bodypart); 442 } 443 } 444 445 message.saveChanges(); 446 Transport.send(message); 447 } catch (MessagingException me) { 448 throw new MessagingException (me.getMessage()); 449 } catch (MalformedURLException e) { 450 throw new AddressException ("Malformed attachment URL: " + 451 a.getObject() + " error " + e.getMessage()); 452 } catch (IOException e) { 453 throw new AddressException ("IOException accessing attachment URL: " + 454 a.getObject() + " error " + e.getMessage()); 455 } finally { 456 if (sourcesList != null) { 457 for (Iterator j = sourcesList.iterator(); j.hasNext();) { 458 resolver.release((Source) j.next()); 459 } 460 } 461 } 462 } 463 464 469 public boolean sendIt() { 470 this.exception = null; 471 try { 472 send(); 473 } catch (Exception e) { 474 this.exception = e; 475 } 476 return exception == null; 477 } 478 479 487 public boolean sendIt(org.apache.cocoon.environment.SourceResolver resolver) { 488 this.exception = null; 489 try { 490 send(resolver); 491 } catch (Exception e) { 492 this.exception = e; 493 } 494 return exception == null; 495 } 496 497 503 public Exception getException() { 504 return this.exception; 505 } 506 507 508 512 public void setFrom(String from) { 513 if (!("".equals(from) || "null".equals(from))) { 514 this.from = from.trim(); 515 } 516 } 517 518 526 public void setTo(String to) { 527 if (!("".equals(to) || "null".equals(to))) { 528 this.to = to.trim(); 529 } 530 } 531 532 540 public void setReplyTo(String replyTo) { 541 if (!("".equals(replyTo) || "null".equals(replyTo))) { 542 this.replyTo = replyTo.trim(); 543 } 544 } 545 546 554 public void setCc(String cc) { 555 if (!("".equals(cc) || "null".equals(cc))) { 556 this.cc = cc.trim(); 557 } 558 } 559 560 568 public void setBcc(String bcc) { 569 if (!("".equals(bcc) || "null".equals(bcc))) { 570 this.bcc = bcc.trim(); 571 } 572 } 573 574 579 public void setCharset(String charset) { 580 if (!("".equals(charset) || "null".equals(charset))) { 581 this.charset = charset.trim(); 582 } 583 } 584 585 589 public void setSubject(String subject) { 590 if (!("".equals(subject) || "null".equals(subject))) { 591 this.subject = subject; 592 } 593 } 594 595 602 public void setBody(String body) { 603 if (!("".equals(body) || "null".equals(body))) { 604 this.body = body; 605 } 606 } 607 608 615 public void setBodyFromSrc(String src) { 616 if (!("".equals(src) || "null".equals(src))) { 617 this.src = src; 618 } 619 } 620 621 625 public void setBodyFromSrcMimeType(String srcMimeType) { 626 if (!("".equals(srcMimeType) || "null".equals(srcMimeType))) { 627 this.srcMimeType = srcMimeType; 628 } 629 } 630 631 639 public void addAttachment(Object attachment) { 640 if (attachment != null) { 641 attachments.add(new Attachment(attachment)); 642 } 643 } 644 645 655 public void addAttachment(Object attachment, String type, String name) { 656 if (attachment != null) { 657 attachments.add(new Attachment(attachment, type, name)); 658 } 659 } 660 661 669 public void addAttachmentURL(String url) { 670 if (url != null) { 671 attachments.add(new Attachment(url, null, null, true)); 672 } 673 } 674 675 685 public void addAttachmentURL(String url, String type, String name) { 686 if (url != null) { 687 attachments.add(new Attachment(url, type, name, true)); 688 } 689 } 690 } 691 | Popular Tags |