Search in sources :

Example 11 with MimeMessage

use of javax.mail.internet.MimeMessage in project camel by apache.

the class MultipleDestinationConsumeTest method testSendAndReceiveMails.

@Test
public void testSendAndReceiveMails() throws Exception {
    Mailbox.clearAll();
    MockEndpoint resultEndpoint = getMockEndpoint("mock:result");
    resultEndpoint.expectedMinimumMessageCount(1);
    MimeMessage message = new MimeMessage(mailSession);
    message.setText(body);
    message.setRecipients(Message.RecipientType.TO, new Address[] { new InternetAddress("james@localhost"), new InternetAddress("bar@localhost") });
    Transport.send(message);
    // lets test the receive worked
    resultEndpoint.assertIsSatisfied(100000);
    Exchange exchange = resultEndpoint.getReceivedExchanges().get(0);
    org.apache.camel.Message in = exchange.getIn();
    assertNotNull("Should have headers", in.getHeaders());
    MailMessage msg = (MailMessage) exchange.getIn();
    Message inMessage = msg != null ? msg.getMessage() : null;
    assertNotNull("In message has no JavaMail message!", inMessage);
    String text = in.getBody(String.class);
    assertEquals("mail body", body, text);
    // need to use iterator as some mail impl returns String[] and others a single String with comma as separator
    // so we let Camel create an iterator so we can use the same code for the test
    Object to = in.getHeader("TO");
    Iterator<String> it = CastUtils.cast(ObjectHelper.createIterator(to));
    int i = 0;
    while (it.hasNext()) {
        if (i == 0) {
            assertEquals("james@localhost", it.next().trim());
        } else {
            assertEquals("bar@localhost", it.next().trim());
        }
        i++;
    }
    Enumeration<Header> iter = CastUtils.cast(inMessage.getAllHeaders());
    while (iter.hasMoreElements()) {
        Header header = iter.nextElement();
        String[] value = message.getHeader(header.getName());
        log.debug("Header: " + header.getName() + " has value: " + ObjectHelper.asString(value));
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Exchange(org.apache.camel.Exchange) Header(javax.mail.Header) MimeMessage(javax.mail.internet.MimeMessage) Test(org.junit.Test)

Example 12 with MimeMessage

use of javax.mail.internet.MimeMessage in project camel by apache.

the class MailIdempotentRepositoryTest 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].setText("Message " + i);
        messages[i].setHeader("Message-ID", "myuid-" + 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 13 with MimeMessage

use of javax.mail.internet.MimeMessage in project camel by apache.

the class MailMessageTest method setUp.

@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    Mailbox.clearAll();
    endpoint = resolveMandatoryEndpoint("pop3://someone@myhost:30/subject");
    Properties properties = new Properties();
    properties.put("mail.smtp.host", "localhost");
    mailSession = Session.getInstance(properties, null);
    mimeMessage = new MimeMessage(mailSession);
    mimeMessage.setText(body);
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) Properties(java.util.Properties) Before(org.junit.Before)

Example 14 with MimeMessage

use of javax.mail.internet.MimeMessage in project camel by apache.

the class MailShutdownCompleteCurrentTaskOnlyTest 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 8 new messages
    Message[] messages = new Message[5];
    for (int i = 0; i < 5; i++) {
        messages[i] = new MimeMessage(sender.getSession());
        messages[i].setText("Message " + i);
        messages[i].setHeader("Message-ID", "" + 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 15 with MimeMessage

use of javax.mail.internet.MimeMessage in project camel by apache.

the class MailSortTermTest 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, "bill", "secret");
    Folder folder = store.getFolder("INBOX");
    folder.open(Folder.READ_WRITE);
    folder.expunge();
    // inserts 3 messages, one with earlier, one with later sent date and one with invalid subject (not returned in search)
    Message[] messages = new Message[3];
    messages[0] = new MimeMessage(sender.getSession());
    messages[0].setText("Earlier date");
    messages[0].setHeader("Message-ID", "0");
    messages[0].setSentDate(new Date(10000));
    messages[0].setSubject("Camel");
    messages[1] = new MimeMessage(sender.getSession());
    messages[1].setText("Later date");
    messages[1].setHeader("Message-ID", "1");
    messages[1].setSentDate(new Date(20000));
    messages[1].setSubject("Camel");
    messages[2] = new MimeMessage(sender.getSession());
    messages[2].setText("Even later date");
    messages[2].setHeader("Message-ID", "2");
    messages[2].setSentDate(new Date(30000));
    messages[2].setSubject("Invalid");
    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) Date(java.util.Date)

Aggregations

MimeMessage (javax.mail.internet.MimeMessage)1146 Test (org.junit.Test)374 InternetAddress (javax.mail.internet.InternetAddress)334 MessagingException (javax.mail.MessagingException)299 Session (javax.mail.Session)222 Properties (java.util.Properties)219 MimeMultipart (javax.mail.internet.MimeMultipart)208 MimeBodyPart (javax.mail.internet.MimeBodyPart)178 Date (java.util.Date)153 IOException (java.io.IOException)137 Message (javax.mail.Message)120 MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)107 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)97 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)83 InputStream (java.io.InputStream)82 ArrayList (java.util.ArrayList)81 Multipart (javax.mail.Multipart)75 DataHandler (javax.activation.DataHandler)73 ByteArrayOutputStream (java.io.ByteArrayOutputStream)72 BodyPart (javax.mail.BodyPart)70