Example usage for javax.mail.internet MimeBodyPart setHeader

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

Introduction

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

Prototype

@Override
public void setHeader(String name, String value) throws MessagingException 

Source Link

Document

Set the value for this header_name.

Usage

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

private MimeBodyPart createHTMLBodyPart(String content, boolean hasAttachments) throws MessagingException {
    MimeBodyPart htmlPart = new MimeBodyPart();
    htmlPart.setContent(content, TEXT_HTML_CONTENT_TYPE);
    htmlPart.setHeader("Content-Type", TEXT_HTML_CONTENT_TYPE);
    if (hasAttachments) {
        htmlPart.setHeader("Content-Disposition", "inline");
        htmlPart.setHeader("Content-Transfer-Encoding", "quoted-printable");
    }/*from   w w  w. ja va  2  s . co  m*/
    return htmlPart;
}

From source file:org.xwiki.mail.internal.factory.text.TextMimeBodyPartFactory.java

@Override
public MimeBodyPart create(String content, Map<String, Object> parameters) throws MessagingException {
    // Create the body part of the email
    MimeBodyPart bodyPart = new MimeBodyPart();

    bodyPart.setContent(content, TEXT_PLAIN_CONTENT_TYPE);

    bodyPart.setHeader("Content-Type", getMimetype(parameters));

    // Handle headers passed as parameter
    addHeaders(bodyPart, parameters);//from   w  w  w . ja  v  a  2s  .  c o m

    return bodyPart;
}

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

private MimeBodyPart createHTMLBodyPart(String content, boolean hasAttachments) throws MessagingException {
    MimeBodyPart htmlPart = new MimeBodyPart();
    htmlPart.setContent(content, "text/html; charset=" + StandardCharsets.UTF_8.name());
    htmlPart.setHeader("Content-Type", "text/html");
    if (hasAttachments) {
        htmlPart.setHeader("Content-Disposition", "inline");
        htmlPart.setHeader("Content-Transfer-Encoding", "quoted-printable");
    }/*from  w  w  w  . ja v a2 s  . c  om*/
    return htmlPart;
}

From source file:rescustomerservices.GmailQuickstart.java

/**
 * Create a MimeMessage using the parameters provided.
 *
 * @param to Email address of the receiver.
 * @param from Email address of the sender, the mailbox account.
 * @param subject Subject of the email./*from   ww  w . j a  v  a 2s. co m*/
 * @param bodyText Body text of the email.
 * @param fileDir Path to the directory containing attachment.
 * @param filename Name of file to be attached.
 * @return MimeMessage to be used to send email.
 * @throws MessagingException
 */
public MimeMessage createEmailWithAttachment(String to, String from, String subject, String bodyText,
        String fileDir, String filename) throws MessagingException, IOException {
    Properties props = new Properties();
    Session session = Session.getDefaultInstance(props, null);

    MimeMessage email = new MimeMessage(session);
    InternetAddress tAddress = new InternetAddress(to);
    InternetAddress fAddress = new InternetAddress(from);

    email.setFrom(fAddress);
    email.addRecipient(javax.mail.Message.RecipientType.TO, tAddress);
    email.setSubject(subject);

    MimeBodyPart mimeBodyPart = new MimeBodyPart();
    mimeBodyPart.setContent(bodyText, "text/plain");
    mimeBodyPart.setHeader("Content-Type", "text/plain; charset=\"UTF-8\"");

    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(mimeBodyPart);

    mimeBodyPart = new MimeBodyPart();
    DataSource source = new FileDataSource(fileDir + filename);

    mimeBodyPart.setDataHandler(new DataHandler(source));
    mimeBodyPart.setFileName(filename);
    String contentType = Files.probeContentType(FileSystems.getDefault().getPath(fileDir, filename));
    mimeBodyPart.setHeader("Content-Type", contentType + "; name=\"" + filename + "\"");
    mimeBodyPart.setHeader("Content-Transfer-Encoding", "base64");

    multipart.addBodyPart(mimeBodyPart);

    email.setContent(multipart);

    return email;
}