Refine search
/** * Set the given plain text and HTML text as alternatives, offering * both options to the email client. Requires multipart mode. * <p><b>NOTE:</b> Invoke {@link #addInline} <i>after</i> {@code setText}; * else, mail readers might not be able to resolve inline references correctly. * @param plainText the plain text for the message * @param htmlText the HTML text for the message * @throws MessagingException in case of errors */ public void setText(String plainText, String htmlText) throws MessagingException { Assert.notNull(plainText, "Plain text must not be null"); Assert.notNull(htmlText, "HTML text must not be null"); MimeMultipart messageBody = new MimeMultipart(MULTIPART_SUBTYPE_ALTERNATIVE); getMainPart().setContent(messageBody, CONTENT_TYPE_ALTERNATIVE); // Create the plain text part of the message. MimeBodyPart plainTextPart = new MimeBodyPart(); setPlainTextToMimePart(plainTextPart, plainText); messageBody.addBodyPart(plainTextPart); // Create the HTML text part of the message. MimeBodyPart htmlTextPart = new MimeBodyPart(); setHtmlTextToMimePart(htmlTextPart, htmlText); messageBody.addBodyPart(htmlTextPart); }
/** * 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); }
/** * Adds message data and attachments. * * @param emailWithData {@link Email} with data * @param msgToSet {@link MimeMessage} to set data into. * @throws MessagingException if there is a failure. */ private void addBodyData(final Email emailWithData, final MimeMessage msgToSet) throws MessagingException { final List<EmailMessage> messages = emailWithData.messages(); final int totalMessages = messages.size(); // Need to use new list since filterEmbeddedAttachments(List) removes attachments from the source List final List<EmailAttachment<? extends DataSource>> attachments = new ArrayList<>(emailWithData.attachments()); if (attachments.isEmpty() && totalMessages == 1) { // special case: no attachments and just one content setContent(messages.get(0), msgToSet); } else { final MimeMultipart multipart = new MimeMultipart(); final MimeMultipart msgMultipart = new MimeMultipart(ALTERNATIVE); multipart.addBodyPart(getBaseBodyPart(msgMultipart)); for (final EmailMessage emailMessage : messages) { msgMultipart.addBodyPart(getBodyPart(emailMessage, attachments)); } addAnyAttachments(attachments, multipart); msgToSet.setContent(multipart); } }
break; case MULTIPART_MODE_MIXED: MimeMultipart mixedMultipart = new MimeMultipart(MULTIPART_SUBTYPE_MIXED); mimeMessage.setContent(mixedMultipart); setMimeMultiparts(mixedMultipart, mixedMultipart); break; case MULTIPART_MODE_RELATED: MimeMultipart relatedMultipart = new MimeMultipart(MULTIPART_SUBTYPE_RELATED); mimeMessage.setContent(relatedMultipart); setMimeMultiparts(relatedMultipart, relatedMultipart); break; case MULTIPART_MODE_MIXED_RELATED: MimeMultipart rootMixedMultipart = new MimeMultipart(MULTIPART_SUBTYPE_MIXED); mimeMessage.setContent(rootMixedMultipart); MimeMultipart nestedRelatedMultipart = new MimeMultipart(MULTIPART_SUBTYPE_RELATED); MimeBodyPart relatedBodyPart = new MimeBodyPart(); relatedBodyPart.setContent(nestedRelatedMultipart); rootMixedMultipart.addBodyPart(relatedBodyPart); setMimeMultiparts(rootMixedMultipart, nestedRelatedMultipart); break;
private MimeBodyPart getMainPart() throws MessagingException { MimeMultipart mimeMultipart = getMimeMultipart(); MimeBodyPart bodyPart = null; for (int i = 0; i < mimeMultipart.getCount(); i++) { BodyPart bp = mimeMultipart.getBodyPart(i); if (bp.getFileName() == null) { bodyPart = (MimeBodyPart) bp; } } if (bodyPart == null) { MimeBodyPart mimeBodyPart = new MimeBodyPart(); mimeMultipart.addBodyPart(mimeBodyPart); bodyPart = mimeBodyPart; } return bodyPart; }
MimeMultipart content = new MimeMultipart("related"); for (DataSource attachment : attachments) { MimeBodyPart attachmentPart = createAttachmentPart(attachment); content.addBodyPart(attachmentPart); content.addBodyPart(bodyPart);
/** * Set the given plain text and HTML text as alternatives, offering * both options to the email client. Requires multipart mode. * <p><b>NOTE:</b> Invoke {@link #addInline} <i>after</i> {@code setText}; * else, mail readers might not be able to resolve inline references correctly. * @param plainText the plain text for the message * @param htmlText the HTML text for the message * @throws MessagingException in case of errors */ public void setText(String plainText, String htmlText) throws MessagingException { Assert.notNull(plainText, "Plain text must not be null"); Assert.notNull(htmlText, "HTML text must not be null"); MimeMultipart messageBody = new MimeMultipart(MULTIPART_SUBTYPE_ALTERNATIVE); getMainPart().setContent(messageBody, CONTENT_TYPE_ALTERNATIVE); // Create the plain text part of the message. MimeBodyPart plainTextPart = new MimeBodyPart(); setPlainTextToMimePart(plainTextPart, plainText); messageBody.addBodyPart(plainTextPart); // Create the HTML text part of the message. MimeBodyPart htmlTextPart = new MimeBodyPart(); setHtmlTextToMimePart(htmlTextPart, htmlText); messageBody.addBodyPart(htmlTextPart); }
/** * 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); } }
+ subjectType + '.'); setContent(body, toMsgString(t), "text/plain"); final MimeMultipart multipart = new MimeMultipart(); multipart.addBodyPart(body); msg.setContent(multipart); msg.setDescription(msgDesc);
/** * @param emailMessage {@link EmailMessage} with data. * @param attachments {@link List} of {@link EmailAttachment}s. * @return new {@link MimeBodyPart} with data from emailMessage and attachments. * @throws MessagingException if there is a failure. */ private MimeBodyPart getBodyPart(final EmailMessage emailMessage, final List<EmailAttachment<? extends DataSource>> attachments) throws MessagingException { final MimeBodyPart bodyPart = new MimeBodyPart(); // detect embedded attachments final List<EmailAttachment<? extends DataSource>> embeddedAttachments = filterEmbeddedAttachments(attachments, emailMessage); if (embeddedAttachments.isEmpty()) { // no embedded attachments, just add message setContent(emailMessage, bodyPart); } else { attachments.removeAll(embeddedAttachments); // embedded attachments detected, join them as related final MimeMultipart relatedMultipart = new MimeMultipart(RELATED); final MimeBodyPart messageData = new MimeBodyPart(); setContent(emailMessage, messageData); relatedMultipart.addBodyPart(messageData); addAnyAttachments(embeddedAttachments, relatedMultipart); bodyPart.setContent(relatedMultipart); } return bodyPart; }
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 ); }
+ subjectType + '.'); setContent(body, toMsgString(t), "text/plain"); final MimeMultipart multipart = new MimeMultipart(); multipart.addBodyPart(body); msg.setContent(multipart); msg.setDescription(msgDesc);
break; case MULTIPART_MODE_MIXED: MimeMultipart mixedMultipart = new MimeMultipart(MULTIPART_SUBTYPE_MIXED); mimeMessage.setContent(mixedMultipart); setMimeMultiparts(mixedMultipart, mixedMultipart); break; case MULTIPART_MODE_RELATED: MimeMultipart relatedMultipart = new MimeMultipart(MULTIPART_SUBTYPE_RELATED); mimeMessage.setContent(relatedMultipart); setMimeMultiparts(relatedMultipart, relatedMultipart); break; case MULTIPART_MODE_MIXED_RELATED: MimeMultipart rootMixedMultipart = new MimeMultipart(MULTIPART_SUBTYPE_MIXED); mimeMessage.setContent(rootMixedMultipart); MimeMultipart nestedRelatedMultipart = new MimeMultipart(MULTIPART_SUBTYPE_RELATED); MimeBodyPart relatedBodyPart = new MimeBodyPart(); relatedBodyPart.setContent(nestedRelatedMultipart); rootMixedMultipart.addBodyPart(relatedBodyPart); setMimeMultiparts(rootMixedMultipart, nestedRelatedMultipart); break;
/** * 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); }
MimeMultipart multipart = new MimeMultipart(); String altType = getContentType(bodyFormat.getClass().getName()); setContent(body, buf, altType == null ? contentType : altType); multipart.addBodyPart(body); multipart.addBodyPart(parts[i]);
this._enableAttachementEmbedment ? new MimeMultipart("related") : new MimeMultipart(); final BodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setContent(this._body.toString(), this._mimeType); multipart.addBodyPart(messageBodyPart); multipart.addBodyPart(part);
private MimeBodyPart getMainPart() throws MessagingException { MimeMultipart mimeMultipart = getMimeMultipart(); MimeBodyPart bodyPart = null; for (int i = 0; i < mimeMultipart.getCount(); i++) { BodyPart bp = mimeMultipart.getBodyPart(i); if (bp.getFileName() == null) { bodyPart = (MimeBodyPart) bp; } } if (bodyPart == null) { MimeBodyPart mimeBodyPart = new MimeBodyPart(); mimeMultipart.addBodyPart(mimeBodyPart); bodyPart = mimeBodyPart; } return bodyPart; }
setContent(body, buf, altType == null ? contentType : altType); if (body != msg) { final MimeMultipart multipart = new MimeMultipart(); multipart.addBodyPart((BodyPart) body); multipart.addBodyPart(parts[i]);
private Object createMessageContent() throws IOException, MessagingException { MimeMultipart content = new MimeMultipart(); MimeBodyPart contentText = new MimeBodyPart(); contentText.setText( "Hello World!" ); content.addBodyPart( contentText ); MimeBodyPart contentFile = new MimeBodyPart(); File testFile = TestUtils.getInputFile( "GetPOP", "txt" ); FileDataSource fds = new FileDataSource( testFile.getAbsolutePath() ); contentFile.setDataHandler( new DataHandler( fds ) ); contentFile.setFileName( testFile.getName() ); content.addBodyPart( contentFile ); return content; }
/** * 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); } }