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.tizzit.util.mail.MailHelper.java

/**
 * Send an email in html-format in iso-8859-1-encoding
 * //from  w w w  .j  a  va2 s  . c  o  m
 * @param subject the subject of the new mail
 * @param htmlMessage the content of the mail as html
 * @param alternativeTextMessage the content of the mail as text
 * @param from the sender-address
 * @param to the receiver-address
 * @param cc the address of the receiver of a copy of this mail
 * @param bcc the address of the receiver of a blind-copy of this mail
 */
public static void sendHtmlMail2(String subject, String htmlMessage, String alternativeTextMessage, String from,
        String to, String cc, String bcc) {
    try {
        MimeMessage msg = new MimeMessage(mailSession);
        msg.setFrom(new InternetAddress(from));
        if (cc != null && !cc.equals(""))
            msg.setRecipients(Message.RecipientType.CC, cc);
        if (bcc != null && !bcc.equals(""))
            msg.setRecipients(Message.RecipientType.BCC, bcc);
        msg.addRecipients(Message.RecipientType.TO, to);
        msg.setSubject(subject, "ISO8859_1");
        msg.setSentDate(new Date());

        MimeMultipart multiPart = new MimeMultipart("alternative");
        MimeBodyPart htmlPart = new MimeBodyPart();
        MimeBodyPart textPart = new MimeBodyPart();

        textPart.setText(alternativeTextMessage, "ISO8859_1");
        textPart.setHeader("MIME-Version", "1.0");
        //textPart.setHeader("Content-Type", textPart.getContentType());
        textPart.setHeader("Content-Type", "text/plain;charset=\"ISO-8859-1\"");

        htmlPart.setContent(htmlMessage, "text/html;charset=\"ISO8859_1\"");
        htmlPart.setHeader("MIME-Version", "1.0");
        htmlPart.setHeader("Content-Type", "text/html;charset=\"ISO-8859-1\"");
        //htmlPart.setHeader("Content-Type", htmlPart.getContentType());

        multiPart.addBodyPart(textPart);
        multiPart.addBodyPart(htmlPart);

        multiPart.setSubType("alternative");

        msg.setContent(multiPart);
        msg.setHeader("MIME-Version", "1.0");
        msg.setHeader("Content-Type", multiPart.getContentType());

        Transport.send(msg);
    } catch (Exception e) {
        log.error("Error sending html-mail: " + e.getLocalizedMessage());
    }
}

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

/**
 * Send an email in html-format in iso-8859-1-encoding
 * //from   w ww  .  ja v a  2  s  . c o m
 * @param subject the subject of the new mail
 * @param htmlMessage the content of the mail as html
 * @param alternativeTextMessage the content of the mail as text
 * @param from the sender-address
 * @param to the receiver-address
 * @param cc the address of the receiver of a copy of this mail
 * @param bcc the address of the receiver of a blind-copy of this mail
 */
public static void sendHtmlMail2(String subject, String htmlMessage, String alternativeTextMessage, String from,
        String[] to, String[] cc, String[] bcc) {
    try {
        MimeMessage msg = new MimeMessage(mailSession);
        msg.setFrom(new InternetAddress(from));

        if (cc != null && cc.length != 0) {
            for (int i = cc.length - 1; i >= 0; i--) {
                msg.addRecipients(Message.RecipientType.CC, cc[i]);
            }
        }
        if (bcc != null && bcc.length != 0) {
            for (int i = bcc.length - 1; i >= 0; i--) {
                msg.addRecipients(Message.RecipientType.BCC, bcc[i]);
            }
        }
        if (to != null && to.length != 0) {
            for (int i = to.length - 1; i >= 0; i--) {
                msg.addRecipients(Message.RecipientType.TO, to[i]);
            }
        }
        msg.setSubject(subject, "ISO8859_1");
        msg.setSentDate(new Date());

        MimeMultipart multiPart = new MimeMultipart("alternative");
        MimeBodyPart htmlPart = new MimeBodyPart();
        MimeBodyPart textPart = new MimeBodyPart();

        textPart.setText(alternativeTextMessage, "ISO8859_1");
        textPart.setHeader("MIME-Version", "1.0");
        //textPart.setHeader("Content-Type", textPart.getContentType());
        textPart.setHeader("Content-Type", "text/plain;charset=\"ISO-8859-1\"");

        htmlPart.setContent(htmlMessage, "text/html;charset=\"ISO8859_1\"");
        htmlPart.setHeader("MIME-Version", "1.0");
        htmlPart.setHeader("Content-Type", "text/html;charset=\"ISO-8859-1\"");
        //htmlPart.setHeader("Content-Type", htmlPart.getContentType());

        multiPart.addBodyPart(textPart);
        multiPart.addBodyPart(htmlPart);

        multiPart.setSubType("alternative");

        msg.setContent(multiPart);
        msg.setHeader("MIME-Version", "1.0");
        msg.setHeader("Content-Type", multiPart.getContentType());

        Transport.send(msg);
    } catch (Exception e) {
        log.error("Error sending html-mail: " + e.getLocalizedMessage());
    }
}

