Search in sources :

Example 71 with BodyPart

use of javax.mail.BodyPart 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

BodyPart (javax.mail.BodyPart)71 MimeMultipart (javax.mail.internet.MimeMultipart)52 MimeBodyPart (javax.mail.internet.MimeBodyPart)41 MessagingException (javax.mail.MessagingException)23 MimeMessage (javax.mail.internet.MimeMessage)19 Header (javax.mail.Header)15 Multipart (javax.mail.Multipart)15 DispositionNotification (com.sun.mail.dsn.DispositionNotification)13 IOException (java.io.IOException)13 HashMap (java.util.HashMap)13 InternetHeaders (javax.mail.internet.InternetHeaders)13 ByteString (com.linkedin.data.ByteString)12 DataHandler (javax.activation.DataHandler)10 Test (org.testng.annotations.Test)10 Session (javax.mail.Session)9 InternetAddress (javax.mail.internet.InternetAddress)8 Properties (java.util.Properties)7 File (java.io.File)6 InputStream (java.io.InputStream)6 FileDataSource (javax.activation.FileDataSource)6