Search in sources :

Example 91 with MimeMultipart

use of javax.mail.internet.MimeMultipart in project hub-alert by blackducksoftware.

the class MimeMultipartBuilder method build.

public MimeMultipart build() throws MessagingException {
    final MimeMultipart email = new MimeMultipart("mixed");
    final MimeBodyPart emailBodyPart = buildEmailBodyPart();
    email.addBodyPart(emailBodyPart);
    addAttachmentBodyParts(email);
    return email;
}
Also used : MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Example 92 with MimeMultipart

use of javax.mail.internet.MimeMultipart in project hub-alert by blackducksoftware.

the class MimeMultipartBuilder method buildHtmlBodyPart.

private MimeBodyPart buildHtmlBodyPart() throws MessagingException {
    final MimeMultipart htmlContent = new MimeMultipart("related");
    final MimeBodyPart htmlPart = new MimeBodyPart();
    htmlPart.setContent(html, "text/html; charset=utf-8");
    htmlContent.addBodyPart(htmlPart);
    for (final Map.Entry<String, String> entry : contentIdsToFilePaths.entrySet()) {
        final MimeBodyPart embeddedImageBodyPart = new MimeBodyPart();
        String imageFilePath = entry.getValue();
        File imageFile = new File(imageFilePath);
        if (!imageFile.exists()) {
            try {
                final File imagesDir = findImagesDirectory();
                if (imagesDir != null) {
                    imageFile = new File(imagesDir, imageFilePath);
                    if (imageFile.exists()) {
                        imageFilePath = imageFile.getCanonicalPath();
                    }
                }
            } catch (final Exception e) {
            // ignore let freemarker fail and log the exception
            // up the chain when it cannot find the image file
            }
        }
        final DataSource fds = new FileDataSource(imageFilePath);
        embeddedImageBodyPart.setDataHandler(new DataHandler(fds));
        embeddedImageBodyPart.setHeader("Content-ID", entry.getKey());
        htmlContent.addBodyPart(embeddedImageBodyPart);
    }
    final MimeBodyPart htmlBodyPart = new MimeBodyPart();
    htmlBodyPart.setContent(htmlContent);
    return htmlBodyPart;
}
Also used : MimeMultipart(javax.mail.internet.MimeMultipart) FileDataSource(javax.activation.FileDataSource) DataHandler(javax.activation.DataHandler) MimeBodyPart(javax.mail.internet.MimeBodyPart) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File) MessagingException(javax.mail.MessagingException) FileDataSource(javax.activation.FileDataSource) DataSource(javax.activation.DataSource)

Example 93 with MimeMultipart

use of javax.mail.internet.MimeMultipart in project uplace.es by Uplace.

the class MailServiceIntTest method testSendMultipartEmail.

