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);
}
}
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);
}
}
Aggregations