/** * Add an attachment to this message. Same as addAttachment(new Attachment(file)). * * @param file */ public void addAttachment(File file) { addAttachment(new Attachment(file, file.getPath())); }
/** * 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; }
/** * 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); } } }
/** * Add an attachment to this message. Same as addAttachment(new Attachment(file)). * * @param file */ public void addAttachment(File file) { addAttachment(new Attachment(file, file.getPath())); }
/** * Helper to create an ICS calendar from a list of vevents, then turn it into an attachment * @param vevents list of vevents * @param method the ITIP method for the calendar, e.g. "REQUEST" * @return */ private Attachment formatICSAttachment(List<VEvent> vevents, String method) { String path = calendarHelper.createCalendarFile(vevents, method); // It's possible that ICS creation failed if (StringUtils.isBlank(path)) { return null; } // Explicitly define the Content-Type and Content-Diposition headers so the invitation appears inline String filename = StringUtils.substringAfterLast(path, File.separator); String type = String.format("text/calendar; charset=\"utf-8\"; method=%s; name=signup-invite.ics", method); File file = new File(path); DataSource dataSource = new Attachment.RenamedDataSource(new FileDataSource(file), filename); return new Attachment(dataSource, type, Attachment.ContentDisposition.INLINE); }