Search in sources :

Example 6 with MimeMessage

use of javax.mail.internet.MimeMessage 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 7 with MimeMessage

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

the class MailProducerTest method testProducerBodyIsMimeMessage.

@Test
public void testProducerBodyIsMimeMessage() throws Exception {
    Mailbox.clearAll();
    getMockEndpoint("mock:result").expectedMessageCount(1);
    Address from = new InternetAddress("fromCamelTest@localhost");
    Address to = new InternetAddress("recipient2@localhost");
    Session session = Session.getDefaultInstance(System.getProperties());
    MimeMessage mimeMessage = new MimeMessage(session);
    mimeMessage.setFrom(from);
    mimeMessage.addRecipient(RecipientType.TO, to);
    mimeMessage.setSubject("This is the subject.");
    mimeMessage.setText("This is the message");
    template.sendBodyAndHeader("direct:start", mimeMessage, "To", "someone@localhost");
    assertMockEndpointsSatisfied();
    // need to check the message header
    Exchange exchange = getMockEndpoint("mock:result").getExchanges().get(0);
    assertNotNull("The message id should not be null", exchange.getIn().getHeader(MailConstants.MAIL_MESSAGE_ID));
    Mailbox box = Mailbox.get("someone@localhost");
    assertEquals(0, box.size());
    // Check if the mimeMessagea has override body and headers
    Mailbox box2 = Mailbox.get("recipient2@localhost");
    assertEquals(1, box2.size());
}
Also used : Exchange(org.apache.camel.Exchange) InternetAddress(javax.mail.internet.InternetAddress) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) Mailbox(org.jvnet.mock_javamail.Mailbox) MimeMessage(javax.mail.internet.MimeMessage) Session(javax.mail.Session) Test(org.junit.Test)

Example 8 with MimeMessage

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

the class MailSearchTermTest 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 5 new messages
    Message[] messages = new Message[6];
    messages[0] = new MimeMessage(sender.getSession());
    messages[0].setSubject("Apache Camel rocks");
    messages[0].setText("I like riding the Camel");
    messages[0].setHeader("Message-ID", "0");
    messages[0].setFrom(new InternetAddress("someone@somewhere.com"));
    messages[1] = new MimeMessage(sender.getSession());
    messages[1].setSubject("Order");
    messages[1].setText("Ordering Camel in Action");
    messages[1].setHeader("Message-ID", "1");
    messages[1].setFrom(new InternetAddress("dude@somewhere.com"));
    messages[2] = new MimeMessage(sender.getSession());
    messages[2].setSubject("Order");
    messages[2].setText("Ordering ActiveMQ in Action");
    messages[2].setHeader("Message-ID", "2");
    messages[2].setFrom(new InternetAddress("dude@somewhere.com"));
    messages[3] = new MimeMessage(sender.getSession());
    messages[3].setSubject("Buy pharmacy");
    messages[3].setText("This is spam");
    messages[3].setHeader("Message-ID", "3");
    messages[3].setFrom(new InternetAddress("spam@me.com"));
    messages[4] = new MimeMessage(sender.getSession());
    messages[4].setSubject("Beers tonight?");
    messages[4].setText("We meet at 7pm the usual place");
    messages[4].setHeader("Message-ID", "4");
    messages[4].setFrom(new InternetAddress("barney@simpsons.com"));
    messages[5] = new MimeMessage(sender.getSession());
    messages[5].setSubject("Spambot attack");
    messages[5].setText("I am attaching you");
    messages[5].setHeader("Message-ID", "5");
    messages[5].setFrom(new InternetAddress("spambot@me.com"));
    folder.appendMessages(messages);
    folder.close(true);
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MimeMessage(javax.mail.internet.MimeMessage) Store(javax.mail.Store) Folder(javax.mail.Folder)

Example 9 with MimeMessage

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

