Search in sources :

Example 1 with SimpleSmtpServer

use of com.dumbster.smtp.SimpleSmtpServer in project ddf by codice.

the class SmtpClientImplITCase method testSend.

@Test
public void testSend() throws IOException, MessagingException, ExecutionException, InterruptedException {
    int port = findAvailablePort();
    SimpleSmtpServer server = SimpleSmtpServer.start(port);
    SmtpClientImpl emailService = new SmtpClientImpl();
    emailService.setHostName(HOSTNAME);
    emailService.setPortNumber(port);
    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);
    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));
}
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) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) SmtpMessage(com.dumbster.smtp.SmtpMessage) SimpleSmtpServer(com.dumbster.smtp.SimpleSmtpServer) Iterator(java.util.Iterator) MimeBodyPart(javax.mail.internet.MimeBodyPart) Session(javax.mail.Session) Test(org.junit.Test)

Example 2 with SimpleSmtpServer

use of com.dumbster.smtp.SimpleSmtpServer 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

SimpleSmtpServer (com.dumbster.smtp.SimpleSmtpServer)2 SmtpMessage (com.dumbster.smtp.SmtpMessage)2 Iterator (java.util.Iterator)2 BodyPart (javax.mail.BodyPart)2 Multipart (javax.mail.Multipart)2 Session (javax.mail.Session)2 InternetAddress (javax.mail.internet.InternetAddress)2 MimeBodyPart (javax.mail.internet.MimeBodyPart)2 MimeMessage (javax.mail.internet.MimeMessage)2 MimeMultipart (javax.mail.internet.MimeMultipart)2 Test (org.junit.Test)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 DataHandler (javax.activation.DataHandler)1 FileDataSource (javax.activation.FileDataSource)1