MimeMessageBuilder { public MimeMessage build() { final MimeMessage message = new MimeMessage(session); try { message.setFrom(fromAddress); } catch (Exception e) { throw new DirectException("Exception setting from address: " + fromAddress, e); } try { message.addRecipients(Message.RecipientType.TO, recipients); } catch (Exception e) { throw new DirectException("Exception setting recipient to address(es): " + recipients, e); } try { message.setSubject(subject); } catch (Exception e) { throw new DirectException("Exception setting subject: " + subject, e); } MimeBodyPart messagePart = new MimeBodyPart(); try { messagePart.setText(text); } catch (Exception e) { throw new DirectException("Exception setting mime message part text: " + text, e); } MimeBodyPart attachmentPart; try { if (documents != null && !StringUtils.isBlank(messageId)) { attachmentPart = getMimeBodyPart(); messageId = messageId.replace(NhincConstants.WS_SOAP_HEADER_MESSAGE_ID_PREFIX, ""); attachmentPart.attachFile(documents.toXdmPackage(messageId).toFile()); } else if (attachment != null && !StringUtils.isBlank(attachmentName)) { attachmentPart = createAttachmentFromSOAPRequest(attachment, attachmentName); } else { throw new Exception( "Could not create attachment. Need documents and messageId or attachment and attachmentName."); } } catch (Exception e) { throw new DirectException("Exception creating attachment: " + attachmentName, e); } Multipart multipart = new MimeMultipart(); try { multipart.addBodyPart(messagePart); multipart.addBodyPart(attachmentPart); message.setContent(multipart); } catch (Exception e) { throw new DirectException("Exception creating multi-part attachment.", e); } try { message.saveChanges(); } catch (Exception e) { throw new DirectException("Exception saving changes.", e); } return message; } MimeMessageBuilder(Session session, Address fromAddress, Address[] recipients); MimeMessageBuilder subject(String str); MimeMessageBuilder text(String str); MimeMessageBuilder documents(DirectDocuments directDocuments); MimeMessageBuilder messageId(String str); MimeMessageBuilder attachment(Document doc); MimeMessageBuilder attachmentName(String str); MimeMessage build();  }
@Test public void canBuildMessage() throws IOException { assertNotNull(getBuilder().build()); }