Search in sources :

Example 41 with FileDataSource

use of javax.activation.FileDataSource in project ddf by codice.

the class SmtpClientImplITCase method testSendWithAttachments.

@Test
public void testSendWithAttachments() throws IOException, MessagingException, ExecutionException, InterruptedException {
    int port = findAvailablePort();
    SimpleSmtpServer server = SimpleSmtpServer.start(port);
    SmtpClientImpl emailService = new SmtpClientImpl();
    emailService.setHostName(HOSTNAME);
    emailService.setPortNumber(port);
    File tmpFile = folder.newFile("email.txt");
    try (OutputStream os = new FileOutputStream(tmpFile)) {
        os.write(ATTACHMENT_TEXT.getBytes());
    }
    Session session = emailService.createSession();
    MimeMessage mimeMessage = new MimeMessage(session);
    mimeMessage.setFrom(new InternetAddress(FROM_ADDR));
    mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(TO_ADDR));
    mimeMessage.setSubject(SUBJECT);
    BodyPart messageBodyPart = new MimeBodyPart();
    messageBodyPart.setText(BODY);
    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(messageBodyPart);
    messageBodyPart = new MimeBodyPart();
    messageBodyPart.setDataHandler(new DataHandler(new FileDataSource(tmpFile)));
    messageBodyPart.setFileName(ATTACHMENT_FILENAME);
    multipart.addBodyPart(messageBodyPart);
    mimeMessage.setContent(multipart);
    emailService.send(mimeMessage).get();
    server.stop();
    assertThat(server.getReceivedEmailSize(), is(1));
    Iterator emailIterator = server.getReceivedEmail();
    SmtpMessage email = (SmtpMessage) emailIterator.next();
    assertThat(email.getHeaderValue(SUBJECT_HEADER), is(SUBJECT));
    assertThat(email.getHeaderValue(FROM_HEADER), containsString(FROM_ADDR));
    assertThat(email.getHeaderValue(TO_HEADER), containsString(TO_ADDR));
    assertThat(email.getBody(), containsString(BODY));
    assertThat(email.getBody(), containsString(ATTACHMENT_TEXT));
    assertThat(email.getBody(), containsString(ATTACHMENT_FILENAME));
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) InternetAddress(javax.mail.internet.InternetAddress) Multipart(javax.mail.Multipart) MimeMultipart(javax.mail.internet.MimeMultipart) SmtpMessage(com.dumbster.smtp.SmtpMessage) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) DataHandler(javax.activation.DataHandler) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) FileOutputStream(java.io.FileOutputStream) SimpleSmtpServer(com.dumbster.smtp.SimpleSmtpServer) FileDataSource(javax.activation.FileDataSource) Iterator(java.util.Iterator) MimeBodyPart(javax.mail.internet.MimeBodyPart) File(java.io.File) Session(javax.mail.Session) Test(org.junit.Test)

Aggregations

FileDataSource (javax.activation.FileDataSource)41 DataHandler (javax.activation.DataHandler)33 Exchange (org.apache.camel.Exchange)18 Message (org.apache.camel.Message)15 Test (org.junit.Test)15 File (java.io.File)12 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)12 MimeBodyPart (javax.mail.internet.MimeBodyPart)11 MimeMultipart (javax.mail.internet.MimeMultipart)7 Producer (org.apache.camel.Producer)7 DataSource (javax.activation.DataSource)6 BodyPart (javax.mail.BodyPart)6 Multipart (javax.mail.Multipart)6 Endpoint (org.apache.camel.Endpoint)6 Processor (org.apache.camel.Processor)6 MessagingException (javax.mail.MessagingException)5 InternetAddress (javax.mail.internet.InternetAddress)5 MimeMessage (javax.mail.internet.MimeMessage)5 List (java.util.List)4 DefaultAttachment (org.apache.camel.impl.DefaultAttachment)4