Example usage for org.springframework.mail.javamail MimeMessageHelper addAttachment

List of usage examples for org.springframework.mail.javamail MimeMessageHelper addAttachment

Introduction

In this page you can find the example usage for org.springframework.mail.javamail MimeMessageHelper addAttachment.

Prototype

public void addAttachment(String attachmentFilename, InputStreamSource inputStreamSource, String contentType)
        throws MessagingException 

Source Link

Document

Add an attachment to the MimeMessage, taking the content from an org.springframework.core.io.InputStreamResource .

Usage

From source file:org.springframework.integration.samples.mailattachments.MimeMessageParsingTest.java

/**
 * 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.
 *///  w  w w.j  av  a 2s.  com
@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);

    }
}