Example usage for javax.mail.internet MimeBodyPart setContent

List of usage examples for javax.mail.internet MimeBodyPart setContent

Introduction

In this page you can find the example usage for javax.mail.internet MimeBodyPart setContent.

Prototype

@Override
public void setContent(Multipart mp) throws MessagingException 

Source Link

Document

This method sets the body part's content to a Multipart object.

Usage

From source file:com.jwm123.loggly.reporter.ReportMailer.java

public MimeBodyPart newMultipartBodyPart(Multipart multipart) throws MessagingException {
    MimeBodyPart mimeBodyPart = new MimeBodyPart();
    mimeBodyPart.setContent(multipart);
    return mimeBodyPart;
}

From source file:org.xwiki.mail.internal.factory.html.HTMLMimeBodyPartFactory.java

/**
 * @return Multipart part of the email, define the HTML as a multipart/alternative
 *//* w  ww  .j  av a2  s.c  om*/
private MimeBodyPart createAlternativePart(MimeBodyPart htmlBodyPart, MimeBodyPart textBodyPart)
        throws MessagingException {
    MimeMultipart alternativeMultiPart = new MimeMultipart("alternative");

    // Note: it's important to have the text before the HTML, from low fidelity to high fidelity according to the
    // MIME specification. Otherwise some readers will display text even though there's HTML in your mail message.
    alternativeMultiPart.addBodyPart(textBodyPart);
    alternativeMultiPart.addBodyPart(htmlBodyPart);

    MimeBodyPart alternativePartWrapper = new MimeBodyPart();
    alternativePartWrapper.setContent(alternativeMultiPart);
    return alternativePartWrapper;
}

From source file:org.xwiki.mail.internal.factory.html.HTMLMimeBodyPartFactory.java

@Override
public MimeBodyPart create(String content, Map<String, Object> parameters) throws MessagingException {
    MimeBodyPart resultBodyPart;/*from   w  w  w  .j a  v  a 2  s . c o  m*/

    // Separate normal attachment from embedded image attachments
    List<Attachment> allAttachments = (List<Attachment>) parameters.get("attachments");
    Pair<List<Attachment>, List<Attachment>> attachmentPairs = separateAttachments(content, allAttachments);
    List<Attachment> embeddedImageAttachments = attachmentPairs.getLeft();
    List<Attachment> normalAttachments = attachmentPairs.getRight();

    // Step 1: Handle the HTML section of the mail.
    MimeBodyPart htmlBodyPart;
    if (!embeddedImageAttachments.isEmpty()) {
        htmlBodyPart = new MimeBodyPart();
        htmlBodyPart.setContent(createHTMLMultipart(content, embeddedImageAttachments));
    } else {
        // Create the HTML body part of the email
        htmlBodyPart = createHTMLBodyPart(content, false);
    }

    // Step 2: Handle the optional alternative text
    String alternativeText = (String) parameters.get("alternate");
    if (alternativeText != null) {
        resultBodyPart = createAlternativePart(htmlBodyPart,
                this.defaultPartFactory.create(alternativeText, Collections.<String, Object>emptyMap()));
    } else {
        // No alternative text, just add the HTML body part to the Multipart
        resultBodyPart = htmlBodyPart;
    }

    // Step 3 Add the normal attachments (if any). Any embedded images have already been handled in the HTML body
    // part. Note: If there are attachments we need to wrap our body part inside a "mixed" Multipart.
    if (!normalAttachments.isEmpty()) {
        MimeMultipart multipart = new MimeMultipart("mixed");
        multipart.addBodyPart(resultBodyPart);
        handleAttachments(multipart, normalAttachments);
        resultBodyPart = new MimeBodyPart();
        resultBodyPart.setContent(multipart);
    }

    // Handle headers passed as parameter
    addHeaders(resultBodyPart, parameters);

    return resultBodyPart;
}

From source file:org.snopoke.util.Emailer.java

public void send() throws MessagingException {
    notNull(from, "From address can not be null");
    isTrue(!to.isEmpty() || !bcc.isEmpty(), "No TO or BCC addresses specified");

    MimeMessage message = getMessasge();

    // Cover wrap
    MimeBodyPart wrap = new MimeBodyPart();
    MimeMultipart cover = getCoverPart();
    wrap.setContent(cover);

    MimeMultipart content = new MimeMultipart("related");
    message.setContent(content);//from  w w w  . j a v  a  2s  . c  om
    content.addBodyPart(wrap);

    addAttachments(content);

    Transport.send(message);
}

