Example usage for javax.mail.internet MimeMultipart MimeMultipart

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

Introduction

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

Prototype

public MimeMultipart() 

Source Link

Document

Default constructor.

Usage

From source file:servlets.mailPDF_Bronze_Yougov.java

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    try {//from w ww.  j a va2s .co m
        System.out.println("Reached @ mailPDF_Bronze_Yougov.");

        String pdfBase64 = request.getParameter("pdfBase64");
        String reportdate = request.getParameter("reportdate");
        String emailaddresses = request.getParameter("emailList");
        String emailMessage = request.getParameter("emailMessage");

        List<String> emailList = Arrays.asList(emailaddresses.split("\\s*,\\s*"));

        System.out.println("pdfBase54 is --------------\n" + pdfBase64 + "\n-----------------End");
        System.out.println(reportdate);
        System.out.println(emailaddresses);

        byte[] decodedBytes = decode(pdfBase64.substring(28));
        MultiPartEmail email = new MultiPartEmail();
        email.setSmtpPort(587);
        email.setDebug(false);
        email.setHostName("smtp.gmail.com");
        email.setAuthentication("reporting@groupnp.com", "D3sign2015");
        email.setTLS(true);

        for (String emailAddress : emailList) {

            email.addTo(emailAddress);
        }

        email.setFrom("reporting@groupnp.com");
        email.setSubject("Yougov Tracker (" + reportdate + ")");

        MimeMultipart part1 = new MimeMultipart();
        BodyPart messageBodyPart1 = new MimeBodyPart();
        messageBodyPart1.setContent(emailMessage, "text/html; charset=utf-8");
        part1.addBodyPart(messageBodyPart1);
        email.addPart(part1);

        MimeMultipart part2 = new MimeMultipart();
        BodyPart messageBodyPart2 = new MimeBodyPart();
        messageBodyPart2
                .setDataHandler(new DataHandler(new ByteArrayDataSource(decodedBytes, "application/pdf")));
        messageBodyPart2.removeHeader("Content-Transfer-Encoding");
        messageBodyPart2.addHeader("Content-Transfer-Encoding", "base64");
        messageBodyPart2.setFileName("YougovTracker_" + reportdate + ".pdf");
        part2.addBodyPart(messageBodyPart2);
        email.addPart(part2);

        email.send();

    } catch (EmailException | MessagingException ex) {
        Logger.getLogger(mailPDF_Bronze_Yougov.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:servlets.mailPDF_Bronze.java

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    try {/*from   w  ww .  ja v a2 s .  co m*/
        System.out.println("Reached @ mailPDF_Bronze.");

        String pdfBase64 = request.getParameter("pdfBase64");
        String reportdate = request.getParameter("reportdate");
        String emailaddresses = request.getParameter("emailList");
        String emailMessage = request.getParameter("emailMessage");

        List<String> emailList = Arrays.asList(emailaddresses.split("\\s*,\\s*"));

        System.out.println("pdfBase54 is --------------\n" + pdfBase64 + "\n-----------------End");
        System.out.println(reportdate);
        System.out.println(emailaddresses);

        byte[] decodedBytes = decode(pdfBase64.substring(28));
        MultiPartEmail email = new MultiPartEmail();
        email.setSmtpPort(587);
        email.setDebug(false);
        email.setHostName("smtp.gmail.com");
        email.setAuthentication("reporting@groupnp.com", "D3sign2015");
        email.setTLS(true);

        for (String emailAddress : emailList) {

            email.addTo(emailAddress);
        }

        email.setFrom("reporting@groupnp.com");
        email.setSubject("LexisNexis Risk India Tracker (" + reportdate + ")");

        MimeMultipart part1 = new MimeMultipart();
        BodyPart messageBodyPart1 = new MimeBodyPart();
        messageBodyPart1.setContent(emailMessage, "text/html; charset=utf-8");
        part1.addBodyPart(messageBodyPart1);
        email.addPart(part1);

        MimeMultipart part2 = new MimeMultipart();
        BodyPart messageBodyPart2 = new MimeBodyPart();
        messageBodyPart2
                .setDataHandler(new DataHandler(new ByteArrayDataSource(decodedBytes, "application/pdf")));
        messageBodyPart2.removeHeader("Content-Transfer-Encoding");
        messageBodyPart2.addHeader("Content-Transfer-Encoding", "base64");
        messageBodyPart2.setFileName("LNIndiaTracker_" + reportdate + ".pdf");
        part2.addBodyPart(messageBodyPart2);
        email.addPart(part2);

        email.send();

    } catch (EmailException | MessagingException ex) {
        Logger.getLogger(mailPDF_Bronze.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:org.apache.james.transport.mailets.StripAttachmentTest.java

@Test
public void testSimpleAttachment() throws MessagingException, IOException {
    Mailet mailet = initMailet();/*from   w ww.  ja v a2 s  .co  m*/

    MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties()));

    MimeMultipart mm = new MimeMultipart();
    MimeBodyPart mp = new MimeBodyPart();
    mp.setText("simple text");
    mm.addBodyPart(mp);
    String body = "\u0023\u00A4\u00E3\u00E0\u00E9";
    MimeBodyPart mp2 = new MimeBodyPart(new ByteArrayInputStream(
            ("Content-Transfer-Encoding: 8bit\r\nContent-Type: application/octet-stream; charset=utf-8\r\n\r\n"
                    + body).getBytes("UTF-8")));
    mp2.setDisposition("attachment");
    mp2.setFileName("10.tmp");
    mm.addBodyPart(mp2);
    String body2 = "\u0014\u00A3\u00E1\u00E2\u00E4";
    MimeBodyPart mp3 = new MimeBodyPart(new ByteArrayInputStream(
            ("Content-Transfer-Encoding: 8bit\r\nContent-Type: application/octet-stream; charset=utf-8\r\n\r\n"
                    + body2).getBytes("UTF-8")));
    mp3.setDisposition("attachment");
    mp3.setFileName("temp.zip");
    mm.addBodyPart(mp3);
    message.setSubject("test");
    message.setContent(mm);
    message.saveChanges();

    Mail mail = FakeMail.builder().mimeMessage(message).build();

    mailet.service(mail);

    ByteArrayOutputStream rawMessage = new ByteArrayOutputStream();
    mail.getMessage().writeTo(rawMessage, new String[] { "Bcc", "Content-Length", "Message-ID" });

    @SuppressWarnings("unchecked")
    Collection<String> c = (Collection<String>) mail
            .getAttribute(StripAttachment.SAVED_ATTACHMENTS_ATTRIBUTE_KEY);
    Assert.assertNotNull(c);

    Assert.assertEquals(1, c.size());

    String name = c.iterator().next();

    File f = new File("./" + name);
    try {
        InputStream is = new FileInputStream(f);
        String savedFile = toString(is);
        is.close();
        Assert.assertEquals(body, savedFile);
    } finally {
        FileUtils.deleteQuietly(f);
    }
}

From source file:servlets.mailPDF_Gold_LexisNexis.java

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    try {/*from  ww  w.  ja  v a 2  s .c  om*/
        System.out.println("Reached @ mailPDF_Gold_LexisNexis.");

        String pdfBase64 = request.getParameter("pdfBase64");
        String reportdate = request.getParameter("reportdate");
        String emailaddresses = request.getParameter("emailList");
        String emailMessage = request.getParameter("emailMessage");
        String filename = request.getParameter("filename");

        List<String> emailList = Arrays.asList(emailaddresses.split("\\s*,\\s*"));

        System.out.println("pdfBase64 is --------------\n" + pdfBase64 + "\n-----------------End");
        System.out.println(reportdate);
        System.out.println(emailaddresses);

        byte[] decodedBytes = decode(pdfBase64.substring(28));
        MultiPartEmail email = new MultiPartEmail();
        email.setSmtpPort(587);
        email.setDebug(false);
        email.setHostName("smtp.gmail.com");
        email.setAuthentication("reporting@groupnp.com", "D3sign2015");
        email.setTLS(true);

        for (String emailAddress : emailList) {

            email.addTo(emailAddress);
        }

        email.setFrom("reporting@groupnp.com");
        email.setSubject(filename + " (" + reportdate + ")");

        MimeMultipart part1 = new MimeMultipart();
        BodyPart messageBodyPart1 = new MimeBodyPart();
        messageBodyPart1.setContent(emailMessage, "text/html; charset=utf-8");
        part1.addBodyPart(messageBodyPart1);
        email.addPart(part1);

        MimeMultipart part2 = new MimeMultipart();
        BodyPart messageBodyPart2 = new MimeBodyPart();
        messageBodyPart2
                .setDataHandler(new DataHandler(new ByteArrayDataSource(decodedBytes, "application/pdf")));
        messageBodyPart2.removeHeader("Content-Transfer-Encoding");
        messageBodyPart2.addHeader("Content-Transfer-Encoding", "base64");
        messageBodyPart2.setFileName(filename + " " + reportdate + ".pdf");
        part2.addBodyPart(messageBodyPart2);
        email.addPart(part2);

        email.send();

    } catch (EmailException | MessagingException ex) {
        Logger.getLogger(mailPDF_Gold_LexisNexis.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:servlets.mailPDF_Gold_CitrixChina.java

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    try {/*from w w w  .  j a  va2 s .c o m*/
        System.out.println("Reached @ mailPDF_Gold_CitrixChina.");

        String pdfBase64 = request.getParameter("pdfBase64");
        String reportdate = request.getParameter("reportdate");
        String emailaddresses = request.getParameter("emailList");
        String emailMessage = request.getParameter("emailMessage");
        String filename = request.getParameter("filename");

        List<String> emailList = Arrays.asList(emailaddresses.split("\\s*,\\s*"));

        System.out.println("pdfBase64 is --------------\n" + pdfBase64 + "\n-----------------End");
        System.out.println(reportdate);
        System.out.println(emailaddresses);

        byte[] decodedBytes = decode(pdfBase64.substring(28));
        MultiPartEmail email = new MultiPartEmail();
        email.setSmtpPort(587);
        email.setDebug(false);
        email.setHostName("smtp.gmail.com");
        email.setAuthentication("reporting@groupnp.com", "D3sign2015");
        email.setTLS(true);

        for (String emailAddress : emailList) {

            email.addTo(emailAddress);
        }

        email.setFrom("reporting@groupnp.com");
        email.setSubject(filename + " (" + reportdate + ")");

        MimeMultipart part1 = new MimeMultipart();
        BodyPart messageBodyPart1 = new MimeBodyPart();
        messageBodyPart1.setContent(emailMessage, "text/html; charset=utf-8");
        part1.addBodyPart(messageBodyPart1);
        email.addPart(part1);

        MimeMultipart part2 = new MimeMultipart();
        BodyPart messageBodyPart2 = new MimeBodyPart();
        messageBodyPart2
                .setDataHandler(new DataHandler(new ByteArrayDataSource(decodedBytes, "application/pdf")));
        messageBodyPart2.removeHeader("Content-Transfer-Encoding");
        messageBodyPart2.addHeader("Content-Transfer-Encoding", "base64");
        messageBodyPart2.setFileName(filename + " " + reportdate + ".pdf");
        part2.addBodyPart(messageBodyPart2);
        email.addPart(part2);

        email.send();

    } catch (EmailException | MessagingException ex) {
        Logger.getLogger(mailPDF_Gold_CitrixChina.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:org.chenillekit.mail.services.TestMailService.java

@Test
public void test_multipartemail_sending() throws EmailException, MessagingException {
    MultiPartEmail email = new MultiPartEmail();
    email.setSubject("Test Mail 2");
    email.addTo("homburgs@gmail.com");
    email.setFrom("homburgs@gmail.com");
    email.setMsg("This is a dummy message text!");
    email.addPart("This is a dummy message part 1!", "text/plain");

    MimeMultipart mmp = new MimeMultipart();
    MimeBodyPart mbp = new MimeBodyPart();

    mbp.setText("This is a dummy MimeBodyPart 1!");

    mmp.addBodyPart(mbp);//from  www  .ja va 2 s.c  o m
    email.addPart(mmp);

    EmailAttachment attachment = new EmailAttachment();
    attachment.setDescription("dummy.txt");
    attachment.setURL(new ClasspathResource("dummy.txt").toURL());
    email.attach(attachment);

    MailService mailService = registry.getService(MailService.class);
    boolean sended = mailService.sendEmail(email);

    assertTrue(sended, "sended");
}

From source file:servlets.mailPDF.java

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    try {//from   w  w w  .  ja  va2  s .com
        System.out.println("Reached @ mailPDF_Bronze.");

        String pdfBase64 = request.getParameter("pdfBase64");
        String reportdate = request.getParameter("reportdate");
        String emailaddresses = request.getParameter("emailList");
        String emailMessage = request.getParameter("emailMessage");
        String filename = request.getParameter("filename");

        List<String> emailList = Arrays.asList(emailaddresses.split("\\s*,\\s*"));

        System.out.println("pdfBase54 is --------------\n" + pdfBase64 + "\n-----------------End");
        System.out.println(reportdate);
        System.out.println(emailaddresses);

        byte[] decodedBytes = decode(pdfBase64.substring(28));
        MultiPartEmail email = new MultiPartEmail();
        email.setSmtpPort(587);
        email.setDebug(false);
        email.setHostName("smtp.gmail.com");
        email.setAuthentication("reporting@groupnp.com", "D3sign2015");
        email.setTLS(true);

        for (String emailAddress : emailList) {

            email.addTo(emailAddress);
        }
        if (filename.equals("LNIndia Tracker (Gold - Weekly)")) {

            email.setFrom("reporting@groupnp.com");
            email.setSubject(filename + " (" + reportdate + ")");

            MimeMultipart part1 = new MimeMultipart();
            BodyPart messageBodyPart1 = new MimeBodyPart();
            messageBodyPart1.setContent(emailMessage, "text/html; charset=utf-8");
            part1.addBodyPart(messageBodyPart1);
            email.addPart(part1);

            MimeMultipart part2 = new MimeMultipart();
            BodyPart messageBodyPart2 = new MimeBodyPart();
            messageBodyPart2
                    .setDataHandler(new DataHandler(new ByteArrayDataSource(decodedBytes, "application/pdf")));
            messageBodyPart2.removeHeader("Content-Transfer-Encoding");
            messageBodyPart2.addHeader("Content-Transfer-Encoding", "base64");
            messageBodyPart2.setFileName(filename + " " + reportdate + ".pdf");
            part2.addBodyPart(messageBodyPart2);
            email.addPart(part2);

            email.send();

        } else if (filename.equals("LNIndia Tracker (Gold - Monthly)")) {

            email.setFrom("reporting@groupnp.com");
            email.setSubject(filename + " (" + reportdate + ")");

            MimeMultipart part1 = new MimeMultipart();
            BodyPart messageBodyPart1 = new MimeBodyPart();
            messageBodyPart1.setContent(emailMessage, "text/html; charset=utf-8");
            part1.addBodyPart(messageBodyPart1);
            email.addPart(part1);

            MimeMultipart part2 = new MimeMultipart();
            BodyPart messageBodyPart2 = new MimeBodyPart();
            messageBodyPart2
                    .setDataHandler(new DataHandler(new ByteArrayDataSource(decodedBytes, "application/pdf")));
            messageBodyPart2.removeHeader("Content-Transfer-Encoding");
            messageBodyPart2.addHeader("Content-Transfer-Encoding", "base64");
            messageBodyPart2.setFileName(filename + " " + reportdate + ".pdf");
            part2.addBodyPart(messageBodyPart2);
            email.addPart(part2);

            email.send();

        }

    } catch (EmailException | MessagingException ex) {
        Logger.getLogger(mailPDF.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:ro.agrade.jira.qanda.listeners.DirectEmailMessageHandler.java

@Override
protected void sendMail(String[] recipients, String subject, String message, String from)
        throws MessagingException {

    SMTPMailServer server = mailServerManager.getDefaultSMTPMailServer();
    if (server == null) {
        LOG.debug("Email server is not configured. QandA is unable to send mails ...");
        return;/*ww w.  j av  a 2s.  c  o  m*/
    }
    LOG.debug("Email message: initializing.");
    //Set the host smtp address
    Properties props = new Properties();

    String proto = server.getMailProtocol().getProtocol();

    props.put("mail.transport.protocol", proto);
    props.put("mail." + proto + ".host", server.getHostname());
    props.put("mail." + proto + ".port", server.getPort());

    String username = server.getUsername();
    String password = server.getPassword();

    Authenticator auth = null;

    if (username != null && password != null) {
        auth = new SMTPAuthenticator(username, password);
        props.put("mail." + proto + ".auth", "true");
    }
    Session session;
    try {
        session = auth != null ? Session.getDefaultInstance(props, auth) : Session.getDefaultInstance(props);
    } catch (SecurityException ex) {
        LOG.warn("Could not get default session. Attempting to create a new one.");
        session = auth != null ? Session.getInstance(props, auth) : Session.getInstance(props);
    }

    // create a message
    MimeMessage msg = new MimeMessage(session);
    Multipart multipart = new MimeMultipart();

    if (from == null) {
        from = server.getDefaultFrom();
    }
    // set the from address
    if (from != null) {
        InternetAddress addressFrom = new InternetAddress(from);
        msg.setFrom(addressFrom);
    }

    if (recipients != null && recipients.length > 0) {
        // set TO address(es)
        InternetAddress[] addressTo = new InternetAddress[recipients.length];
        for (int i = 0; i < recipients.length; i++) {
            addressTo[i] = new InternetAddress(recipients[i]);
        }
        msg.setRecipients(Message.RecipientType.TO, addressTo);
    }

    // Setting the Subject
    msg.setSubject(subject);

    // Setting text content
    MimeBodyPart contentPart = new MimeBodyPart();
    contentPart.setContent(message, "text/html; charset=utf-8");
    multipart.addBodyPart(contentPart);

    msg.setContent(multipart);
    Transport.send(msg);
    LOG.debug("Email message sent successfully.");
}

From source file:org.eclipse.che.mail.MailSender.java

public void sendMail(EmailBean emailBean) throws SendMailException {
    File tempDir = null;/*from www.j ava  2s  . c  o  m*/
    try {
        MimeMessage message = new MimeMessage(mailSessionProvider.get());
        Multipart contentPart = new MimeMultipart();

        MimeBodyPart bodyPart = new MimeBodyPart();
        bodyPart.setText(emailBean.getBody(), "UTF-8", getSubType(emailBean.getMimeType()));
        contentPart.addBodyPart(bodyPart);

        if (emailBean.getAttachments() != null) {
            tempDir = Files.createTempDir();
            for (Attachment attachment : emailBean.getAttachments()) {
                // Create attachment file in temporary directory
                byte[] attachmentContent = Base64.getDecoder().decode(attachment.getContent());
                File attachmentFile = new File(tempDir, attachment.getFileName());
                Files.write(attachmentContent, attachmentFile);

                // Attach the attachment file to email
                MimeBodyPart attachmentPart = new MimeBodyPart();
                attachmentPart.attachFile(attachmentFile);
                attachmentPart.setContentID("<" + attachment.getContentId() + ">");
                contentPart.addBodyPart(attachmentPart);
            }
        }

        message.setContent(contentPart);
        message.setSubject(emailBean.getSubject(), "UTF-8");
        message.setFrom(new InternetAddress(emailBean.getFrom(), true));
        message.setSentDate(new Date());
        message.addRecipients(Message.RecipientType.TO, InternetAddress.parse(emailBean.getTo()));

        if (emailBean.getReplyTo() != null) {
            message.setReplyTo(InternetAddress.parse(emailBean.getReplyTo()));
        }
        LOG.info("Sending from {} to {} with subject {}", emailBean.getFrom(), emailBean.getTo(),
                emailBean.getSubject());

        Transport.send(message);
        LOG.debug("Mail sent");
    } catch (Exception e) {
        LOG.error(e.getLocalizedMessage());
        throw new SendMailException(e.getLocalizedMessage(), e);
    } finally {
        if (tempDir != null) {
            try {
                FileUtils.deleteDirectory(tempDir);
            } catch (IOException exception) {
                LOG.error(exception.getMessage());
            }
        }
    }
}

From source file:com.anteam.alert.email.service.EmailAntlert.java

@Override
public boolean send(AntlertMessage antlertMsg) {

    MimeMessage mimeMsg; // MIME

    // from?to?//from w ww .j a  v  a  2  s  . co  m
    mimeMsg = new javax.mail.internet.MimeMessage(mailSession);

    try {
        // ?
        mimeMsg.setFrom(sender);
        // 
        mimeMsg.setRecipients(RecipientType.TO, receivers);
        // 
        mimeMsg.setSubject(antlertMsg.getTitle(), CHARSET);

        // 
        MimeBodyPart messageBody = new MimeBodyPart();
        messageBody.setContent(antlertMsg.getContent(), CONTENT_MIME_TYPE);

        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messageBody);
        mimeMsg.setContent(multipart);

        // ??
        mimeMsg.setSentDate(new Date());
        mimeMsg.saveChanges();

        // ??
        Transport.send(mimeMsg);
    } catch (MessagingException e) {
        logger.error("???", e);
        return false;
    }
    return true;
}