Search in sources :

Example 1 with ScriptMimeMessage

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));
}
Also used : BodyPart(javax.mail.BodyPart) ScriptMimeMessage(org.xwiki.mail.script.ScriptMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) InputStream(java.io.InputStream) ScriptMimeMessage(org.xwiki.mail.script.ScriptMimeMessage) ScriptMailResult(org.xwiki.mail.script.ScriptMailResult) Test(org.junit.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Example 2 with ScriptMimeMessage

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());
}
Also used : BodyPart(javax.mail.BodyPart) ScriptMimeMessage(org.xwiki.mail.script.ScriptMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) ScriptMimeMessage(org.xwiki.mail.script.ScriptMimeMessage) ScriptMailResult(org.xwiki.mail.script.ScriptMailResult) Test(org.junit.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Aggregations

ServerSetupTest (com.icegreen.greenmail.util.ServerSetupTest)2 BodyPart (javax.mail.BodyPart)2 MimeMessage (javax.mail.internet.MimeMessage)2 MimeMultipart (javax.mail.internet.MimeMultipart)2 Test (org.junit.Test)2 ScriptMailResult (org.xwiki.mail.script.ScriptMailResult)2 ScriptMimeMessage (org.xwiki.mail.script.ScriptMimeMessage)2 InputStream (java.io.InputStream)1