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));
}
}
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);
}
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);
}
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);
}
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);
}
Aggregations