Search in sources :

Example 1 with InternetAddress

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

the class MailSorter method compareMessageProperty.

/**
     * Compare the value of the property of the two messages.
     *
     * @param msg1     Message 1
     * @param msg2     Message 2
     * @param property Property to compare
     * @return msg1.property.compareTo(msg2.property)
     * @throws javax.mail.MessagingException If message data could not be read.
     */
private static int compareMessageProperty(Message msg1, Message msg2, SortTerm property) throws MessagingException {
    if (property.equals(SortTerm.TO)) {
        InternetAddress addr1 = (InternetAddress) msg1.getRecipients(Message.RecipientType.TO)[0];
        InternetAddress addr2 = (InternetAddress) msg2.getRecipients(Message.RecipientType.TO)[0];
        return addr1.getAddress().compareTo(addr2.getAddress());
    } else if (property.equals(SortTerm.CC)) {
        InternetAddress addr1 = (InternetAddress) msg1.getRecipients(Message.RecipientType.CC)[0];
        InternetAddress addr2 = (InternetAddress) msg2.getRecipients(Message.RecipientType.CC)[0];
        return addr1.getAddress().compareTo(addr2.getAddress());
    } else if (property.equals(SortTerm.FROM)) {
        InternetAddress addr1 = (InternetAddress) msg1.getFrom()[0];
        InternetAddress addr2 = (InternetAddress) msg2.getFrom()[0];
        return addr1.getAddress().compareTo(addr2.getAddress());
    } else if (property.equals(SortTerm.ARRIVAL)) {
        Date arr1 = msg1.getReceivedDate();
        Date arr2 = msg2.getReceivedDate();
        return arr1.compareTo(arr2);
    } else if (property.equals(SortTerm.DATE)) {
        Date sent1 = msg1.getSentDate();
        Date sent2 = msg2.getSentDate();
        return sent1.compareTo(sent2);
    } else if (property.equals(SortTerm.SIZE)) {
        int size1 = msg1.getSize();
        int size2 = msg2.getSize();
        return Integer.compare(size1, size2);
    } else if (property.equals(SortTerm.SUBJECT)) {
        String sub1 = msg1.getSubject();
        String sub2 = msg2.getSubject();
        return sub1.compareTo(sub2);
    }
    throw new IllegalArgumentException(String.format("Unknown sort term: %s", property.toString()));
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Date(java.util.Date)

Example 2 with InternetAddress

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

the class SearchTermBuilderTest method testSearchTermSentLast24Hours.

public void testSearchTermSentLast24Hours() throws Exception {
    SearchTermBuilder build = new SearchTermBuilder();
    long offset = -1 * (24 * 60 * 60 * 1000L);
    SearchTerm st = build.subject("Camel").sentNow(Comparison.GE, offset).build();
    assertNotNull(st);
    // create dummy message
    Mailbox.clearAll();
    JavaMailSender sender = new DefaultJavaMailSender();
    MimeMessage msg = new MimeMessage(sender.getSession());
    msg.setSubject("Yeah Camel rocks");
    msg.setText("Apache Camel is a cool project. Have a fun ride.");
    msg.setFrom(new InternetAddress("someone@somewhere.com"));
    msg.setSentDate(new Date());
    assertTrue("Should match message", st.match(msg));
    MimeMessage msg2 = new MimeMessage(sender.getSession());
    msg2.setSubject("Camel in Action");
    msg2.setText("Hey great book");
    msg2.setFrom(new InternetAddress("dude@apache.org"));
    // mark it as sent 2 days ago
    long twoDays = 2 * 24 * 60 * 60 * 1000L;
    long time = new Date().getTime() - twoDays;
    msg2.setSentDate(new Date(time));
    assertFalse("Should not match message as its too old", st.match(msg2));
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) SearchTerm(javax.mail.search.SearchTerm) Date(java.util.Date)

Example 3 with InternetAddress

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

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

use of javax.mail.internet.InternetAddress 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)

Aggregations

InternetAddress (javax.mail.internet.InternetAddress)255 MimeMessage (javax.mail.internet.MimeMessage)106 MessagingException (javax.mail.MessagingException)69 Session (javax.mail.Session)49 Properties (java.util.Properties)45 ArrayList (java.util.ArrayList)42 Address (javax.mail.Address)41 Message (javax.mail.Message)40 Date (java.util.Date)38 JavaMailInternetAddress (com.zimbra.common.mime.shim.JavaMailInternetAddress)36 AddressException (javax.mail.internet.AddressException)34 X509Certificate (java.security.cert.X509Certificate)32 MimeBodyPart (javax.mail.internet.MimeBodyPart)30 Test (org.junit.Test)29 IOException (java.io.IOException)26 MimeMultipart (javax.mail.internet.MimeMultipart)26 PolicyExpression (org.nhindirect.policy.PolicyExpression)18 HashMap (java.util.HashMap)17 CertificateResolver (org.nhindirect.stagent.cert.CertificateResolver)17 PolicyResolver (org.nhindirect.stagent.policy.PolicyResolver)17