From source file:org.vosao.utils.EmailUtil.java

/**
 * Send email with html content and attachments.
 * @param htmlBody/*  w  w  w. j  a  v a2 s  .com*/
 * @param subject
 * @param fromAddress
 * @param fromText
 * @param toAddress
 * @return null if OK or error message.
 */
public static String sendEmail(final String htmlBody, final String subject, final String fromAddress,
        final String fromText, final String toAddress, final List<FileItem> files) {

    Properties props = new Properties();
    Session session = Session.getDefaultInstance(props, null);
    try {
        Multipart mp = new MimeMultipart();
        MimeBodyPart htmlPart = new MimeBodyPart();
        htmlPart.setContent(htmlBody, "text/html");
        htmlPart.setHeader("Content-type", "text/html; charset=UTF-8");
        mp.addBodyPart(htmlPart);
        for (FileItem item : files) {
            MimeBodyPart attachment = new MimeBodyPart();
            attachment.setFileName(item.getFilename());
            String mimeType = MimeType.getContentTypeByExt(FolderUtil.getFileExt(item.getFilename()));
            if (mimeType.equals("text/plain")) {
                mimeType = MimeType.DEFAULT;
            }
            DataSource ds = new ByteArrayDataSource(item.getData(), mimeType);
            attachment.setDataHandler(new DataHandler(ds));
            mp.addBodyPart(attachment);
        }
        MimeMessage msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(fromAddress, fromText));
        msg.addRecipient(Message.RecipientType.TO, new InternetAddress(toAddress, toAddress));
        msg.setSubject(subject, "UTF-8");
        msg.setContent(mp);
        Transport.send(msg);
        return null;
    } catch (AddressException e) {
        return e.getMessage();
    } catch (MessagingException e) {
        return e.getMessage();
    } catch (UnsupportedEncodingException e) {
        return e.getMessage();
    }
}

From source file:org.wf.dp.dniprorada.util.Mail.java

public Mail _Attach(String sBody) {
    try {/*www. j  a  va  2 s .  c  o  m*/
        MimeBodyPart oMimeBodyPart = new MimeBodyPart();
        //oMimeBodyPart.setText(sBody,DEFAULT_ENCODING,"Content-Type: text/html;");
        oMimeBodyPart.setText(sBody, DEFAULT_ENCODING);
        //         oMimeBodyPart.setHeader("Content-Type", "text/html");
        oMimeBodyPart.setHeader("Content-Type", "text/html;charset=utf-8");
        oMultiparts.addBodyPart(oMimeBodyPart);
        log.info("[_Attach:sBody]:sBody=" + sBody);
    } catch (Exception oException) {
        log.error("[_Attach:sBody]", oException);
    }
    return this;
}

From source file:org.wf.dp.dniprorada.util.Mail.java

public Mail _Attach(DataSource oDataSource, String sFileName, String sDescription) {
    try {//  w  w w.  j  av a2  s  . com
        MimeBodyPart oMimeBodyPart = new MimeBodyPart();
        oMimeBodyPart.setHeader("Content-Type", "multipart/mixed");
        oMimeBodyPart.setDataHandler(new DataHandler(oDataSource));
        oMimeBodyPart.setFileName(MimeUtility.encodeText(sFileName));
        oMultiparts.addBodyPart(oMimeBodyPart);
        log.info("[_Attach:oFile]:sFileName=" + sFileName + ",sDescription=" + sDescription);
    } catch (Exception oException) {
        log.error("[_Attach:oFile]sFileName=" + sFileName + ",sDescription=" + sDescription, oException);
    }
    return this;
}

