Search in sources :

Example 1 with EmailFragment

use of org.springframework.integration.samples.mailattachments.support.EmailFragment in project spring-integration-samples by spring-projects.

the class MimeMessageParsingTest method testProcessingOfNestedEmailAttachments.

/**
 * This test will create a Mime Message that in return contains another
 * mime message. The nested mime message contains an attachment.
 *
 * The root message consist of both HTML and Text message.
 */
@Test
public void testProcessingOfNestedEmailAttachments() {
    final JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
    mailSender.setPort(this.wiserPort);
    final MimeMessage rootMessage = mailSender.createMimeMessage();
    try {
        final MimeMessageHelper messageHelper = new MimeMessageHelper(rootMessage, true);
        messageHelper.setFrom("testfrom@springintegration.org");
        messageHelper.setTo("testto@springintegration.org");
        messageHelper.setSubject("Parsing of Attachments");
        messageHelper.setText("Spring Integration Rocks!", "Spring Integration <b>Rocks</b>!");
        final String pictureName = "picture.png";
        final ByteArrayResource byteArrayResource = getFileData(pictureName);
        messageHelper.addInline("picture12345", byteArrayResource, "image/png");
    } catch (MessagingException e) {
        throw new MailParseException(e);
    }
    mailSender.send(rootMessage);
    final List<WiserMessage> wiserMessages = wiser.getMessages();
    Assert.assertTrue(wiserMessages.size() == 1);
    boolean foundTextMessage = false;
    boolean foundPicture = false;
    boolean foundHtmlMessage = false;
    for (WiserMessage wiserMessage : wiserMessages) {
        List<EmailFragment> emailFragments = new ArrayList<EmailFragment>();
        try {
            final MimeMessage mailMessage = wiserMessage.getMimeMessage();
            EmailParserUtils.handleMessage(null, mailMessage, emailFragments);
        } catch (MessagingException e) {
            throw new IllegalStateException("Error while retrieving Mime Message.");
        }
        Assert.assertTrue(emailFragments.size() == 3);
        for (EmailFragment emailFragment : emailFragments) {
            if ("<picture12345>".equals(emailFragment.getFilename())) {
                foundPicture = true;
            }
            if ("message.txt".equals(emailFragment.getFilename())) {
                foundTextMessage = true;
            }
            if ("message.html".equals(emailFragment.getFilename())) {
                foundHtmlMessage = true;
            }
        }
        Assert.assertTrue(foundPicture);
        Assert.assertTrue(foundTextMessage);
        Assert.assertTrue(foundHtmlMessage);
    }
}
Also used : MessagingException(javax.mail.MessagingException) ArrayList(java.util.ArrayList) WiserMessage(org.subethamail.wiser.WiserMessage) ByteArrayResource(org.springframework.core.io.ByteArrayResource) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) JavaMailSenderImpl(org.springframework.mail.javamail.JavaMailSenderImpl) MimeMessage(javax.mail.internet.MimeMessage) MailParseException(org.springframework.mail.MailParseException) EmailFragment(org.springframework.integration.samples.mailattachments.support.EmailFragment) Test(org.junit.Test)

Example 2 with EmailFragment

use of org.springframework.integration.samples.mailattachments.support.EmailFragment in project spring-integration-samples by spring-projects.

the class MimeMessageParsingTest method testProcessingOfEmailAttachments.

/**
 * This test will create a Mime Message that contains an Attachment, send it
 * to an SMTP Server (Using Wiser) and retrieve and process the Mime Message.
 *
 * This test verifies that the parsing of the retrieved Mime Message is
 * successful and that the correct number of {@link EmailFragment}s is created.
 */
@Test
public void testProcessingOfEmailAttachments() {
    final JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
    mailSender.setPort(this.wiserPort);
    final MimeMessage message = mailSender.createMimeMessage();
    final String pictureName = "picture.png";
    final ByteArrayResource byteArrayResource = getFileData(pictureName);
    try {
        final MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom("testfrom@springintegration.org");
        helper.setTo("testto@springintegration.org");
        helper.setSubject("Parsing of Attachments");
        helper.setText("Spring Integration Rocks!");
        helper.addAttachment(pictureName, byteArrayResource, "image/png");
    } catch (MessagingException e) {
        throw new MailParseException(e);
    }
    mailSender.send(message);
    final List<WiserMessage> wiserMessages = wiser.getMessages();
    Assert.assertTrue(wiserMessages.size() == 1);
    boolean foundTextMessage = false;
    boolean foundPicture = false;
    for (WiserMessage wiserMessage : wiserMessages) {
        final List<EmailFragment> emailFragments = new ArrayList<EmailFragment>();
        try {
            final MimeMessage mailMessage = wiserMessage.getMimeMessage();
            EmailParserUtils.handleMessage(null, mailMessage, emailFragments);
        } catch (MessagingException e) {
            throw new IllegalStateException("Error while retrieving Mime Message.");
        }
        Assert.assertTrue(emailFragments.size() == 2);
        for (EmailFragment emailFragment : emailFragments) {
            if ("picture.png".equals(emailFragment.getFilename())) {
                foundPicture = true;
            }
            if ("message.txt".equals(emailFragment.getFilename())) {
                foundTextMessage = true;
            }
        }
        Assert.assertTrue(foundPicture);
        Assert.assertTrue(foundTextMessage);
    }
}
Also used : MessagingException(javax.mail.MessagingException) ArrayList(java.util.ArrayList) WiserMessage(org.subethamail.wiser.WiserMessage) ByteArrayResource(org.springframework.core.io.ByteArrayResource) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) JavaMailSenderImpl(org.springframework.mail.javamail.JavaMailSenderImpl) MimeMessage(javax.mail.internet.MimeMessage) MailParseException(org.springframework.mail.MailParseException) EmailFragment(org.springframework.integration.samples.mailattachments.support.EmailFragment) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)2 MessagingException (javax.mail.MessagingException)2 MimeMessage (javax.mail.internet.MimeMessage)2 Test (org.junit.Test)2 ByteArrayResource (org.springframework.core.io.ByteArrayResource)2 EmailFragment (org.springframework.integration.samples.mailattachments.support.EmailFragment)2 MailParseException (org.springframework.mail.MailParseException)2 JavaMailSenderImpl (org.springframework.mail.javamail.JavaMailSenderImpl)2 MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)2 WiserMessage (org.subethamail.wiser.WiserMessage)2