use of org.xwiki.mail.script.ScriptMimeMessage in project xwiki-platform by xwiki.
the class ScriptingIntegrationTest method sendHTMLAndCalendarInvitationMail.
@Test
public void sendHTMLAndCalendarInvitationMail() throws Exception {
ScriptMimeMessage message = this.scriptService.createMessage("john@doe.com", "subject");
message.addPart("text/html", "<font size=\"\\\"2\\\"\">simple meeting invitation</font>");
// @formatter:off
String calendarContent = "BEGIN:VCALENDAR\r\n" + "METHOD:REQUEST\r\n" + "PRODID: Meeting\r\n" + "VERSION:2.0\r\n" + "BEGIN:VEVENT\r\n" + "DTSTAMP:20140616T164100\r\n" + "DTSTART:20140616T164100\r\n" + "DTEND:20140616T194100\r\n" + "SUMMARY:test request\r\n" + "UID:324\r\n" + "ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE:MAILTO:john@doe.com\r\n" + "ORGANIZER:MAILTO:john@doe.com\r\n" + "LOCATION:on the net\r\n" + "DESCRIPTION:learn some stuff\r\n" + "SEQUENCE:0\r\n" + "PRIORITY:5\r\n" + "CLASS:PUBLIC\r\n" + "STATUS:CONFIRMED\r\n" + "TRANSP:OPAQUE\r\n" + "BEGIN:VALARM\r\n" + "ACTION:DISPLAY\r\n" + "DESCRIPTION:REMINDER\r\n" + "TRIGGER;RELATED=START:-PT00H15M00S\r\n" + "END:VALARM\r\n" + "END:VEVENT\r\n" + "END:VCALENDAR";
// @formatter:on
message.addPart("text/calendar;method=CANCEL", calendarContent, Collections.<String, Object>singletonMap("headers", Collections.singletonMap("Content-Class", "urn:content-classes:calendarmessage")));
ScriptMailResult result = this.scriptService.send(Arrays.asList(message));
// Verify that there are no errors and that 1 mail was sent
assertNull(this.scriptService.getLastError());
assertTrue(result.getStatusResult().isProcessed());
assertEquals(1, result.getStatusResult().getProcessedMailCount());
// Verify that all mails have been sent properly
assertFalse("There should not be any failed result!", result.getStatusResult().getByState(MailState.SEND_ERROR).hasNext());
assertFalse("There should not be any failed result!", result.getStatusResult().getByState(MailState.PREPARE_ERROR).hasNext());
assertFalse("There should not be any mails in the ready state!", result.getStatusResult().getByState(MailState.PREPARE_SUCCESS).hasNext());
// Verify that the mail has been received (wait maximum 30 seconds).
this.mail.waitForIncomingEmail(30000L, 1);
MimeMessage[] messages = this.mail.getReceivedMessages();
assertEquals(1, messages.length);
assertEquals("subject", messages[0].getHeader("Subject", null));
assertEquals("john@doe.com", messages[0].getHeader("To", null));
assertEquals(2, ((MimeMultipart) messages[0].getContent()).getCount());
BodyPart htmlBodyPart = ((MimeMultipart) messages[0].getContent()).getBodyPart(0);
assertEquals("text/html", htmlBodyPart.getHeader("Content-Type")[0]);
assertEquals("<font size=\"\\\"2\\\"\">simple meeting invitation</font>", htmlBodyPart.getContent());
BodyPart calendarBodyPart = ((MimeMultipart) messages[0].getContent()).getBodyPart(1);
assertEquals("text/calendar;method=CANCEL", calendarBodyPart.getHeader("Content-Type")[0]);
InputStream is = (InputStream) calendarBodyPart.getContent();
assertEquals(calendarContent, IOUtils.toString(is));
}
use of org.xwiki.mail.script.ScriptMimeMessage in project xwiki-platform by xwiki.
the class ScriptingIntegrationTest method sendTextMail.
@Test
public void sendTextMail() throws Exception {
ScriptMimeMessage message1 = this.scriptService.createMessage("john@doe.com", "subject");
message1.addPart("text/plain", "some text here");
ScriptMimeMessage message2 = this.scriptService.createMessage("john@doe.com", "subject");
message2.addPart("text/plain", "some text here");
ScriptMimeMessage message3 = this.scriptService.createMessage("john@doe.com", "subject");
message3.addPart("text/plain", "some text here");
// Send 3 mails (3 times the same mail) to verify we can send several emails at once.
List<ScriptMimeMessage> messagesList = Arrays.asList(message1, message2, message3);
ScriptMailResult result = this.scriptService.sendAsynchronously(messagesList, "memory");
// Verify that there are no errors
assertNull(this.scriptService.getLastError());
// Wait for all mails to be sent
result.getStatusResult().waitTillProcessed(30000L);
assertTrue(result.getStatusResult().isProcessed());
// Verify that all mails have been sent properly
assertFalse("There should not be any failed result!", result.getStatusResult().getByState(MailState.SEND_ERROR).hasNext());
assertFalse("There should not be any failed result!", result.getStatusResult().getByState(MailState.PREPARE_ERROR).hasNext());
assertFalse("There should not be any mails in the ready state!", result.getStatusResult().getByState(MailState.PREPARE_SUCCESS).hasNext());
// Verify that the mails have been received (wait maximum 30 seconds).
this.mail.waitForIncomingEmail(30000L, 3);
MimeMessage[] messages = this.mail.getReceivedMessages();
assertEquals(3, messages.length);
assertEquals("subject", messages[0].getHeader("Subject", null));
assertEquals("john@doe.com", messages[0].getHeader("To", null));
assertEquals(1, ((MimeMultipart) messages[0].getContent()).getCount());
BodyPart textBodyPart = ((MimeMultipart) messages[0].getContent()).getBodyPart(0);
assertEquals("text/plain", textBodyPart.getHeader("Content-Type")[0]);
assertEquals("some text here", textBodyPart.getContent());
}
Aggregations