From source file:org.wf.dp.dniprorada.util.Mail.java

public Mail _Attach(URL oURL, String sName) {
    try {//from   ww  w.  ja  v a 2s. com
        MimeBodyPart oMimeBodyPart = new MimeBodyPart();//javax.activation
        oMimeBodyPart.setHeader("Content-Type", "multipart/mixed");
        DataSource oDataSource = new URLDataSource(oURL);
        oMimeBodyPart.setDataHandler(new DataHandler(oDataSource));
        //oPart.setFileName(MimeUtility.encodeText(source.getName()));
        oMimeBodyPart.setFileName(
                MimeUtility.encodeText(sName == null || "".equals(sName) ? oDataSource.getName() : sName));
        oMultiparts.addBodyPart(oMimeBodyPart);
        log.info("[_Attach:oURL]:sName=" + sName);
    } catch (Exception oException) {
        log.error("[_Attach:oURL]:sName=" + sName, oException);
    }
    return this;
}

From source file:org.xwiki.commons.internal.DefaultMailSender.java

private MimeBodyPart createAttachmentPart(Attachment attachment) {
    try {/*from  w w  w .  j  a v a 2 s .  c  o  m*/
        String name = attachment.getFilename();
        byte[] stream = attachment.getContent();
        File temp = File.createTempFile("tmpfile", ".tmp");
        FileOutputStream fos = new FileOutputStream(temp);
        fos.write(stream);
        fos.close();
        DataSource source = new FileDataSource(temp);
        MimeBodyPart part = new MimeBodyPart();
        String mimeType = MimeTypesUtil.getMimeTypeFromFilename(name);

        part.setDataHandler(new DataHandler(source));
        part.setHeader("Content-Type", mimeType);
        part.setFileName(name);
        part.setContentID("<" + name + ">");
        part.setDisposition("inline");

        temp.deleteOnExit();
        return part;
    } catch (Exception e) {
        return new MimeBodyPart();
    }
}

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

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

    // Save the attachment to a temporary file on the file system and wrap it in a Java Mail Data Source.
    DataSource source = createTemporaryAttachmentDataSource(attachment);
    attachmentPart.setDataHandler(new DataHandler(source));

    attachmentPart.setHeader("Content-Type", attachment.getMimeType());

    // Add a content-id so that we can uniquely reference this attachment. This is used for example to
    // display the attachment inline in some mail HTML content.
    // Note: According to http://tools.ietf.org/html/rfc2392 the id must be enclosed in angle brackets.
    attachmentPart.setHeader("Content-ID", "<" + attachment.getFilename() + ">");

    attachmentPart.setFileName(source.getName());

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

    return attachmentPart;
}

From source file:org.xwiki.mail.internal.DefaultMimeBodyPartFactory.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; charset=" + StandardCharsets.UTF_8.name());

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

    // Handle headers passed as parameter
    addHeaders(bodyPart, parameters);//from   ww  w .  j ava  2  s . co m

    return bodyPart;
}

From source file:org.xwiki.mail.internal.factory.attachment.AttachmentMimeBodyPartFactory.java

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

    // Save the attachment to a temporary file on the file system and wrap it in a Java Mail Data Source.
    DataSource source = createTemporaryAttachmentDataSource(attachment);
    attachmentPart.setDataHandler(new DataHandler(source));

    attachmentPart.setHeader("Content-Type", attachment.getMimeType());

    // Add a content-id so that we can uniquely reference this attachment. This is used for example to
    // display the attachment inline in some mail HTML content.
    // Note: According to http://tools.ietf.org/html/rfc2392 the id must be enclosed in angle brackets.
    attachmentPart.setHeader("Content-ID", "<" + attachment.getFilename() + ">");

    attachmentPart.setFileName(attachment.getFilename());

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

    return attachmentPart;
}