@Test
public void testSendMultipartEmail() throws Exception {
    mailService.sendEmail("john.doe@example.com", "testSubject", "testContent", true, false);
    verify(javaMailSender).send((MimeMessage) messageCaptor.capture());
    MimeMessage message = (MimeMessage) messageCaptor.getValue();
    MimeMultipart mp = (MimeMultipart) message.getContent();
    MimeBodyPart part = (MimeBodyPart) ((MimeMultipart) mp.getBodyPart(0).getContent()).getBodyPart(0);
    ByteArrayOutputStream aos = new ByteArrayOutputStream();
    part.writeTo(aos);
    assertThat(message.getSubject()).isEqualTo("testSubject");
    assertThat(message.getAllRecipients()[0].toString()).isEqualTo("john.doe@example.com");
    assertThat(message.getFrom()[0].toString()).isEqualTo("test@localhost");
    assertThat(message.getContent()).isInstanceOf(Multipart.class);
    assertThat(aos.toString()).isEqualTo("\r\ntestContent");
    assertThat(part.getDataHandler().getContentType()).isEqualTo("text/plain; charset=UTF-8");
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MimeBodyPart(javax.mail.internet.MimeBodyPart) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 94 with MimeMultipart

use of javax.mail.internet.MimeMultipart in project uplace.es by Uplace.

the class MailServiceIntTest method testSendMultipartHtmlEmail.

@Test
public void testSendMultipartHtmlEmail() throws Exception {
    mailService.sendEmail("john.doe@example.com", "testSubject", "testContent", true, true);
    verify(javaMailSender).send((MimeMessage) messageCaptor.capture());
    MimeMessage message = (MimeMessage) messageCaptor.getValue();
    MimeMultipart mp = (MimeMultipart) message.getContent();
    MimeBodyPart part = (MimeBodyPart) ((MimeMultipart) mp.getBodyPart(0).getContent()).getBodyPart(0);
    ByteArrayOutputStream aos = new ByteArrayOutputStream();
    part.writeTo(aos);
    assertThat(message.getSubject()).isEqualTo("testSubject");
    assertThat(message.getAllRecipients()[0].toString()).isEqualTo("john.doe@example.com");
    assertThat(message.getFrom()[0].toString()).isEqualTo("test@localhost");
    assertThat(message.getContent()).isInstanceOf(Multipart.class);
    assertThat(aos.toString()).isEqualTo("\r\ntestContent");
    assertThat(part.getDataHandler().getContentType()).isEqualTo("text/html;charset=UTF-8");
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MimeBodyPart(javax.mail.internet.MimeBodyPart) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 95 with MimeMultipart

use of javax.mail.internet.MimeMultipart in project Lucee by lucee.

the class SMTPClient method createMimeMessage.

private MimeMessageAndSession createMimeMessage(lucee.runtime.config.Config config, String hostName, int port, String username, String password, long lifeTimesan, long idleTimespan, boolean tls, boolean ssl, boolean sendPartial, boolean newConnection, boolean userset) throws MessagingException {
    Properties props = (Properties) System.getProperties().clone();
    String strTimeout = Caster.toString(getTimeout(config));
    props.put("mail.smtp.host", hostName);
    props.put("mail.smtp.timeout", strTimeout);
    props.put("mail.smtp.connectiontimeout", strTimeout);
    props.put("mail.smtp.sendpartial", Caster.toString(sendPartial));
    props.put("mail.smtp.userset", userset);
    if (port > 0) {
        props.put("mail.smtp.port", Caster.toString(port));
    }
    if (ssl) {
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.port", Caster.toString(port));
        props.put("mail.smtp.socketFactory.fallback", "false");
    } else {
        props.put("mail.smtp.socketFactory.class", "javax.net.SocketFactory");
        props.remove("mail.smtp.socketFactory.port");
        props.remove("mail.smtp.socketFactory.fallback");
    }
    Authenticator auth = null;
    if (!StringUtil.isEmpty(username)) {
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", tls ? "true" : "false");
        props.put("mail.smtp.user", username);
        props.put("mail.smtp.password", password);
        props.put("password", password);
        auth = new SMTPAuthenticator(username, password);
    } else {
        props.put("mail.smtp.auth", "false");
        props.remove("mail.smtp.starttls.enable");
        props.remove("mail.smtp.user");
        props.remove("mail.smtp.password");
        props.remove("password");
    }
    SessionAndTransport sat = newConnection ? new SessionAndTransport(hash(props), props, auth, lifeTimesan, idleTimespan) : SMTPConnectionPool.getSessionAndTransport(props, hash(props), auth, lifeTimesan, idleTimespan);
    // Contacts
    SMTPMessage msg = new SMTPMessage(sat.session);
    if (from == null)
        throw new MessagingException("you have do define the from for the mail");
    // if(tos==null)throw new MessagingException("you have do define the to for the mail");
    checkAddress(from, charset);
    // checkAddress(tos,charset);
    msg.setFrom(from);
    if (tos != null) {
        checkAddress(tos, charset);
        msg.setRecipients(Message.RecipientType.TO, tos);
    }
    if (ccs != null) {
        checkAddress(ccs, charset);
        msg.setRecipients(Message.RecipientType.CC, ccs);
    }
    if (bccs != null) {
        checkAddress(bccs, charset);
        msg.setRecipients(Message.RecipientType.BCC, bccs);
    }
    if (rts != null) {
        checkAddress(rts, charset);
        msg.setReplyTo(rts);
    }
    if (fts != null) {
        checkAddress(fts, charset);
        msg.setEnvelopeFrom(fts[0].toString());
    }
    // Subject and headers
    try {
        msg.setSubject(MailUtil.encode(subject, charset.name()));
    } catch (UnsupportedEncodingException e) {
        throw new MessagingException("the encoding " + charset + " is not supported");
    }
    msg.setHeader("X-Mailer", xmailer);
    msg.setHeader("Date", getNow(timeZone));
    Multipart mp = null;
    // only Plain
    if (StringUtil.isEmpty(htmlText)) {
        if (ArrayUtil.isEmpty(attachmentz) && ArrayUtil.isEmpty(parts)) {
            fillPlainText(config, msg);
            setHeaders(msg, headers);
            return new MimeMessageAndSession(msg, sat);
        }
        mp = new MimeMultipart("mixed");
        mp.addBodyPart(getPlainText(config));
    } else // Only HTML
    if (StringUtil.isEmpty(plainText)) {
        if (ArrayUtil.isEmpty(attachmentz) && ArrayUtil.isEmpty(parts)) {
            fillHTMLText(config, msg);
            setHeaders(msg, headers);
            return new MimeMessageAndSession(msg, sat);
        }
        mp = new MimeMultipart("mixed");
        mp.addBodyPart(getHTMLText(config));
    } else // Plain and HTML
    {
        mp = new MimeMultipart("alternative");
        mp.addBodyPart(getPlainText(config));
        // this need to be last
        mp.addBodyPart(getHTMLText(config));
        if (!ArrayUtil.isEmpty(attachmentz) || !ArrayUtil.isEmpty(parts)) {
            MimeBodyPart content = new MimeBodyPart();
            content.setContent(mp);
            mp = new MimeMultipart("mixed");
            mp.addBodyPart(content);
        }
    }
    // parts
    if (!ArrayUtil.isEmpty(parts)) {
        Iterator<MailPart> it = parts.iterator();
        if (mp instanceof MimeMultipart)
            ((MimeMultipart) mp).setSubType("alternative");
        while (it.hasNext()) {
            mp.addBodyPart(toMimeBodyPart(config, it.next()));
        }
    }
    // Attachments
    if (!ArrayUtil.isEmpty(attachmentz)) {
        for (int i = 0; i < attachmentz.length; i++) {
            mp.addBodyPart(toMimeBodyPart(mp, config, attachmentz[i]));
        }
    }
    msg.setContent(mp);
    setHeaders(msg, headers);
    return new MimeMessageAndSession(msg, sat);
}
Also used : SMTPMessage(com.sun.mail.smtp.SMTPMessage) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) MessagingException(javax.mail.MessagingException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Properties(java.util.Properties) MailPart(lucee.runtime.net.mail.MailPart) SessionAndTransport(lucee.runtime.net.smtp.SMTPConnectionPool.SessionAndTransport) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart) Authenticator(javax.mail.Authenticator)

Aggregations

MimeMultipart (javax.mail.internet.MimeMultipart)213 MimeBodyPart (javax.mail.internet.MimeBodyPart)134 MimeMessage (javax.mail.internet.MimeMessage)85 MessagingException (javax.mail.MessagingException)69 ByteArrayOutputStream (java.io.ByteArrayOutputStream)55 BodyPart (javax.mail.BodyPart)55 IOException (java.io.IOException)47 ByteString (com.linkedin.data.ByteString)37 DataHandler (javax.activation.DataHandler)33 InternetAddress (javax.mail.internet.InternetAddress)33 Test (org.testng.annotations.Test)31 ZMimeMultipart (com.zimbra.common.zmime.ZMimeMultipart)27 HashMap (java.util.HashMap)27 ZMimeBodyPart (com.zimbra.common.zmime.ZMimeBodyPart)26 Multipart (javax.mail.Multipart)26 Properties (java.util.Properties)25 Session (javax.mail.Session)24 ByteArrayDataSource (javax.mail.util.ByteArrayDataSource)24 InputStream (java.io.InputStream)23 Date (java.util.Date)21