From source file:org.pentaho.reporting.platform.plugin.SimpleEmailComponent.java

private Multipart processAttachments(final Multipart multipartBody) throws MessagingException, IOException {

    if (getAttachmentContent() == null) {

        // We don't have a first attachment, won't even search for the others.
        return multipartBody;

    }/*from w  w w .  j av  a2  s.c  o m*/

    // We have attachments; Creating a multipart-mixed

    final MimeMultipart mixedMultipart = new MimeMultipart("mixed");

    // Add the first part
    final MimeBodyPart bodyPart = new MimeBodyPart();
    bodyPart.setContent(multipartBody);
    mixedMultipart.addBodyPart(bodyPart);

    // Process each of the attachments we have
    processSpecificAttachment(mixedMultipart, getAttachmentContent());
    processSpecificAttachment(mixedMultipart, getAttachmentContent2());
    processSpecificAttachment(mixedMultipart, getAttachmentContent3());

    return mixedMultipart;

}

From source file:org.tizzit.util.mail.Mail.java

public boolean sendHtmlMail(String alternativePlaintextBody) {
    try {// www.j a v a 2 s.  com
        if (this.attachments.size() > 0) {
            MimeMultipart mainMultiPart = new MimeMultipart("mixed");
            if (alternativePlaintextBody != null) {
                MimeMultipart alternativeMultiPart = new MimeMultipart("alternative");
                MimeBodyPart plainTextBodyPart = new MimeBodyPart();
                MimeBodyPart htmlBodyPart = new MimeBodyPart();
                plainTextBodyPart.setText(alternativePlaintextBody, this.encoding, "plain");
                htmlBodyPart.setText(this.messageText, this.encoding, "html");
                alternativeMultiPart.addBodyPart(plainTextBodyPart);
                alternativeMultiPart.addBodyPart(htmlBodyPart);

                MimeBodyPart containerBodyPart = new MimeBodyPart();
                containerBodyPart.setContent(alternativeMultiPart);
                mainMultiPart.addBodyPart(containerBodyPart);
            } else { // without plain text alternative
                MimeBodyPart htmlBodyPart = new MimeBodyPart();
                htmlBodyPart.setText(this.messageText, this.encoding, "html");
                mainMultiPart.addBodyPart(htmlBodyPart);
            }
            for (int i = 0; i < this.attachments.size(); i++) {
                mainMultiPart.addBodyPart(this.attachments.get(i));
            }
            this.message.setContent(mainMultiPart);
        } else { // no attachments
            if (alternativePlaintextBody != null) {
                MimeMultipart mainMultipart = new MimeMultipart("alternative");
                MimeBodyPart plainTextBodyPart = new MimeBodyPart();
                MimeBodyPart htmlBodyPart = new MimeBodyPart();
                plainTextBodyPart.setText(alternativePlaintextBody, this.encoding, "plain");
                htmlBodyPart.setText(this.messageText, this.encoding, "html");
                mainMultipart.addBodyPart(plainTextBodyPart);
                mainMultipart.addBodyPart(htmlBodyPart);
                this.message.setContent(mainMultipart);
            } else { // no alternative plain text neither -> no MimeMessage!
                this.message.setContent(this.messageText, "text/html");
            }
        }
        this.message.saveChanges();
        doSend();
    } catch (MessagingException exception) {
        log.error("Error sending HTML mail", exception);
        return false;
    }
    return true;
}

From source file:com.szmslab.quickjavamail.send.MailSender.java

/**
 * ?????// ww  w.j  a v a2s  .com
 *
 * @param message
 *            
 * @throws MessagingException
 * @throws UnsupportedEncodingException
 */
