Search in sources :

Example 86 with Message

use of javax.mail.Message in project camel by apache.

the class MailCommitOnCompletionTest method prepareMailbox.

private void prepareMailbox() throws Exception {
    // connect to mailbox
    Mailbox.clearAll();
    JavaMailSender sender = new DefaultJavaMailSender();
    Store store = sender.getSession().getStore("pop3");
    store.connect("localhost", 25, "jones", "secret");
    Folder folder = store.getFolder("INBOX");
    folder.open(Folder.READ_WRITE);
    folder.expunge();
    // inserts 5 new messages
    Message[] messages = new Message[5];
    for (int i = 0; i < 5; i++) {
        messages[i] = new MimeMessage(sender.getSession());
        messages[i].setHeader("Message-ID", "" + i);
        messages[i].setText("Message " + i);
    }
    folder.appendMessages(messages);
    folder.close(true);
}
Also used : Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MimeMessage(javax.mail.internet.MimeMessage) Store(javax.mail.Store) Folder(javax.mail.Folder) MockEndpoint(org.apache.camel.component.mock.MockEndpoint)

Example 87 with Message

use of javax.mail.Message in project camel by apache.

the class MailContentTypeTest method testSendMultipartMail.

@Test
public void testSendMultipartMail() throws Exception {
    Mailbox.clearAll();
    Map<String, Object> headers = new HashMap<String, Object>();
    headers.put(MailConstants.MAIL_ALTERNATIVE_BODY, "Hello World");
    sendBody("direct:c", "<html><body><h1>Hello</h1>World</body></html>", headers);
    Mailbox box = Mailbox.get("claus@localhost");
    Message msg = box.get(0);
    assertTrue(msg.getContentType().startsWith("multipart/alternative"));
    assertEquals("Hello World", ((MimeMultipart) msg.getContent()).getBodyPart(0).getContent());
    assertEquals("<html><body><h1>Hello</h1>World</body></html>", ((MimeMultipart) msg.getContent()).getBodyPart(1).getContent());
}
Also used : Mailbox(org.jvnet.mock_javamail.Mailbox) Message(javax.mail.Message) HashMap(java.util.HashMap) MimeMultipart(javax.mail.internet.MimeMultipart) Test(org.junit.Test)

Example 88 with Message

use of javax.mail.Message in project camel by apache.

the class MailConvertersTest method testMultipartToInputStream.

@Test
public void testMultipartToInputStream() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(1);
    template.send("direct:a", new Processor() {

        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setBody("Hello World");
            exchange.getIn().setHeader(MailConstants.MAIL_ALTERNATIVE_BODY, "Alternative World");
        }
    });
    assertMockEndpointsSatisfied();
    Message mailMessage = mock.getReceivedExchanges().get(0).getIn().getBody(MailMessage.class).getMessage();
    assertNotNull(mailMessage);
    Object content = mailMessage.getContent();
    assertIsInstanceOf(Multipart.class, content);
    InputStream is = mock.getReceivedExchanges().get(0).getIn().getBody(InputStream.class);
    assertNotNull(is);
    assertEquals("Alternative World", context.getTypeConverter().convertTo(String.class, is));
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) Message(javax.mail.Message) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) InputStream(java.io.InputStream) Test(org.junit.Test)

Example 89 with Message

use of javax.mail.Message in project camel by apache.

the class MailConvertersTest method testMailMessageToInputStream.

@Test
public void testMailMessageToInputStream() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(1);
    template.sendBodyAndHeader("direct:a", "Hello World", "Subject", "Camel rocks");
    assertMockEndpointsSatisfied();
    Message mailMessage = mock.getReceivedExchanges().get(0).getIn().getBody(MailMessage.class).getMessage();
    assertNotNull(mailMessage);
    InputStream is = MailConverters.toInputStream(mailMessage);
    assertNotNull(is);
    assertEquals("Hello World", context.getTypeConverter().convertTo(String.class, is));
}
Also used : Message(javax.mail.Message) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) InputStream(java.io.InputStream) Test(org.junit.Test)

Example 90 with Message

use of javax.mail.Message in project camel by apache.

the class MailConvertersTest method testMultipartToByteArray.

@Test
public void testMultipartToByteArray() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(1);
    template.send("direct:a", new Processor() {

        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setBody("Hello World");
            exchange.getIn().setHeader(MailConstants.MAIL_ALTERNATIVE_BODY, "Alternative World");
        }
    });
    assertMockEndpointsSatisfied();
    Message mailMessage = mock.getReceivedExchanges().get(0).getIn().getBody(MailMessage.class).getMessage();
    assertNotNull(mailMessage);
    Object content = mailMessage.getContent();
    assertIsInstanceOf(Multipart.class, content);
    byte[] is = mock.getReceivedExchanges().get(0).getIn().getBody(byte[].class);
    assertNotNull(is);
    assertEquals("Alternative World", context.getTypeConverter().convertTo(String.class, is));
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) Message(javax.mail.Message) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Test(org.junit.Test)

Aggregations

Message (javax.mail.Message)149 MimeMessage (javax.mail.internet.MimeMessage)81 MessagingException (javax.mail.MessagingException)53 InternetAddress (javax.mail.internet.InternetAddress)48 Folder (javax.mail.Folder)41 Test (org.junit.Test)40 Session (javax.mail.Session)37 Properties (java.util.Properties)36 Store (javax.mail.Store)28 Date (java.util.Date)19 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)19 MimeMultipart (javax.mail.internet.MimeMultipart)18 Mailbox (org.jvnet.mock_javamail.Mailbox)18 MimeBodyPart (javax.mail.internet.MimeBodyPart)16 PasswordAuthentication (javax.mail.PasswordAuthentication)15 IOException (java.io.IOException)13 ArrayList (java.util.ArrayList)13 Multipart (javax.mail.Multipart)13 Address (javax.mail.Address)11 HashMap (java.util.HashMap)8