Exemplo n.º 1
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));
   }
 }
Exemplo n.º 2
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;
  }
Exemplo n.º 3
0
 private void setSubject(MailController controller, MimeMessage msg) throws MessagingException {
   String subject = controller.getSubject();
   msg.setSubject(!StringUtil.isEmpty(subject) ? subject : StringUtil.EMPTY);
 }
Exemplo n.º 4
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)});
   }
 }
Exemplo n.º 5
0
 private void setSender(MimeMessage msg, OptionService os) throws MessagingException {
   String from = os.get(OptionService.OPTION_MAIL_FROM, null);
   if (!StringUtil.isEmpty(from)) {
     msg.setSender(new InternetAddress(from));
   }
 }