@Test
public void testSendMessageWithAttachment() throws Exception {
final String ATTACHMENT_NAME = "boring-attachment.txt";
//mock smtp server
Wiser wiser = new Wiser();
int port = 2525 + (int)(Math.random() * 100);
mailSender.setPort(port);
wiser.setPort(port);
wiser.start();
Date dte = new Date();
String emailSubject = "grepster testSendMessageWithAttachment: " + dte;
String emailBody = "Body of the grepster testSendMessageWithAttachment message sent at: " + dte;
ClassPathResource cpResource = new ClassPathResource("/test-attachment.txt");
// a null from should work
mailEngine.sendMessage(new String[] {
"foo@bar.com"
}, null, cpResource, emailBody, emailSubject, ATTACHMENT_NAME);
mailEngine.sendMessage(new String[] {
"foo@bar.com"
}, mailMessage.getFrom(), cpResource, emailBody, emailSubject, ATTACHMENT_NAME);
wiser.stop();
// one without and one with from
assertTrue(wiser.getMessages().size() == 2);
WiserMessage wm = wiser.getMessages().get(0);
MimeMessage mm = wm.getMimeMessage();
Object o = wm.getMimeMessage().getContent();
assertTrue(o instanceof MimeMultipart);
MimeMultipart multi = (MimeMultipart)o;