/** Creates new JavaX message from {@link Email email}. */ protected MimeMessage createMessage(Email email, Session session) throws MessagingException { MimeMessage msg = new MimeMessage(session); msg.setFrom(email.getFrom().toInternetAddress()); // to int totalTo = email.getTo().length; InternetAddress[] address = new InternetAddress[totalTo]; for (int i = 0; i < totalTo; i++) { address[i] = email.getTo()[i].toInternetAddress(); } msg.setRecipients(Message.RecipientType.TO, address); // replyTo if (email.getReplyTo() != null) { int totalReplyTo = email.getReplyTo().length; address = new InternetAddress[totalReplyTo]; for (int i = 0; i < totalReplyTo; i++) { address[i] = email.getReplyTo()[i].toInternetAddress(); } msg.setReplyTo(address); } // cc if (email.getCc() != null) { int totalCc = email.getCc().length; address = new InternetAddress[totalCc]; for (int i = 0; i < totalCc; i++) { address[i] = email.getCc()[i].toInternetAddress(); } msg.setRecipients(Message.RecipientType.CC, address); } // bcc if (email.getBcc() != null) { int totalBcc = email.getBcc().length; address = new InternetAddress[totalBcc]; for (int i = 0; i < totalBcc; i++) { address[i] = email.getBcc()[i].toInternetAddress(); } msg.setRecipients(Message.RecipientType.BCC, address); } // subject & date if (email.getSubjectEncoding() != null) { msg.setSubject(email.getSubject(), email.getSubjectEncoding()); } else { msg.setSubject(email.getSubject()); } Date date = email.getSentDate(); if (date == null) { date = new Date(); } msg.setSentDate(date); // headers Map<String, String> headers = email.getAllHeaders(); if (headers != null) { for (Map.Entry<String, String> stringStringEntry : headers.entrySet()) { String value = stringStringEntry.getValue(); msg.setHeader(stringStringEntry.getKey(), value); } } // message data and attachments final List<EmailMessage> messages = email.getAllMessages(); final List<EmailAttachment> attachments = email.getAttachments() == null ? null : new ArrayList<>(email.getAttachments()); final int totalMessages = messages.size(); if ((attachments == null) && (totalMessages == 1)) { // special case: no attachments and just one content EmailMessage emailMessage = messages.get(0); msg.setContent( emailMessage.getContent(), emailMessage.getMimeType() + CHARSET + emailMessage.getEncoding()); } else { Multipart multipart = new MimeMultipart(); Multipart msgMultipart = multipart; if (totalMessages > 1) { MimeBodyPart bodyPart = new MimeBodyPart(); msgMultipart = new MimeMultipart(ALTERNATIVE); bodyPart.setContent(msgMultipart); multipart.addBodyPart(bodyPart); } for (EmailMessage emailMessage : messages) { // detect embedded attachments List<EmailAttachment> embeddedAttachments = filterEmbeddedAttachments(attachments, emailMessage); MimeBodyPart bodyPart = new MimeBodyPart(); if (embeddedAttachments == null) { // no embedded attachments, just add message bodyPart.setContent( emailMessage.getContent(), emailMessage.getMimeType() + CHARSET + emailMessage.getEncoding()); } else { // embedded attachments detected, join them as related MimeMultipart relatedMultipart = new MimeMultipart(RELATED); MimeBodyPart messageData = new MimeBodyPart(); messageData.setContent( emailMessage.getContent(), emailMessage.getMimeType() + CHARSET + emailMessage.getEncoding()); relatedMultipart.addBodyPart(messageData); for (EmailAttachment att : embeddedAttachments) { MimeBodyPart attBodyPart = createAttachmentBodyPart(att); relatedMultipart.addBodyPart(attBodyPart); } bodyPart.setContent(relatedMultipart); } msgMultipart.addBodyPart(bodyPart); } if (attachments != null) { // attach remaining attachments for (EmailAttachment att : attachments) { MimeBodyPart attBodyPart = createAttachmentBodyPart(att); multipart.addBodyPart(attBodyPart); } } msg.setContent(multipart); } return msg; }