Search in sources :

Example 31 with BodyPart

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

the class XOPSample method process.

private void process(Element element, MimeMultipart mp) throws Exception {
    if (element.getNamespaceURI().equals("http://www.w3.org/2004/08/xop/include") && element.getLocalName().equals("Include")) {
        String cid = element.getAttribute("href").substring(4);
        BodyPart part = mp.getBodyPart("<" + cid + ">");
        String base64 = Base64.encodeBase64String(IOUtils.toByteArray(part.getInputStream()));
        element.getParentNode().replaceChild(element.getOwnerDocument().createTextNode(base64), element);
    } else {
        for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
            if (child instanceof Element) {
                process((Element) child, mp);
            }
        }
    }
}
Also used : BodyPart(javax.mail.BodyPart) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element)

Example 32 with BodyPart

use of javax.mail.BodyPart 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 33 with BodyPart

use of javax.mail.BodyPart in project disconf by knightliao.

the class SimpleMailSender method sendHtmlMail.

/**
     * 以HTML格式发送邮件
     *
     * @param mailInfo 待发送的邮件信息
     */
public static boolean sendHtmlMail(MailSenderInfo mailInfo) {
    try {
        LOG.info("send to " + mailInfo.getFromAddress());
        // 设置一些通用的数据
        Message mailMessage = setCommon(mailInfo);
        // MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
        Multipart mainPart = new MimeMultipart();
        // 创建一个包含HTML内容的MimeBodyPart
        BodyPart html = new MimeBodyPart();
        // 设置HTML内容
        html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
        mainPart.addBodyPart(html);
        // 将MiniMultipart对象设置为邮件内容
        mailMessage.setContent(mainPart);
        // 发送邮件
        Transport.send(mailMessage);
        LOG.info("send to " + mailInfo.getFromAddress() + " dnoe.");
        return true;
    } catch (MessagingException ex) {
        LOG.error(ex.toString(), ex);
    }
    return false;
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) MessagingException(javax.mail.MessagingException) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Example 34 with BodyPart

use of javax.mail.BodyPart in project rest.li by linkedin.

the class TestMIMEReaderDrain method testSinglePartialTopRemaining.

@Test(dataProvider = "allTypesOfBodiesDataSource")
public void testSinglePartialTopRemaining(final int chunkSize, final List<MimeBodyPart> bodyPartList) throws Exception {
    //Execute the request, verify the correct header came back to ensure the server took the proper drain actions
    //and return the payload so we can assert deeper.
    executeRequestWithDrainStrategy(chunkSize, bodyPartList, SINGLE_PARTIAL_TOP_REMAINING, "onDrainComplete");
    //Single part drains the first 6 then the top level drains all of remaining
    List<SinglePartMIMEDrainReaderCallbackImpl> singlePartMIMEReaderCallbacks = _currentMultiPartMIMEReaderCallback.getSinglePartMIMEReaderCallbacks();
    Assert.assertEquals(singlePartMIMEReaderCallbacks.size(), 6);
    for (int i = 0; i < singlePartMIMEReaderCallbacks.size(); i++) {
        //Actual

        final SinglePartMIMEDrainReaderCallbackImpl currentCallback = singlePartMIMEReaderCallbacks.get(i);
        //Expected

        final BodyPart currentExpectedPart = _currentMimeMultipartBody.getBodyPart(i);
        //Construct expected headers and verify they match

        final Map<String, String> expectedHeaders = new HashMap<String, String>();
        @SuppressWarnings("unchecked") final Enumeration<Header> allHeaders = currentExpectedPart.getAllHeaders();
        while (allHeaders.hasMoreElements()) {
            final Header header = allHeaders.nextElement();
            expectedHeaders.put(header.getName(), header.getValue());
        }
        Assert.assertEquals(currentCallback.getHeaders(), expectedHeaders);
        //Verify that the bodies are empty
        Assert.assertNull(currentCallback.getFinishedData());
    }
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) Header(javax.mail.Header) HashMap(java.util.HashMap) ByteString(com.linkedin.data.ByteString) Test(org.testng.annotations.Test)

Example 35 with BodyPart

use of javax.mail.BodyPart in project rest.li by linkedin.

the class TestMIMEReaderDrain method testSingleAll.

@Test(dataProvider = "allTypesOfBodiesDataSource")
public void testSingleAll(final int chunkSize, final List<MimeBodyPart> bodyPartList) throws Exception {
    //Execute the request, verify the correct header came back to ensure the server took the proper drain actions
    //and return the payload so we can assert deeper.
    executeRequestWithDrainStrategy(chunkSize, bodyPartList, SINGLE_ALL, "onFinished");
    //Single part drains all, one by one
    List<SinglePartMIMEDrainReaderCallbackImpl> singlePartMIMEReaderCallbacks = _currentMultiPartMIMEReaderCallback.getSinglePartMIMEReaderCallbacks();
    Assert.assertEquals(singlePartMIMEReaderCallbacks.size(), 12);
    //Verify everything was drained
    for (int i = 0; i < singlePartMIMEReaderCallbacks.size(); i++) {
        //Actual

        final SinglePartMIMEDrainReaderCallbackImpl currentCallback = singlePartMIMEReaderCallbacks.get(i);
        //Expected

        final BodyPart currentExpectedPart = _currentMimeMultipartBody.getBodyPart(i);
        //Construct expected headers and verify they match

        final Map<String, String> expectedHeaders = new HashMap<String, String>();
        @SuppressWarnings("unchecked") final Enumeration<Header> allHeaders = currentExpectedPart.getAllHeaders();
        while (allHeaders.hasMoreElements()) {
            final Header header = allHeaders.nextElement();
            expectedHeaders.put(header.getName(), header.getValue());
        }
        Assert.assertEquals(currentCallback.getHeaders(), expectedHeaders);
        //Verify that the bodies are empty
        Assert.assertNull(currentCallback.getFinishedData());
    }
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) Header(javax.mail.Header) HashMap(java.util.HashMap) ByteString(com.linkedin.data.ByteString) Test(org.testng.annotations.Test)

Aggregations

BodyPart (javax.mail.BodyPart)71 MimeMultipart (javax.mail.internet.MimeMultipart)52 MimeBodyPart (javax.mail.internet.MimeBodyPart)41 MessagingException (javax.mail.MessagingException)23 MimeMessage (javax.mail.internet.MimeMessage)19 Header (javax.mail.Header)15 Multipart (javax.mail.Multipart)15 DispositionNotification (com.sun.mail.dsn.DispositionNotification)13 IOException (java.io.IOException)13 HashMap (java.util.HashMap)13 InternetHeaders (javax.mail.internet.InternetHeaders)13 ByteString (com.linkedin.data.ByteString)12 DataHandler (javax.activation.DataHandler)10 Test (org.testng.annotations.Test)10 Session (javax.mail.Session)9 InternetAddress (javax.mail.internet.InternetAddress)8 Properties (java.util.Properties)7 File (java.io.File)6 InputStream (java.io.InputStream)6 FileDataSource (javax.activation.FileDataSource)6