Example usage for javax.mail.internet MimeBodyPart MimeBodyPart

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

Introduction

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

Prototype

public MimeBodyPart() 

Source Link

Document

An empty MimeBodyPart object is created.

Usage

From source file:mitm.common.mail.BodyPartUtils.java

public static MimeBodyPart makeContentBodyPart(MimePart sourcePart, HeaderMatcher matcher)
        throws MessagingException, IOException {
    MimeBodyPart newBodyPart = new MimeBodyPart();

    newBodyPart.setContent(sourcePart.getContent(), sourcePart.getContentType());

    HeaderUtils.copyHeaders(sourcePart, newBodyPart, matcher);

    return newBodyPart;
}

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  w  w w.  j  a v  a 2 s  .co  m

    return bodyPart;
}

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

/**
 * Send email with html content and attachments.
 * @param htmlBody//w  w w.  jav  a2s.c  o m
 * @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()));
            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.vosao.utils.EmailUtil.java

/**
 * Send email with html content and attachments.
 * @param htmlBody//from  w w  w.  ja  va 2  s .  c  om
 * @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.apache.usergrid.apm.service.util.Mailer.java

public static void send(String recipeintEmail, String subject, String messageText) {
    /*//ww  w  .j  ava2  s.com
     * It is a good practice to put this in a java.util.Properties file and
     * encrypt password. Scroll down to comments below to see how to use
     * java.util.Properties in JSF context.
     */
    Properties props = new Properties();
    try {
        props.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("conf/email.properties"));
    } catch (IOException e) {
        e.printStackTrace();
    }

    final String senderEmail = props.getProperty("mail.smtp.sender.email");
    final String smtpUser = props.getProperty("mail.smtp.user");
    final String senderName = props.getProperty("mail.smtp.sender.name");
    final String senderPassword = props.getProperty("senderPassword");
    final String emailtoCC = props.getProperty("instaopsOpsEmailtoCC");

    Session session = Session.getDefaultInstance(props, new Authenticator() {
        @Override
        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(smtpUser, senderPassword);
        }
    });
    session.setDebug(false);

    try {
        MimeMessage message = new MimeMessage(session);

        BodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setContent(messageText, "text/html");

        // Add message text
        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messageBodyPart);

        message.setContent(multipart);
        message.setSubject(subject);
        InternetAddress senderAddress = new InternetAddress(senderEmail, senderName);
        message.setFrom(senderAddress);
        message.addRecipient(Message.RecipientType.CC, new InternetAddress(emailtoCC));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipeintEmail));
        Transport.send(message);
        log.info("email sent");
    } catch (MessagingException m) {
        log.error(m.toString());
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:org.nuxeo.ecm.automation.client.jaxrs.impl.MultipartRequestEntity.java

public void setRequest(String content) throws MessagingException {
    MimeBodyPart part = new MimeBodyPart();
    part.setText(content, "UTF-8");
    part.setContentID("request");
    part.setHeader("Content-Type", Constants.CTYPE_REQUEST);
    part.setHeader("Content-Transfer-Encoding", "8bit");
    part.setHeader("Content-Length", Integer.toString(content.length()));
    mp.addBodyPart(part);//www . j  a v a  2  s  . c o  m
}

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 .jav a  2 s. c o m*/

    return bodyPart;
}

From source file:com.googlecode.ddom.mime.JavaMailTest.java

private void test(boolean preamble) throws Exception {
    MimeMultipart multipart = new MimeMultipart();

    MimeBodyPart bodyPart1 = new MimeBodyPart();
    StringBuilder buffer = new StringBuilder();
    for (int i = 0; i < 1000; i++) {
        buffer.append('(');
        buffer.append(i);/*from  ww w . j  a v a 2s  . c  om*/
        buffer.append(')');
    }
    String content1 = buffer.toString();
    bodyPart1
            .setDataHandler(new DataHandler(new ByteArrayDataSource(content1.getBytes("UTF-8"), "text/plain")));
    Map<String, String> headers1 = new HashMap<String, String>();
    headers1.put("Content-ID", "<1@example.com>");
    headers1.put("Content-Type", "text/plain; charset=UTF-8");
    setHeaders(bodyPart1, headers1);
    multipart.addBodyPart(bodyPart1);

    MimeBodyPart bodyPart2 = new MimeBodyPart();
    byte[] content2 = new byte[10000];
    new Random().nextBytes(content2);
    bodyPart2.setDataHandler(new DataHandler(new ByteArrayDataSource(content2, "application/octet-stream")));
    Map<String, String> headers2 = new HashMap<String, String>();
    headers2.put("Content-ID", "<2@example.com>");
    headers2.put("Content-Type", "application/octet-stream");
    setHeaders(bodyPart2, headers2);
    multipart.addBodyPart(bodyPart2);

    if (preamble) {
        multipart.setPreamble("This is a MIME multipart.");
    }

    String boundary = new ContentType(multipart.getContentType()).getParameter("boundary");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    multipart.writeTo(baos);
    MultipartReader mpr = new MultipartReader(new ByteArrayInputStream(baos.toByteArray()), boundary);
    assertTrue(mpr.nextPart());
    assertEquals(headers1, readHeaders(mpr));
    assertEquals(content1, IOUtils.toString(mpr.getContent(), "UTF-8"));
    assertTrue(mpr.nextPart());
    assertEquals(headers2, readHeaders(mpr));
    assertArrayEquals(content2, IOUtils.toByteArray(mpr.getContent()));
    assertFalse(mpr.nextPart());
}

From source file:org.nuxeo.ecm.automation.client.jaxrs.impl.MultipartRequestEntity.java

protected void setBlob(Blob blob, String id) throws MessagingException, IOException {
    MimeBodyPart part = new MimeBodyPart();
    if (blob instanceof HasFile) {
        part.attachFile(((HasFile) blob).getFile());
    } else {/*w  ww.  ja va  2  s .c  o  m*/
        part.setDataHandler(new DataHandler(new BlobDataSource(blob)));
        part.setFileName(blob.getFileName());
    }
    part.setHeader("Content-Type", blob.getMimeType());
    part.setHeader("Content-Transfer-Encoding", "binary");
    int length = blob.getLength();
    if (length > -1) {
        part.setHeader("Content-Length", Integer.toString(length));
    }
    part.setContentID(id);
    mp.addBodyPart(part);
}

From source file:net.sourceforge.vulcan.mailer.MessageAssembler.java

private void addMultipartBody(Multipart multipart, String type, String content) throws MessagingException {
    MimeBodyPart bp = new MimeBodyPart();

    bp.setContent(content, type);//  ww w .java2s  .  c om

    multipart.addBodyPart(bp);
}