Example usage for javax.mail.internet MimeMessage setContentID

List of usage examples for javax.mail.internet MimeMessage setContentID

Introduction

In this page you can find the example usage for javax.mail.internet MimeMessage setContentID.

Prototype

public void setContentID(String cid) throws MessagingException 

Source Link

Document

Set the "Content-ID" header field of this Message.

Usage

From source file:org.grouter.common.mail.MailHandler.java

/**
 * Creates a multipart email with html and plain text email body to fallback to if
 * email client do not handle html base emails.
 *
 * @param mimeMessage//  w w  w . j  a  v a  2  s  .  c  o  m
 * @param dto
 * @throws MessagingException
 */
private void createHtmlAndPlainTextBody(MimeMessage mimeMessage, MailDto dto) throws MessagingException {
    if (StringUtils.isNotEmpty(dto.getHtmlTextBody())) {
        mimeMessage.setContentID(MailDto.CONTENT_TYPE_MULTIPART_ALTERNATIVE);

        // Create your text message part
        BodyPart plainBodyPart = new MimeBodyPart();
        plainBodyPart.setContent(dto.getPlainTextBody(), MailDto.CONTENT_TYPE_TEXT_PLAIN);

        // Create a multi-part to combine the parts
        Multipart multipart = new MimeMultipart("alternative");
        multipart.addBodyPart(plainBodyPart);

        StringBuilder sb = new StringBuilder();
        sb.append(PRE_HTML_HEADER);
        sb.append(dto.getSubject()).append("\n");
        sb.append(POST_HTML_HEADER);
        sb.append(dto.getHtmlTextBody());
        sb.append(END_HTML);

        BodyPart htmlBodyPart = new MimeBodyPart();
        htmlBodyPart.setContent(sb.toString(), MailDto.CONTENT_TYPE_TEXT_HTML);

        // Add html part to multi part
        multipart.addBodyPart(htmlBodyPart);
        mimeMessage.setContent(multipart);
    } else {
        mimeMessage.setSubject(dto.getSubject());
        mimeMessage.setText(dto.getPlainTextBody());
        mimeMessage.setContent(dto.getPlainTextBody(), MailDto.CONTENT_TYPE_TEXT_PLAIN);
    }

}