private void setContent(MimeMessage message) throws MessagingException, UnsupportedEncodingException {
    if (StringUtils.isBlank(html)) {
        if (attachmentFileList.isEmpty()) {
            /*
             * text/plain
             */
            message.setText(text, charset);
        } else {
            /*
             * multipart/mixed
             *  text/plain
             *  attachment
             */
            Multipart mixedMultipart = createMixedMimeMultipart();
            mixedMultipart.addBodyPart(createTextPart());
            for (AttachmentFile file : attachmentFileList) {
                mixedMultipart.addBodyPart(createAttachmentPart(file));
            }
            message.setContent(mixedMultipart);
        }
    } else {
        if (attachmentFileList.isEmpty()) {
            if (inlineImageFileList.isEmpty()) {
                /*
                 * multipart/alternative
                 *  text/plain
                 *  text/html
                 */
                Multipart alternativeMultipart = createAlternativeMimeMultipart();
                alternativeMultipart.addBodyPart(createTextPart());
                alternativeMultipart.addBodyPart(createHtmlPart());
                message.setContent(alternativeMultipart);
            } else {
                /*
                 * multipart/alternative
                 *  text/plain
                 *  mulpart/related
                 *    text/html
                 *    image
                 */
                Multipart relatedMultipart = createRelatedMimeMultipart();
                relatedMultipart.addBodyPart(createHtmlPart());
                for (InlineImageFile file : inlineImageFileList) {
                    relatedMultipart.addBodyPart(createImagePart(file));
                }
                MimeBodyPart relatedPart = new MimeBodyPart();
                relatedPart.setContent(relatedMultipart);

                Multipart alternativeMultipart = createAlternativeMimeMultipart();
                alternativeMultipart.addBodyPart(createTextPart());
                alternativeMultipart.addBodyPart(relatedPart);
                message.setContent(alternativeMultipart);
            }
        } else {
            if (inlineImageFileList.isEmpty()) {
                /*
                 * multipart/mixed
                 *  mulpart/alternative
                 *   text/plain
                 *   text/html
                 *  attachment
                 */
                Multipart alternativeMultipart = createAlternativeMimeMultipart();
                alternativeMultipart.addBodyPart(createTextPart());
                alternativeMultipart.addBodyPart(createHtmlPart());
                MimeBodyPart alternativePart = new MimeBodyPart();
                alternativePart.setContent(alternativeMultipart);

                Multipart mixedMultipart = createMixedMimeMultipart();
                mixedMultipart.addBodyPart(alternativePart);
                for (AttachmentFile file : attachmentFileList) {
                    mixedMultipart.addBodyPart(createAttachmentPart(file));
                }
                message.setContent(mixedMultipart);
            } else {
                /*
                 * multipart/mixed
                 *  mulpart/alternative
                 *   text/plain
                 *   mulpart/related
                 *     text/html
                 *     image
                 *  attachment
                 */
                Multipart relatedMultipart = createRelatedMimeMultipart();
                relatedMultipart.addBodyPart(createHtmlPart());
                for (InlineImageFile file : inlineImageFileList) {
                    relatedMultipart.addBodyPart(createImagePart(file));
                }
                MimeBodyPart relatedPart = new MimeBodyPart();
                relatedPart.setContent(relatedMultipart);

                Multipart alternativeMultipart = createAlternativeMimeMultipart();
                alternativeMultipart.addBodyPart(createTextPart());
                alternativeMultipart.addBodyPart(relatedPart);
                MimeBodyPart alternativePart = new MimeBodyPart();
                alternativePart.setContent(alternativeMultipart);

                Multipart mixedMultipart = createMixedMimeMultipart();
                mixedMultipart.addBodyPart(alternativePart);
                for (AttachmentFile file : attachmentFileList) {
                    mixedMultipart.addBodyPart(createAttachmentPart(file));
                }
                message.setContent(mixedMultipart);
            }
        }
    }
    setHeaderToPart(message);
}

From source file:fr.xebia.cloud.amazon.aws.iam.AmazonAwsIamAccountCreator.java

