Search in sources :

Example 16 with Message

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

the class MailSorterTest method createMessage.

/**
     * Create a new message with the specified data
     */
private static Message createMessage(String to, String cc, String from, Date received, Date sent, int size, String subject) throws MessagingException {
    final Message msg = Mockito.mock(Message.class);
    when(msg.getFrom()).thenReturn(new Address[] { new InternetAddress(from) });
    when(msg.getRecipients(Message.RecipientType.TO)).thenReturn(new Address[] { new InternetAddress(to) });
    when(msg.getRecipients(Message.RecipientType.CC)).thenReturn(new Address[] { new InternetAddress(cc) });
    when(msg.getSentDate()).thenReturn(sent);
    when(msg.getReceivedDate()).thenReturn(received);
    when(msg.getSize()).thenReturn(size);
    when(msg.getSubject()).thenReturn(subject);
    return msg;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Message(javax.mail.Message)

Example 17 with Message

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

the class MailSorterTest method testSortMessages.

@Test
public void testSortMessages() throws Exception {
    Message[] expected = new Message[] { MESSAGES[0], MESSAGES[1], MESSAGES[2] };
    // Sort using all the terms. Message order should be the same no matter what term is used
    for (SortTerm term : POSSIBLE_TERMS) {
        Message[] actual = MESSAGES.clone();
        MailSorter.sortMessages(actual, new SortTerm[] { term });
        try {
            assertArrayEquals(actual, expected);
        } catch (Exception ex) {
            throw new Exception("Term: " + term.toString(), ex);
        }
    }
}
Also used : SortTerm(com.sun.mail.imap.SortTerm) Message(javax.mail.Message) MessagingException(javax.mail.MessagingException) Test(org.junit.Test)

Example 18 with Message

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

the class MailUsingCustomSessionTest method testSendAndReceiveMailsWithCustomSession.

@Test
public void testSendAndReceiveMailsWithCustomSession() throws Exception {
    MockEndpoint mockEndpoint = getMockEndpoint("mock:result");
    mockEndpoint.expectedBodiesReceived("hello camel!");
    Map<String, Object> headers = new HashMap<>();
    headers.put("subject", "Hello Camel");
    template.sendBodyAndHeaders("smtp://james@localhost?session=#myCustomMailSession", "hello camel!", headers);
    mockEndpoint.assertIsSatisfied();
    Mailbox mailbox = Mailbox.get("james@localhost");
    assertEquals("Expected one mail for james@localhost", 1, mailbox.size());
    Message message = mailbox.get(0);
    assertEquals("hello camel!", message.getContent());
    assertEquals("camel@localhost", message.getFrom()[0].toString());
}
Also used : Mailbox(org.jvnet.mock_javamail.Mailbox) Message(javax.mail.Message) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 19 with Message

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

the class MailProcessOnlyUnseenMessagesTest method prepareMailbox.

private void prepareMailbox() throws Exception {
    // connect to mailbox
    Mailbox.clearAll();
    JavaMailSender sender = new DefaultJavaMailSender();
    Store store = sender.getSession().getStore("imap");
    store.connect("localhost", 25, "claus", "secret");
    Folder folder = store.getFolder("INBOX");
    folder.open(Folder.READ_WRITE);
    folder.expunge();
    // inserts two messages with the SEEN flag
    Message[] msg = new Message[2];
    msg[0] = new MimeMessage(sender.getSession());
    msg[0].setText("Message 1");
    msg[0].setHeader("Message-ID", "0");
    msg[0].setFlag(Flags.Flag.SEEN, true);
    msg[1] = new MimeMessage(sender.getSession());
    msg[1].setText("Message 2");
    msg[0].setHeader("Message-ID", "1");
    msg[1].setFlag(Flags.Flag.SEEN, true);
    folder.appendMessages(msg);
    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)

Example 20 with Message

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

the class MailRouteTest method assertMailboxReceivedMessages.

protected void assertMailboxReceivedMessages(String name) throws IOException, MessagingException {
    Mailbox mailbox = Mailbox.get(name);
    assertEquals(name + " should have received 1 mail", 1, mailbox.size());
    Message message = mailbox.get(0);
    assertNotNull(name + " should have received at least one mail!", message);
    assertEquals("hello world!", message.getContent());
    assertEquals("camel@localhost", message.getFrom()[0].toString());
    boolean found = false;
    for (Address adr : message.getRecipients(RecipientType.TO)) {
        if (name.equals(adr.toString())) {
            found = true;
        }
    }
    assertTrue("Should have found the recpient to in the mail: " + name, found);
}
Also used : Mailbox(org.jvnet.mock_javamail.Mailbox) Message(javax.mail.Message) Address(javax.mail.Address)

Aggregations

Message (javax.mail.Message)132 MimeMessage (javax.mail.internet.MimeMessage)69 MessagingException (javax.mail.MessagingException)49 InternetAddress (javax.mail.internet.InternetAddress)40 Test (org.junit.Test)40 Folder (javax.mail.Folder)37 Properties (java.util.Properties)32 Session (javax.mail.Session)31 Store (javax.mail.Store)28 Date (java.util.Date)19 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)19 Mailbox (org.jvnet.mock_javamail.Mailbox)18 MimeMultipart (javax.mail.internet.MimeMultipart)15 MimeBodyPart (javax.mail.internet.MimeBodyPart)14 PasswordAuthentication (javax.mail.PasswordAuthentication)13 IOException (java.io.IOException)12 ArrayList (java.util.ArrayList)12 Multipart (javax.mail.Multipart)11 Address (javax.mail.Address)9 HashMap (java.util.HashMap)7