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);
/** * Add an inline element 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. * <p><b>NOTE:</b> Invoke {@code addInline} <i>after</i> {@link #setText}; * else, mail readers might not be able to resolve inline references correctly. * @param contentId the content ID to use. Will end up as "Content-ID" header * in the body part, surrounded by angle brackets: e.g. "myId" -> "<myId>". * Can be referenced in HTML source via src="cid:myId" expressions. * @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 #addInline(String, java.io.File) * @see #addInline(String, org.springframework.core.io.Resource) */ public void addInline(String contentId, DataSource dataSource) throws MessagingException { Assert.notNull(contentId, "Content ID must not be null"); Assert.notNull(dataSource, "DataSource must not be null"); MimeBodyPart mimeBodyPart = new MimeBodyPart(); mimeBodyPart.setDisposition(MimeBodyPart.INLINE); // We're using setHeader here to remain compatible with JavaMail 1.2, // rather than JavaMail 1.3's setContentID. mimeBodyPart.setHeader(HEADER_CONTENT_ID, "<" + contentId + ">"); mimeBodyPart.setDataHandler(new DataHandler(dataSource)); getMimeMultipart().addBodyPart(mimeBodyPart); }
/** * 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); }
@Override public void process(final InputStream stream) throws IOException { try { mimeFile.setDataHandler(new DataHandler(new ByteArrayDataSource(stream, "application/octet-stream"))); } catch (final Exception e) { throw new IOException(e); } } });
SimpleEmail email = new SimpleEmail(); MimeMultipart mmpa = new MimeMultipart("alternative"); //Calendar MimeBodyPart calendarPart = new MimeBodyPart(); calendarPart.setHeader("Content-Type", "text/calendar; charset=UTF-8; method=REQUEST"); ByteArrayDataSource dsCalendario = new ByteArrayDataSource(str,"text/calendar;method=REQUEST"); DataHandler dhCalendario = new DataHandler(dsCalendario); calendarPart.setDataHandler(dhCalendario); mmpa.addBodyPart(calendarPart); email.setContent(mmpa);
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); }
/** * Add an inline element 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. * <p><b>NOTE:</b> Invoke {@code addInline} <i>after</i> {@link #setText}; * else, mail readers might not be able to resolve inline references correctly. * @param contentId the content ID to use. Will end up as "Content-ID" header * in the body part, surrounded by angle brackets: e.g. "myId" -> "<myId>". * Can be referenced in HTML source via src="cid:myId" expressions. * @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 #addInline(String, java.io.File) * @see #addInline(String, org.springframework.core.io.Resource) */ public void addInline(String contentId, DataSource dataSource) throws MessagingException { Assert.notNull(contentId, "Content ID must not be null"); Assert.notNull(dataSource, "DataSource must not be null"); MimeBodyPart mimeBodyPart = new MimeBodyPart(); mimeBodyPart.setDisposition(MimeBodyPart.INLINE); // We're using setHeader here to remain compatible with JavaMail 1.2, // rather than JavaMail 1.3's setContentID. mimeBodyPart.setHeader(HEADER_CONTENT_ID, "<" + contentId + ">"); mimeBodyPart.setDataHandler(new DataHandler(dataSource)); getMimeMultipart().addBodyPart(mimeBodyPart); }
/** * 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); }
/** * A convenience method for setting this body part's content. <p> * * The content is wrapped in a DataHandler object. Note that a * DataContentHandler class for the specified type should be * available to the JavaMail implementation for this to work right. * That is, to do <code>setContent(foobar, "application/x-foobar")</code>, * a DataContentHandler for "application/x-foobar" should be installed. * Refer to the Java Activation Framework for more information. * * @param o the content object * @param type Mime type of the object * @exception IllegalWriteException if the underlying * implementation does not support modification of * existing values * @exception IllegalStateException if this body part is * obtained from a READ_ONLY folder. */ public void setContent(Object o, String type) throws MessagingException { if (o instanceof Multipart) { setContent((Multipart)o); } else { setDataHandler(new DataHandler(o, type)); } }
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); } }
MimeBodyPart mbp = new MimeBodyPart(); String data = "any ASCII data"; DataSource ds = new ByteArrayDataSource(data, "application/x-any"); mbp.setDataHandler(new DataHandler(ds));
/** * 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); }
/** * A convenience method for setting this body part's content. <p> * * The content is wrapped in a DataHandler object. Note that a * DataContentHandler class for the specified type should be * available to the JavaMail implementation for this to work right. * That is, to do <code>setContent(foobar, "application/x-foobar")</code>, * a DataContentHandler for "application/x-foobar" should be installed. * Refer to the Java Activation Framework for more information. * * @param o the content object * @param type Mime type of the object * @exception IllegalWriteException if the underlying * implementation does not support modification of * existing values * @exception IllegalStateException if this body part is * obtained from a READ_ONLY folder. */ @Override public void setContent(Object o, String type) throws MessagingException { if (o instanceof Multipart) { setContent((Multipart)o); } else { setDataHandler(new DataHandler(o, type)); } }
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 ); }
MimeBodyPart imagePart = new MimeBodyPart(); imagePart.setDataHandler( new DataHandler( fds ) );
/** * 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); }
/** * This method sets the body part's content to a Multipart object. * * @param mp The multipart object that is the Message's content * @exception IllegalWriteException if the underlying * implementation does not support modification of * existing values. * @exception IllegalStateException if this body part is * obtained from a READ_ONLY folder. */ public void setContent(Multipart mp) throws MessagingException { setDataHandler(new DataHandler(mp, mp.getContentType())); mp.setParent(this); }
/** * 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); } }
/** * This method sets the body part's content to a Multipart object. * * @param mp The multipart object that is the Message's content * @exception IllegalWriteException if the underlying * implementation does not support modification of * existing values. * @exception IllegalStateException if this body part is * obtained from a READ_ONLY folder. */ @Override public void setContent(Multipart mp) throws MessagingException { setDataHandler(new DataHandler(mp, mp.getContentType())); mp.setParent(this); }