/** * test version of sendMail */ protected void testSendMail(InternetAddress from, InternetAddress[] to, String subject, String content, Map<RecipientType, InternetAddress[]> headerTo, InternetAddress[] replyTo, List<String> additionalHeaders, List<Attachment> attachments) { log.info("sendMail: from: {} to: {} subject: {} headerTo: {} replyTo: {} content: {} additionalHeaders: {}", from, arrayToStr(to), subject, mapToStr(headerTo), arrayToStr(replyTo), content, listToStr(additionalHeaders)); //If the attachments isn't empty do something with them if (CollectionUtils.isNotEmpty(attachments)){ for (Attachment attachment:attachments) { //If it's text, we'll dump out the file contents, otherwise just the name String attachmentContent = "BINARY"; if (attachment.getContentTypeHeader() != null && attachment.getContentTypeHeader().startsWith("text/")) { try { attachmentContent = IOUtils.toString(attachment.getDataSource().getInputStream(), "UTF-8"); } catch (IOException e) { // TODO Auto-generated catch block log.debug("sendMail: error accessing attachment content",e); } } log.info("sendMail: attachment name: {} type header: {} body:{}{}", attachment.getFilename(),attachment.getContentTypeHeader(), System.lineSeparator(), attachmentContent); } } }
/** * Attaches a file as a body part to the multipart message * * @param attachment * @throws MessagingException */ private MimeBodyPart createAttachmentPart(Attachment attachment) throws MessagingException { DataSource source = attachment.getDataSource(); MimeBodyPart attachPart = new MimeBodyPart(); attachPart.setDataHandler(new DataHandler(source)); attachPart.setFileName(attachment.getFilename()); if (attachment.getContentTypeHeader() != null) { attachPart.setHeader("Content-Type", attachment.getContentTypeHeader()); } if (attachment.getContentDispositionHeader() != null) { attachPart.setHeader("Content-Disposition", attachment.getContentDispositionHeader()); } return attachPart; }