Search in sources :

Example 81 with MimeMultipart

use of javax.mail.internet.MimeMultipart in project ddf by codice.

the class SmtpClientImplITCase method testSend.

@Test
public void testSend() throws IOException, MessagingException, ExecutionException, InterruptedException {
    int port = findAvailablePort();
    SimpleSmtpServer server = SimpleSmtpServer.start(port);
    SmtpClientImpl emailService = new SmtpClientImpl();
    emailService.setHostName(HOSTNAME);
    emailService.setPortNumber(port);
    Session session = emailService.createSession();
    MimeMessage mimeMessage = new MimeMessage(session);
    mimeMessage.setFrom(new InternetAddress(FROM_ADDR));
    mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(TO_ADDR));
    mimeMessage.setSubject(SUBJECT);
    BodyPart messageBodyPart = new MimeBodyPart();
    messageBodyPart.setText(BODY);
    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(messageBodyPart);
    mimeMessage.setContent(multipart);
    emailService.send(mimeMessage).get();
    server.stop();
    assertThat(server.getReceivedEmailSize(), is(1));
    Iterator emailIterator = server.getReceivedEmail();
    SmtpMessage email = (SmtpMessage) emailIterator.next();
    assertThat(email.getHeaderValue(SUBJECT_HEADER), is(SUBJECT));
    assertThat(email.getHeaderValue(FROM_HEADER), containsString(FROM_ADDR));
    assertThat(email.getHeaderValue(TO_HEADER), containsString(TO_ADDR));
    assertThat(email.getBody(), containsString(BODY));
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) InternetAddress(javax.mail.internet.InternetAddress) Multipart(javax.mail.Multipart) MimeMultipart(javax.mail.internet.MimeMultipart) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) SmtpMessage(com.dumbster.smtp.SmtpMessage) SimpleSmtpServer(com.dumbster.smtp.SimpleSmtpServer) Iterator(java.util.Iterator) MimeBodyPart(javax.mail.internet.MimeBodyPart) Session(javax.mail.Session) Test(org.junit.Test)

Example 82 with MimeMultipart

use of javax.mail.internet.MimeMultipart in project JMRI by JMRI.

the class MailMessage method prepare.

/**
     * sets up needed parts for sending email message presumes any needed sets
     * have been done first
     */
public void prepare() {
    try {
        Properties props = System.getProperties();
        props.put("mail.transport.protocol", pProtocol);
        props.put("mail.smtp.starttls.enable", pTls);
        if (mailhost != null) {
            props.put("mail.smtp.host", mailhost);
        }
        props.put("mail.smtp.auth", pAuth);
        Authenticator auth = new SMTPAuthenticator();
        // Get a Session object
        session = Session.getInstance(props, auth);
        if (log.isDebugEnabled()) {
            session.setDebug(true);
        }
        // construct the message
        msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(from));
        msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
        if (cc != null) {
            msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc, false));
        }
        if (bcc != null) {
            msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc, false));
        }
        msg.setSubject(subject);
        // We need a multipart message to hold attachment.
        mp = new MimeMultipart();
    } catch (MessagingException e) {
        log.warn("Exception in prepare", e);
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) MessagingException(javax.mail.MessagingException) Properties(java.util.Properties) Authenticator(javax.mail.Authenticator)

Example 83 with MimeMultipart

use of javax.mail.internet.MimeMultipart in project webservices-axiom by apache.

the class XOPSample method getInlinedMessage.

public final InputStream getInlinedMessage() {
    try {
        MimeMultipart mp = getMultipart();
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setNamespaceAware(true);
        Document rootPart = documentBuilderFactory.newDocumentBuilder().parse(mp.getBodyPart(0).getInputStream());
        process(rootPart.getDocumentElement(), mp);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        TransformerFactory.newInstance().newTransformer().transform(new DOMSource(rootPart), new StreamResult(baos));
        return new ByteArrayInputStream(baos.toByteArray());
    } catch (Exception ex) {
        throw new Error(ex);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) StreamResult(javax.xml.transform.stream.StreamResult) MimeMultipart(javax.mail.internet.MimeMultipart) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.w3c.dom.Document)

Example 84 with MimeMultipart

use of javax.mail.internet.MimeMultipart in project webservices-axiom by apache.

the class AbstractMultipartWriterTest method test.

private void test(String contentTransferEncoding) throws Exception {
    Random random = new Random();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    MultipartWriter mpw = factory.createMultipartWriter(baos, UIDGenerator.generateMimeBoundary());
    byte[] content = new byte[8192];
    random.nextBytes(content);
    OutputStream partOutputStream = mpw.writePart("application/octet-stream", contentTransferEncoding, UIDGenerator.generateContentId());
    partOutputStream.write(content);
    partOutputStream.close();
    mpw.complete();
    MimeMultipart mp = new MimeMultipart(new ByteArrayDataSource(baos.toByteArray()));
    assertEquals(1, mp.getCount());
    MimeBodyPart bp = (MimeBodyPart) mp.getBodyPart(0);
    assertEquals(contentTransferEncoding, bp.getHeader("Content-Transfer-Encoding")[0]);
    baos.reset();
    bp.getDataHandler().writeTo(baos);
    assertTrue(Arrays.equals(content, baos.toByteArray()));
}
Also used : Random(java.util.Random) MimeMultipart(javax.mail.internet.MimeMultipart) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MimeBodyPart(javax.mail.internet.MimeBodyPart) ByteArrayDataSource(org.apache.axiom.attachments.ByteArrayDataSource)

Example 85 with MimeMultipart

use of javax.mail.internet.MimeMultipart in project webservices-axiom by apache.

the class AttachmentsTest method testTurkishLocale.

private void testTurkishLocale(String contentIDHeaderName) throws Exception {
    Locale locale = Locale.getDefault();
    Locale.setDefault(new Locale("tr", "TR"));
    try {
        MimeMessage message = new MimeMessage((Session) null);
        MimeMultipart mp = new MimeMultipart("related");
        MimeBodyPart bp1 = new MimeBodyPart();
        bp1.setText("<root/>", "utf-8", "xml");
        bp1.addHeader("Content-Transfer-Encoding", "binary");
        mp.addBodyPart(bp1);
        MimeBodyPart bp2 = new MimeBodyPart();
        byte[] content = new byte[8192];
        new Random().nextBytes(content);
        bp2.setDataHandler(new DataHandler("Test", "text/plain"));
        bp2.addHeader("Content-Transfer-Encoding", "binary");
        bp2.addHeader(contentIDHeaderName, "part@apache.org");
        mp.addBodyPart(bp2);
        message.setContent(mp);
        message.saveChanges();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        mp.writeTo(baos);
        String contentType = message.getContentType();
        InputStream in = new ByteArrayInputStream(baos.toByteArray());
        Attachments attachments = new Attachments(in, contentType);
        assertNotNull(attachments.getDataHandler("part@apache.org"));
    } finally {
        Locale.setDefault(locale);
    }
}
Also used : Locale(java.util.Locale) Random(java.util.Random) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) PipedInputStream(java.io.PipedInputStream) ExceptionInputStream(org.apache.axiom.testutils.io.ExceptionInputStream) InputStream(java.io.InputStream) DataHandler(javax.activation.DataHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MimeBodyPart(javax.mail.internet.MimeBodyPart)

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