private void sendEmail(Map<String, String> templatesParams, List<BodyPart> attachments, String toAddress)
        throws MessagingException {

    MimeBodyPart htmlAndPlainTextAlternativeBody = new MimeBodyPart();

    // TEXT AND HTML MESSAGE (gmail requires plain text alternative, otherwise, it displays the 1st plain text attachment in the preview)
    MimeMultipart cover = new MimeMultipart("alternative");
    htmlAndPlainTextAlternativeBody.setContent(cover);
    BodyPart textHtmlBodyPart = new MimeBodyPart();
    String textHtmlBody = FreemarkerUtils.generate(templatesParams,
            "/fr/xebia/cloud/amazon/aws/iam/amazon-aws-iam-credentials-email-" + environment.getIdentifier()
                    + ".html.ftl");
    textHtmlBodyPart.setContent(textHtmlBody, "text/html");
    cover.addBodyPart(textHtmlBodyPart);

    BodyPart textPlainBodyPart = new MimeBodyPart();
    cover.addBodyPart(textPlainBodyPart);
    String textPlainBody = FreemarkerUtils.generate(templatesParams,
            "/fr/xebia/cloud/amazon/aws/iam/amazon-aws-iam-credentials-email-" + environment.getIdentifier()
                    + ".txt.ftl");
    textPlainBodyPart.setContent(textPlainBody, "text/plain");

    MimeMultipart content = new MimeMultipart("related");
    content.addBodyPart(htmlAndPlainTextAlternativeBody);

    // ATTACHMENTS
    for (BodyPart bodyPart : attachments) {
        content.addBodyPart(bodyPart);/*from ww w .j a  v a2s  . com*/
    }

    MimeMessage msg = new MimeMessage(mailSession);

    msg.setFrom(mailFrom);
    msg.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(toAddress));
    msg.addRecipient(javax.mail.Message.RecipientType.CC, mailFrom);

    String subject = "[Xebia Amazon AWS " + environment.getIdentifier() + "] Credentials";

    msg.setSubject(subject);
    msg.setContent(content);

    mailTransport.sendMessage(msg, msg.getAllRecipients());
}

From source file:lucee.runtime.net.smtp.SMTPClient.java

private MimeMessageAndSession createMimeMessage(lucee.runtime.config.Config config, String hostName, int port,
        String username, String password, long lifeTimesan, long idleTimespan, boolean tls, boolean ssl,
        boolean newConnection) throws MessagingException {

    SessionAndTransport sat = getSessionAndTransport(config, hostName, port, username, password, lifeTimesan,
            idleTimespan, timeout, tls, ssl, newConnection);
    /*Properties props = createProperties(config,hostName,port,username,password,tls,ssl,timeout);
    Authenticator auth=null;/*  w ww. j  av a  2s. co m*/
     if(!StringUtil.isEmpty(username)) 
        auth=new SMTPAuthenticator( username, password );
             
               
       SessionAndTransport sat = newConnection?new SessionAndTransport(hash(props), props, auth,lifeTimesan,idleTimespan):
        SMTPConnectionPool.getSessionAndTransport(props,hash(props),auth,lifeTimesan,idleTimespan);
    */
    // Contacts
    SMTPMessage msg = new SMTPMessage(sat.session);
    if (from == null)
        throw new MessagingException("you have do define the from for the mail");
    //if(tos==null)throw new MessagingException("you have do define the to for the mail"); 

    checkAddress(from, charset);
    //checkAddress(tos,charset);

    msg.setFrom(from);
    //msg.setRecipients(Message.RecipientType.TO, tos);

    if (tos != null) {
        checkAddress(tos, charset);
        msg.setRecipients(Message.RecipientType.TO, tos);
    }
    if (ccs != null) {
        checkAddress(ccs, charset);
        msg.setRecipients(Message.RecipientType.CC, ccs);
    }
    if (bccs != null) {
        checkAddress(bccs, charset);
        msg.setRecipients(Message.RecipientType.BCC, bccs);
    }
    if (rts != null) {
        checkAddress(rts, charset);
        msg.setReplyTo(rts);
    }
    if (fts != null) {
        checkAddress(fts, charset);
        msg.setEnvelopeFrom(fts[0].toString());
    }

    // Subject and headers
    try {
        msg.setSubject(MailUtil.encode(subject, charset));
    } catch (UnsupportedEncodingException e) {
        throw new MessagingException("the encoding " + charset + " is not supported");
    }
    msg.setHeader("X-Mailer", xmailer);

    msg.setHeader("Date", getNow(timeZone));
    //msg.setSentDate(new Date());

    Multipart mp = null;

    // Only HTML
    if (plainText == null) {
        if (ArrayUtil.isEmpty(attachmentz) && ArrayUtil.isEmpty(parts)) {
            fillHTMLText(msg);
            setHeaders(msg, headers);
            return new MimeMessageAndSession(msg, sat);
        }
        mp = new MimeMultipart("related");
        mp.addBodyPart(getHTMLText());
    }
    // only Plain
    else if (htmlText == null) {
        if (ArrayUtil.isEmpty(attachmentz) && ArrayUtil.isEmpty(parts)) {
            fillPlainText(msg);
            setHeaders(msg, headers);
            return new MimeMessageAndSession(msg, sat);
        }
        mp = new MimeMultipart();
        mp.addBodyPart(getPlainText());
    }
    // Plain and HTML
    else {
        mp = new MimeMultipart("alternative");
        mp.addBodyPart(getPlainText());
        mp.addBodyPart(getHTMLText());

        if (!ArrayUtil.isEmpty(attachmentz) || !ArrayUtil.isEmpty(parts)) {
            MimeBodyPart content = new MimeBodyPart();
            content.setContent(mp);
            mp = new MimeMultipart();
            mp.addBodyPart(content);
        }
    }

    // parts
    if (!ArrayUtil.isEmpty(parts)) {
        Iterator<MailPart> it = parts.iterator();
        if (mp instanceof MimeMultipart)
            ((MimeMultipart) mp).setSubType("alternative");
        while (it.hasNext()) {
            mp.addBodyPart(toMimeBodyPart(it.next()));
        }
    }

    // Attachments
    if (!ArrayUtil.isEmpty(attachmentz)) {
        for (int i = 0; i < attachmentz.length; i++) {
            mp.addBodyPart(toMimeBodyPart(mp, config, attachmentz[i]));
        }
    }
    msg.setContent(mp);
    setHeaders(msg, headers);

    return new MimeMessageAndSession(msg, sat);
}

