Example #1
0
  // -- Private methods
  private MimeMessage createMimeMessage(
      MailController controller, Session session, OptionService os) throws MessagingException {
    MimeMessage msg = new MimeMessage(session);
    setSender(msg, os);
    setReplyTo(controller, msg);
    setFrom(controller, msg, os);
    addRecipients(controller, msg);
    setSubject(controller, msg);

    Multipart content = new MimeMultipart();
    msg.setContent(content);

    /* Text */
    String body = controller.getBody();
    if (!StringUtil.isEmpty(body)) {
      MimeBodyPart part = new MimeBodyPart();
      part.setContent(body, controller.getContentType());
      content.addBodyPart(part);
    }

    /* Attachments */
    List<File> attachments = controller.getAttachments();
    if (!attachments.isEmpty()) {
      for (File attachment : attachments) {
        MimeBodyPart part = new MimeBodyPart();
        DataSource source = new FileDataSource(attachment);
        part.setDataHandler(new DataHandler(source));
        part.setFileName(attachment.getName());
        content.addBodyPart(part);
      }
    }

    return msg;
  }
Example #2
0
  // -- MailService overrides
  @Override
  public void send(MailController controller) throws MessagingException {
    if (!controller.hasRecipients()) {
      LOG.warning("Not recipients. No email to send");
      return;
    }

    /* log */

    /* Open the Session */
    Properties properties = new Properties();
    Session session = Session.getInstance(properties, null);
    OptionService os = findService(OptionService.class);
    MimeMessage mm = createMimeMessage(controller, session, os);

    LOG.info("");
    LOG.info(" From:    " + toString(mm.getFrom()));
    LOG.info(" ReplyTo: " + toString(mm.getReplyTo()));
    LOG.info(" Sender:  " + mm.getSender());
    LOG.info(" To:      " + toString(mm.getRecipients(Message.RecipientType.TO)));
    LOG.info(" CC:      " + toString(mm.getRecipients(Message.RecipientType.CC)));
    LOG.info(" Bcc:     " + toString(mm.getRecipients(Message.RecipientType.BCC)));
    LOG.info(" Subject: " + mm.getSubject());
    LOG.info("");

    Transport.send(mm);
  }
Example #3
0
 private void setFrom(MailController controller, MimeMessage msg, OptionService os)
     throws MessagingException {
   String from = controller.getFrom();
   if (StringUtil.isEmpty(from)) {
     from = os.get(OptionService.OPTION_MAIL_FROM, null);
   }
   if (!StringUtil.isEmpty(from)) {
     msg.setFrom(new InternetAddress(from));
   }
 }
Example #4
0
 private void setSubject(MailController controller, MimeMessage msg) throws MessagingException {
   String subject = controller.getSubject();
   msg.setSubject(!StringUtil.isEmpty(subject) ? subject : StringUtil.EMPTY);
 }
Example #5
0
 private void addRecipients(MailController controller, MimeMessage msg) throws MessagingException {
   addRecipients(controller.getTo(), Message.RecipientType.TO, msg);
   addRecipients(controller.getCc(), Message.RecipientType.CC, msg);
   addRecipients(controller.getBcc(), Message.RecipientType.BCC, msg);
 }
Example #6
0
 private void setReplyTo(MailController controller, MimeMessage msg) throws MessagingException {
   String replyTo = controller.getReplyTo();
   if (!StringUtil.isEmpty(replyTo)) {
     msg.setReplyTo(new InternetAddress[] {new InternetAddress(replyTo)});
   }
 }