use of javax.activation.DataHandler in project camel by apache.
the class MailAttachmentDuplicateNamesTest method testSendAndRecieveMailWithAttachmentsWithDuplicateNames.
@Test
public void testSendAndRecieveMailWithAttachmentsWithDuplicateNames() throws Exception {
// clear mailbox
Mailbox.clearAll();
// START SNIPPET: e1
// create an exchange with a normal body and attachment to be produced as email
Endpoint endpoint = context.getEndpoint("smtp://james@mymailserver.com?password=secret");
// create the exchange with the mail message that is multipart with a file and a Hello World text/plain message.
Exchange exchange = endpoint.createExchange();
Message in = exchange.getIn();
in.setBody("Hello World");
in.addAttachment("logo.jpeg", new DataHandler(new FileDataSource("src/test/data/logo.jpeg")));
in.addAttachment("logo.jpeg", new DataHandler(new FileDataSource("src/test/data/logo.jpeg")));
// create a producer that can produce the exchange (= send the mail)
Producer producer = endpoint.createProducer();
// start the producer
producer.start();
// and let it go (processes the exchange by sending the email)
producer.process(exchange);
// END SNIPPET: e1
// need some time for the mail to arrive on the inbox (consumed and sent to the mock)
Thread.sleep(2000);
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(1);
Exchange out = mock.assertExchangeReceived(0);
mock.assertIsSatisfied();
// plain text
assertEquals("Hello World", out.getIn().getBody(String.class));
// attachment
Map<String, DataHandler> attachments = out.getIn().getAttachments();
assertNotNull("Should have attachments", attachments);
assertEquals(1, attachments.size());
DataHandler handler = out.getIn().getAttachment("logo.jpeg");
assertNotNull("The logo should be there", handler);
// content type should match
boolean match1 = "image/jpeg; name=logo.jpeg".equals(handler.getContentType());
boolean match2 = "application/octet-stream; name=logo.jpeg".equals(handler.getContentType());
assertTrue("Should match 1 or 2", match1 || match2);
producer.stop();
}
use of javax.activation.DataHandler in project camel by apache.
the class MailAttachmentTest method testSendAndReceiveMailWithAttachments.
@Test
public void testSendAndReceiveMailWithAttachments() throws Exception {
// clear mailbox
Mailbox.clearAll();
// START SNIPPET: e1
// create an exchange with a normal body and attachment to be produced as email
Endpoint endpoint = context.getEndpoint("smtp://james@mymailserver.com?password=secret");
// create the exchange with the mail message that is multipart with a file and a Hello World text/plain message.
Exchange exchange = endpoint.createExchange();
Message in = exchange.getIn();
in.setBody("Hello World");
DefaultAttachment att = new DefaultAttachment(new FileDataSource("src/test/data/logo.jpeg"));
att.addHeader("Content-Description", "some sample content");
in.addAttachmentObject("logo.jpeg", att);
// create a producer that can produce the exchange (= send the mail)
Producer producer = endpoint.createProducer();
// start the producer
producer.start();
// and let it go (processes the exchange by sending the email)
producer.process(exchange);
// END SNIPPET: e1
// need some time for the mail to arrive on the inbox (consumed and sent to the mock)
Thread.sleep(2000);
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(1);
Exchange out = mock.assertExchangeReceived(0);
mock.assertIsSatisfied();
// plain text
assertEquals("Hello World", out.getIn().getBody(String.class));
// attachment
Map<String, Attachment> attachments = out.getIn().getAttachmentObjects();
assertNotNull("Should have attachments", attachments);
assertEquals(1, attachments.size());
Attachment attachment = out.getIn().getAttachmentObject("logo.jpeg");
DataHandler handler = attachment.getDataHandler();
assertNotNull("The logo should be there", handler);
// content type should match
boolean match1 = "image/jpeg; name=logo.jpeg".equals(handler.getContentType());
boolean match2 = "application/octet-stream; name=logo.jpeg".equals(handler.getContentType());
assertTrue("Should match 1 or 2", match1 || match2);
assertEquals("Handler name should be the file name", "logo.jpeg", handler.getName());
assertEquals("some sample content", attachment.getHeader("content-description"));
producer.stop();
}
use of javax.activation.DataHandler in project camel by apache.
the class NestedMimeMessageConsumeTest method testNestedMultipart.
@Test
public void testNestedMultipart() throws Exception {
Mailbox.clearAll();
MockEndpoint resultEndpoint = getMockEndpoint("mock:result");
resultEndpoint.expectedMinimumMessageCount(1);
prepareMailbox("james3");
resultEndpoint.assertIsSatisfied();
Exchange exchange = resultEndpoint.getReceivedExchanges().get(0);
String text = exchange.getIn().getBody(String.class);
assertThat(text, containsString("Test with bold face, pictures and attachments"));
assertEquals("text/plain; charset=us-ascii", exchange.getIn().getHeader("Content-Type"));
Set<String> attachmentNames = exchange.getIn().getAttachmentNames();
assertNotNull("attachments got lost", attachmentNames);
assertEquals(2, attachmentNames.size());
for (String s : attachmentNames) {
Attachment att = exchange.getIn().getAttachmentObject(s);
DataHandler dh = att.getDataHandler();
Object content = dh.getContent();
assertNotNull("Content should not be empty", content);
assertThat(dh.getName(), anyOf(equalTo("image001.png"), equalTo("test.txt")));
}
}
use of javax.activation.DataHandler in project camel by apache.
the class MailSplitAttachmentsTest method setup.
@Before
public void setup() {
// create the exchange with the mail message that is multipart with a file and a Hello World text/plain message.
endpoint = context.getEndpoint("smtp://james@mymailserver.com?password=secret");
exchange = endpoint.createExchange();
Message in = exchange.getIn();
in.setBody("Hello World");
in.addAttachment("logo.jpeg", new DataHandler(new FileDataSource("src/test/data/logo.jpeg")));
in.addAttachment("license.txt", new DataHandler(new FileDataSource("src/main/resources/META-INF/LICENSE.txt")));
}
use of javax.activation.DataHandler 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());
}
}
Aggregations