Search in sources :

Example 21 with MimeBodyPart

use of javax.mail.internet.MimeBodyPart in project jodd by oblac.

the class SendMailSession method createMessage.

// ---------------------------------------------------------------- adapter
/**
	 * Creates new JavaX message from {@link Email email}.
	 */
protected MimeMessage createMessage(Email email, Session session) throws MessagingException {
    MimeMessage msg = new MimeMessage(session);
    msg.setFrom(email.getFrom().toInternetAddress());
    // to
    int totalTo = email.getTo().length;
    InternetAddress[] address = new InternetAddress[totalTo];
    for (int i = 0; i < totalTo; i++) {
        address[i] = email.getTo()[i].toInternetAddress();
    }
    msg.setRecipients(Message.RecipientType.TO, address);
    // replyTo
    if (email.getReplyTo() != null) {
        int totalReplyTo = email.getReplyTo().length;
        address = new InternetAddress[totalReplyTo];
        for (int i = 0; i < totalReplyTo; i++) {
            address[i] = email.getReplyTo()[i].toInternetAddress();
        }
        msg.setReplyTo(address);
    }
    // cc
    if (email.getCc() != null) {
        int totalCc = email.getCc().length;
        address = new InternetAddress[totalCc];
        for (int i = 0; i < totalCc; i++) {
            address[i] = email.getCc()[i].toInternetAddress();
        }
        msg.setRecipients(Message.RecipientType.CC, address);
    }
    // bcc
    if (email.getBcc() != null) {
        int totalBcc = email.getBcc().length;
        address = new InternetAddress[totalBcc];
        for (int i = 0; i < totalBcc; i++) {
            address[i] = email.getBcc()[i].toInternetAddress();
        }
        msg.setRecipients(Message.RecipientType.BCC, address);
    }
    if (email.getSubjectEncoding() != null) {
        msg.setSubject(email.getSubject(), email.getSubjectEncoding());
    } else {
        msg.setSubject(email.getSubject());
    }
    Date date = email.getSentDate();
    if (date == null) {
        date = new Date();
    }
    msg.setSentDate(date);
    // headers
    Map<String, String> headers = email.getAllHeaders();
    if (headers != null) {
        for (Map.Entry<String, String> stringStringEntry : headers.entrySet()) {
            String value = stringStringEntry.getValue();
            msg.setHeader(stringStringEntry.getKey(), value);
        }
    }
    // message data and attachments
    final List<EmailMessage> messages = email.getAllMessages();
    final List<EmailAttachment> attachments = email.getAttachments() == null ? null : new ArrayList<>(email.getAttachments());
    final int totalMessages = messages.size();
    if ((attachments == null) && (totalMessages == 1)) {
        // special case: no attachments and just one content
        EmailMessage emailMessage = messages.get(0);
        msg.setContent(emailMessage.getContent(), emailMessage.getMimeType() + CHARSET + emailMessage.getEncoding());
    } else {
        Multipart multipart = new MimeMultipart();
        Multipart msgMultipart = multipart;
        if (totalMessages > 1) {
            MimeBodyPart bodyPart = new MimeBodyPart();
            msgMultipart = new MimeMultipart(ALTERNATIVE);
            bodyPart.setContent(msgMultipart);
            multipart.addBodyPart(bodyPart);
        }
        for (EmailMessage emailMessage : messages) {
            // detect embedded attachments
            List<EmailAttachment> embeddedAttachments = filterEmbeddedAttachments(attachments, emailMessage);
            MimeBodyPart bodyPart = new MimeBodyPart();
            if (embeddedAttachments == null) {
                // no embedded attachments, just add message
                bodyPart.setContent(emailMessage.getContent(), emailMessage.getMimeType() + CHARSET + emailMessage.getEncoding());
            } else {
                // embedded attachments detected, join them as related
                MimeMultipart relatedMultipart = new MimeMultipart(RELATED);
                MimeBodyPart messageData = new MimeBodyPart();
                messageData.setContent(emailMessage.getContent(), emailMessage.getMimeType() + CHARSET + emailMessage.getEncoding());
                relatedMultipart.addBodyPart(messageData);
                for (EmailAttachment att : embeddedAttachments) {
                    MimeBodyPart attBodyPart = createAttachmentBodyPart(att);
                    relatedMultipart.addBodyPart(attBodyPart);
                }
                bodyPart.setContent(relatedMultipart);
            }
            msgMultipart.addBodyPart(bodyPart);
        }
        if (attachments != null) {
            // attach remaining attachments
            for (EmailAttachment att : attachments) {
                MimeBodyPart attBodyPart = createAttachmentBodyPart(att);
                multipart.addBodyPart(attBodyPart);
            }
        }
        msg.setContent(multipart);
    }
    return msg;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) Date(java.util.Date) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart) Map(java.util.Map)

