if (arrayInputStream != null && arrayInputStream instanceof ByteArrayInputStream) { // create the second message part with the attachment from a OutputStrean MimeBodyPart attachment= new MimeBodyPart(); ByteArrayDataSource ds = new ByteArrayDataSource(arrayInputStream, "application/pdf"); attachment.setDataHandler(new DataHandler(ds)); attachment.setFileName("Report.pdf"); mimeMultipart.addBodyPart(attachment); }
Multipart multipart = new MimeMultipart("mixed"); for (String str : attachment_PathList) { MimeBodyPart messageBodyPart = new MimeBodyPart(); DataSource source = new FileDataSource(str); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(source.getName()); multipart.addBodyPart(messageBodyPart); } msg.setContent(multipart); Transport.send(msg);
/** * Use the specified file to provide the data for this part. * The simple file name is used as the file name for this * part and the data in the file is used as the data for this * part. The encoding will be chosen appropriately for the * file data. The disposition of this part is set to * {@link Part#ATTACHMENT Part.ATTACHMENT}. * * @param file the File object to attach * @exception IOException errors related to accessing the file * @exception MessagingException message related errors * @since JavaMail 1.4 */ public void attachFile(File file) throws IOException, MessagingException { FileDataSource fds = new FileDataSource(file); this.setDataHandler(new DataHandler(fds)); this.setFileName(fds.getName()); this.setDisposition(ATTACHMENT); }
/** * Creates a MimeMultipart, so that we can have an attachment. * * @param message * @return */ private MimeMultipart createMultipart(Message message) throws Exception { MimeMultipart toReturn = new MimeMultipart(); MimeBodyPart textContent = new MimeBodyPart(); textContent.setContent(message.getContent(), message.getContentType()); MimeBodyPart attachment = new MimeBodyPart(); attachment.setContent(message.getAttachment(), message.getAttachmentContentType()); attachment.setFileName(message.getAttachmentFileName()); toReturn.addBodyPart(textContent); toReturn.addBodyPart(attachment); return toReturn; }
MimeMultipart rootContainer = new MimeMultipart(); rootContainer.setSubType("related"); rootContainer.addBodyPart(alternativeMultiPartWithPlainTextAndHtml); // not in focus here rootContainer.addBodyPart(createInlineImagePart(base64EncodedImageContentByteArray)); ... message.setContent(rootContainer); message.setHeader("MIME-Version", "1.0"); message.setHeader("Content-Type", rootContainer.getContentType()); ... BodyPart createInlineImagePart(byte[] base64EncodedImageContentByteArray) throws MessagingException { InternetHeaders headers = new InternetHeaders(); headers.addHeader("Content-Type", "image/jpeg"); headers.addHeader("Content-Transfer-Encoding", "base64"); MimeBodyPart imagePart = new MimeBodyPart(headers, base64EncodedImageContentByteArray); imagePart.setDisposition(MimeBodyPart.INLINE); imagePart.setContentID("<image>"); imagePart.setFileName("image.jpg"); return imagePart;
private void attachCSV(Multipart multipart, QrHolder[] attachmentData) throws Exception { StringBuilder sb = new StringBuilder(); for (QrHolder qrHolder : attachmentData) { sb.append(qrHolder.token) .append(",") .append(qrHolder.deviceId) .append(",") .append(qrHolder.dashId) .append("\n"); } MimeBodyPart attachmentsPart = new MimeBodyPart(); ByteArrayDataSource source = new ByteArrayDataSource(sb.toString(), "text/csv"); attachmentsPart.setDataHandler(new DataHandler(source)); attachmentsPart.setFileName("tokens.csv"); multipart.addBodyPart(attachmentsPart); }
/** * Use the specified file to provide the data for this part. * The simple file name is used as the file name for this * part and the data in the file is used as the data for this * part. The encoding will be chosen appropriately for the * file data. The disposition of this part is set to * {@link Part#ATTACHMENT Part.ATTACHMENT}. * * @param file the File object to attach * @exception IOException errors related to accessing the file * @exception MessagingException message related errors * @since JavaMail 1.4 */ public void attachFile(File file) throws IOException, MessagingException { FileDataSource fds = new FileDataSource(file); this.setDataHandler(new DataHandler(fds)); this.setFileName(fds.getName()); this.setDisposition(ATTACHMENT); }
Multipart multipart = new MimeMultipart("mixed"); for (int alen = 0; attlen < attachments.length; attlen++) { MimeBodyPart messageAttachment = new MimeBodyPart(); fileName = ""+ attachments[attlen]; messageAttachment.attachFile(fileName); messageAttachment.setFileName(attachment); multipart.addBodyPart(messageAttachment); }
if (isEmpty(name)) { //Exceptional case. name = toString(attachmentFormatters[i]); parts[i].setFileName(name); MimeMultipart multipart = new MimeMultipart(); String altType = getContentType(bodyFormat.getClass().getName()); setContent(body, buf, altType == null ? contentType : altType);
private void attachQRs(Multipart multipart, QrHolder[] attachmentData) throws Exception { for (QrHolder qrHolder : attachmentData) { MimeBodyPart attachmentsPart = new MimeBodyPart(); ByteArrayDataSource source = new ByteArrayDataSource(qrHolder.data, "image/jpeg"); attachmentsPart.setDataHandler(new DataHandler(source)); attachmentsPart.setFileName(qrHolder.makeQRFilename()); multipart.addBodyPart(attachmentsPart); } }
/** * Use the specified file with the specified Content-Type and * Content-Transfer-Encoding to provide the data for this part. * If contentType or encoding are null, appropriate values will * be chosen. * The simple file name is used as the file name for this * part and the data in the file is used as the data for this * part. The disposition of this part is set to * {@link Part#ATTACHMENT Part.ATTACHMENT}. * * @param file the File object to attach * @param contentType the Content-Type, or null * @param encoding the Content-Transfer-Encoding, or null * @exception IOException errors related to accessing the file * @exception MessagingException message related errors * @since JavaMail 1.5 */ public void attachFile(File file, String contentType, String encoding) throws IOException, MessagingException { DataSource fds = new EncodedFileDataSource(file, contentType, encoding); this.setDataHandler(new DataHandler(fds)); this.setFileName(fds.getName()); this.setDisposition(ATTACHMENT); }
Multipart multipart = new MimeMultipart(); MimeBodyPart html = new MimeBodyPart(); // Use actual html not "strstr" html.setContent("<html><body><h1>Hi</h1></body></html>", "text/html"); multipart.addBodyPart(html); // ... // Joop Eggen suggestion to avoid spaces in the file name String fileName = "Service_Change_Alert_" + new SimpleDateFormat("yyyy-MM-dd_HH:mm").format(date) + ".xlsx"; ByteArrayOutputStream baos = new ByteArrayOutputStream(); updateDataBook.write(baos); byte[] poiBytes = baos.toByteArray(); // Can be followed by the DataSource / DataHandler stuff if you really need it MimeBodyPart attachment = new MimeBodyPart(); attachment.setFileName(filename); attachment.setContent(poiBytes, "application/vnd.ms-excel"); //attachment.setDataHandler(dh); attachment.setDisposition(MimeBodyPart.ATTACHMENT); multipart.addBodyPart(attachment);
if (isEmpty(name)) { //Exceptional case. name = toString(attachmentFormatters[i]); parts[i].setFileName(name); setContent(body, buf, altType == null ? contentType : altType); if (body != msg) { final MimeMultipart multipart = new MimeMultipart();
private void addAttachedContent( String filename, String fileContent ) throws Exception { // create a data source MimeBodyPart mbp = new MimeBodyPart(); // get a data Handler to manipulate this file type; mbp.setDataHandler( new DataHandler( new ByteArrayDataSource( fileContent.getBytes(), "application/x-any" ) ) ); // include the file in the data source mbp.setFileName( filename ); // add the part with the file in the BodyPart(); data.parts.addBodyPart( mbp ); }
/** * Use the specified file with the specified Content-Type and * Content-Transfer-Encoding to provide the data for this part. * If contentType or encoding are null, appropriate values will * be chosen. * The simple file name is used as the file name for this * part and the data in the file is used as the data for this * part. The disposition of this part is set to * {@link Part#ATTACHMENT Part.ATTACHMENT}. * * @param file the File object to attach * @param contentType the Content-Type, or null * @param encoding the Content-Transfer-Encoding, or null * @exception IOException errors related to accessing the file * @exception MessagingException message related errors * @since JavaMail 1.5 */ public void attachFile(File file, String contentType, String encoding) throws IOException, MessagingException { DataSource fds = new EncodedFileDataSource(file, contentType, encoding); this.setDataHandler(new DataHandler(fds)); this.setFileName(fds.getName()); this.setDisposition(ATTACHMENT); }
try { MimeMultipart multipart = new MimeMultipart(); MimeBodyPart[] ambp = new MimeBodyPart[atn.length]; final MimeBodyPart body; for (int i = 0; i < atn.length; ++i) { ambp[i] = createBodyPart(i); ambp[i].setFileName(atn[i]);
/** * Add an attachment to the MimeMessage, taking the content from a * {@code javax.activation.DataSource}. * <p>Note that the InputStream returned by the DataSource implementation * needs to be a <i>fresh one on each call</i>, as JavaMail will invoke * {@code getInputStream()} multiple times. * @param attachmentFilename the name of the attachment as it will * appear in the mail (the content type will be determined by this) * @param dataSource the {@code javax.activation.DataSource} to take * the content from, determining the InputStream and the content type * @throws MessagingException in case of errors * @see #addAttachment(String, org.springframework.core.io.InputStreamSource) * @see #addAttachment(String, java.io.File) */ public void addAttachment(String attachmentFilename, DataSource dataSource) throws MessagingException { Assert.notNull(attachmentFilename, "Attachment filename must not be null"); Assert.notNull(dataSource, "DataSource must not be null"); try { MimeBodyPart mimeBodyPart = new MimeBodyPart(); mimeBodyPart.setDisposition(MimeBodyPart.ATTACHMENT); mimeBodyPart.setFileName(MimeUtility.encodeText(attachmentFilename)); mimeBodyPart.setDataHandler(new DataHandler(dataSource)); getRootMimeMultipart().addBodyPart(mimeBodyPart); } catch (UnsupportedEncodingException ex) { throw new MessagingException("Failed to encode attachment filename", ex); } }
Object ccl = getAndSetContextClassLoader(MAILHANDLER_LOADER); try { MimeMultipart multipart = new MimeMultipart(); MimeBodyPart[] ambp = new MimeBodyPart[atn.length]; final MimeBodyPart body; for (int i = 0; i < atn.length; ++i) { ambp[i] = createBodyPart(i); ambp[i].setFileName(atn[i]);
@Override public void sendHtmlWithAttachment(String to, String subj, String body, QrHolder[] attachments) throws Exception { MimeMessage message = new MimeMessage(session); message.setFrom(from); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); message.setSubject(subj, "UTF-8"); Multipart multipart = new MimeMultipart(); MimeBodyPart bodyMessagePart = new MimeBodyPart(); bodyMessagePart.setContent(body, TEXT_HTML_CHARSET_UTF_8); multipart.addBodyPart(bodyMessagePart); for (QrHolder qrHolder : attachments) { MimeBodyPart attachmentsPart = new MimeBodyPart(); attachmentsPart.setDataHandler(new DataHandler(new ByteArrayDataSource(qrHolder.data, "image/jpeg"))); attachmentsPart.setFileName(qrHolder.makeQRFilename()); multipart.addBodyPart(attachmentsPart); } message.setContent(multipart); try (Transport transport = session.getTransport()) { transport.connect(host, username, password); transport.sendMessage(message, message.getAllRecipients()); } log.debug("Mail sent to {}. Subj: {}", to, subj); log.trace("Mail body: {}", body); }
private Response executeReport( long userId, boolean mail, ReportExecutor executor) throws SQLException, IOException { final ByteArrayOutputStream stream = new ByteArrayOutputStream(); if (mail) { new Thread(() -> { try { executor.execute(stream); MimeBodyPart attachment = new MimeBodyPart(); attachment.setFileName("report.xlsx"); attachment.setDataHandler(new DataHandler(new ByteArrayDataSource( stream.toByteArray(), "application/octet-stream"))); Context.getMailManager().sendMessage( userId, "Report", "The report is in the attachment.", attachment); } catch (SQLException | IOException | MessagingException e) { LOGGER.warn("Report failed", e); } }).start(); return Response.noContent().build(); } else { executor.execute(stream); return Response.ok(stream.toByteArray()) .header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION_VALUE_XLSX).build(); } }