From source file:org.pentaho.reporting.platform.plugin.SimpleEmailComponent.java

private Multipart getMultipartBody(final Session session) throws MessagingException, IOException {

    // if we have a mimeMessage, use it. Otherwise, build one with what we have
    // We can have both a messageHtml and messageText. Build according to it

    MimeMultipart parentMultipart = new MimeMultipart();
    MimeBodyPart htmlBodyPart = null, textBodyPart = null;

    if (getMimeMessage() != null) {

        // Rebuild a MimeMessage and use this one

        final MimeBodyPart original = new MimeBodyPart();
        final MimeMessage originalMimeMessage = new MimeMessage(session, getMimeMessage().getInputStream());
        final MimeMultipart relatedMultipart = (MimeMultipart) originalMimeMessage.getContent();

        parentMultipart = relatedMultipart;

        htmlBodyPart = new MimeBodyPart();
        htmlBodyPart.setContent(relatedMultipart);

    }//from  ww w  .java 2s.c om

    // The information we have in the mime-message overrides the getMessageHtml.
    if (getMessageHtml() != null && htmlBodyPart != null) {

        final String content = getInputString(getMessageHtml());

        htmlBodyPart = new MimeBodyPart();
        htmlBodyPart.setContent(content, "text/html; charset=" + LocaleHelper.getSystemEncoding());
        final MimeMultipart htmlMultipart = new MimeMultipart();
        htmlMultipart.addBodyPart(htmlBodyPart);

        parentMultipart = htmlMultipart;
    }

    if (getMessagePlain() != null) {

        final String content = getInputString(getMessagePlain());

        textBodyPart = new MimeBodyPart();
        textBodyPart.setContent(content, "text/plain; charset=" + LocaleHelper.getSystemEncoding());
        final MimeMultipart textMultipart = new MimeMultipart();
        textMultipart.addBodyPart(textBodyPart);

        parentMultipart = textMultipart;
    }

    // We have both text and html? Encapsulate it in a multipart/alternative

    if (htmlBodyPart != null && textBodyPart != null) {

        final MimeMultipart alternative = new MimeMultipart("alternative");
        alternative.addBodyPart(textBodyPart);
        alternative.addBodyPart(htmlBodyPart);

        parentMultipart = alternative;

    }

    return parentMultipart;

}