the class MailSearchTermUriConfigLast24HoursTest 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();
    long twoDaysAgo = new Date().getTime() - 2 * 24 * 60 * 60 * 1000L;
    long twentyHoursAgo = new Date().getTime() - 1 * 20 * 60 * 60 * 1000L;
    long oneHourAgo = new Date().getTime() - 1 * 1 * 60 * 60 * 1000L;
    // inserts 5 new messages
    Message[] messages = new Message[6];
    messages[0] = new MimeMessage(sender.getSession());
    messages[0].setSubject("Apache Camel rocks");
    messages[0].setText("I like riding the Camel");
    messages[0].setHeader("Message-ID", "0");
    messages[0].setFrom(new InternetAddress("someone@somewhere.com"));
    messages[0].setSentDate(new Date(twoDaysAgo));
    messages[1] = new MimeMessage(sender.getSession());
    messages[1].setSubject("Order");
    messages[1].setText("Ordering Camel in Action");
    messages[1].setFrom(new InternetAddress("dude@somewhere.com"));
    messages[1].setHeader("Message-ID", "1");
    messages[1].setSentDate(new Date(twoDaysAgo));
    messages[2] = new MimeMessage(sender.getSession());
    messages[2].setSubject("Order");
    messages[2].setText("Ordering ActiveMQ in Action");
    messages[2].setHeader("Message-ID", "2");
    messages[2].setFrom(new InternetAddress("dude@somewhere.com"));
    messages[2].setSentDate(new Date(twentyHoursAgo));
    messages[3] = new MimeMessage(sender.getSession());
    messages[3].setSubject("Buy pharmacy");
    messages[3].setText("This is spam");
    messages[3].setHeader("Message-ID", "3");
    messages[3].setFrom(new InternetAddress("spam@me.com"));
    messages[3].setSentDate(new Date(twentyHoursAgo));
    messages[4] = new MimeMessage(sender.getSession());
    messages[4].setSubject("Beers tonight?");
    messages[4].setText("We meet at 7pm the usual place");
    messages[4].setHeader("Message-ID", "4");
    messages[4].setFrom(new InternetAddress("barney@simpsons.com"));
    messages[4].setSentDate(new Date(oneHourAgo));
    messages[5] = new MimeMessage(sender.getSession());
    messages[5].setSubject("Spambot attack");
    messages[5].setText("I am attaching you");
    messages[5].setHeader("Message-ID", "5");
    messages[5].setFrom(new InternetAddress("spambot@me.com"));
    messages[5].setSentDate(new Date());
    messages[5].setSentDate(new Date(oneHourAgo));
    folder.appendMessages(messages);
    folder.close(true);
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) 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)

Example 10 with MimeMessage

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

the class MimeMessageConsumeTest method testSendAndReceiveMails.

@Test
public void testSendAndReceiveMails() throws Exception {
    Mailbox.clearAll();
    MockEndpoint resultEndpoint = getMockEndpoint("mock:result");
    resultEndpoint.expectedMinimumMessageCount(1);
    Properties properties = new Properties();
    properties.put("mail.smtp.host", "localhost");
    Session session = Session.getInstance(properties, null);
    MimeMessage message = new MimeMessage(session);
    populateMimeMessageBody(message);
    message.setRecipients(Message.RecipientType.TO, "james3@localhost");
    Transport.send(message);
    // lets test the receive worked
    resultEndpoint.assertIsSatisfied();
    Exchange exchange = resultEndpoint.getReceivedExchanges().get(0);
    String text = exchange.getIn().getBody(String.class);
    assertEquals("mail body", body, text);
    assertNotNull("attachments got lost", exchange.getIn().getAttachments());
    for (String s : exchange.getIn().getAttachmentNames()) {
        DataHandler dh = exchange.getIn().getAttachment(s);
        Object content = dh.getContent();
        assertNotNull("Content should not be empty", content);
        assertEquals("log4j2.properties", dh.getName());
    }
}
Also used : Exchange(org.apache.camel.Exchange) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) MimeMessage(javax.mail.internet.MimeMessage) DataHandler(javax.activation.DataHandler) Properties(java.util.Properties) Session(javax.mail.Session) Test(org.junit.Test)

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