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)); } }
// -- 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; }
private void setSubject(MailController controller, MimeMessage msg) throws MessagingException { String subject = controller.getSubject(); msg.setSubject(!StringUtil.isEmpty(subject) ? subject : StringUtil.EMPTY); }
private void setReplyTo(MailController controller, MimeMessage msg) throws MessagingException { String replyTo = controller.getReplyTo(); if (!StringUtil.isEmpty(replyTo)) { msg.setReplyTo(new InternetAddress[] {new InternetAddress(replyTo)}); } }
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)); } }