Example 22 with MimeBodyPart

use of javax.mail.internet.MimeBodyPart in project spring-framework by spring-projects.

the class MimeMessageHelper method addInline.

/**
	 * 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" -> "&lt;myId&gt;".
	 * 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);
}
Also used : DataHandler(javax.activation.DataHandler) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Example 23 with MimeBodyPart

use of javax.mail.internet.MimeBodyPart in project spring-framework by spring-projects.

the class MimeMessageHelper method createMimeMultiparts.

/**
	 * Determine the MimeMultipart objects to use, which will be used
	 * to store attachments on the one hand and text(s) and inline elements
	 * on the other hand.
	 * <p>Texts and inline elements can either be stored in the root element
	 * itself (MULTIPART_MODE_MIXED, MULTIPART_MODE_RELATED) or in a nested element
	 * rather than the root element directly (MULTIPART_MODE_MIXED_RELATED).
	 * <p>By default, the root MimeMultipart element will be of type "mixed"
	 * (MULTIPART_MODE_MIXED) or "related" (MULTIPART_MODE_RELATED).
	 * The main multipart element will either be added as nested element of
	 * type "related" (MULTIPART_MODE_MIXED_RELATED) or be identical to the root
	 * element itself (MULTIPART_MODE_MIXED, MULTIPART_MODE_RELATED).
	 * @param mimeMessage the MimeMessage object to add the root MimeMultipart
	 * object to
	 * @param multipartMode the multipart mode, as passed into the constructor
	 * (MIXED, RELATED, MIXED_RELATED, or NO)
	 * @throws MessagingException if multipart creation failed
	 * @see #setMimeMultiparts
	 * @see #MULTIPART_MODE_NO
	 * @see #MULTIPART_MODE_MIXED
	 * @see #MULTIPART_MODE_RELATED
	 * @see #MULTIPART_MODE_MIXED_RELATED
	 */
protected void createMimeMultiparts(MimeMessage mimeMessage, int multipartMode) throws MessagingException {
    switch(multipartMode) {
        case MULTIPART_MODE_NO:
            setMimeMultiparts(null, null);
            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;
        default:
            throw new IllegalArgumentException("Only multipart modes MIXED_RELATED, RELATED and NO supported");
    }
}
Also used : MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Example 24 with MimeBodyPart

use of javax.mail.internet.MimeBodyPart in project spring-framework by spring-projects.

the class MimeMessageHelper method getMainPart.

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;
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Example 25 with MimeBodyPart

use of javax.mail.internet.MimeBodyPart in project rest.li by linkedin.

the class MIMETestUtils method createTinyDataSource.

//Now create the javax data sources:
private static final MimeBodyPart createTinyDataSource() {
    try {
        //Tiny body.
        final String body = "1";
        final MimeBodyPart dataPart = new MimeBodyPart();
        final ContentType contentType = new ContentType(TEXT_PLAIN_CONTENT_TYPE);
        dataPart.setContent(body, contentType.getBaseType());
        return dataPart;
    } catch (Exception exception) {
        Assert.fail();
    }
    return null;
}
Also used : ContentType(javax.mail.internet.ContentType) ByteString(com.linkedin.data.ByteString) MimeBodyPart(javax.mail.internet.MimeBodyPart) IOException(java.io.IOException)

Aggregations

MimeBodyPart (javax.mail.internet.MimeBodyPart)172 MimeMultipart (javax.mail.internet.MimeMultipart)111 MimeMessage (javax.mail.internet.MimeMessage)65 MessagingException (javax.mail.MessagingException)64 IOException (java.io.IOException)49 DataHandler (javax.activation.DataHandler)39 ByteString (com.linkedin.data.ByteString)38 ByteArrayOutputStream (java.io.ByteArrayOutputStream)37 BodyPart (javax.mail.BodyPart)35 InternetAddress (javax.mail.internet.InternetAddress)31 ZMimeBodyPart (com.zimbra.common.zmime.ZMimeBodyPart)30 Test (org.testng.annotations.Test)28 ZMimeMultipart (com.zimbra.common.zmime.ZMimeMultipart)23 Multipart (javax.mail.Multipart)20 InputStream (java.io.InputStream)18 ArrayList (java.util.ArrayList)17 HashMap (java.util.HashMap)16 Session (javax.mail.Session)16 Date (java.util.Date)15 Properties (java.util.Properties)15