Search in sources :

Example 1 with ExtendedMimeMessage

use of org.xwiki.mail.ExtendedMimeMessage in project xwiki-platform by xwiki.

the class FileSystemMailContentStoreTest method saveMessageThrowsMailStoreExceptionWhenError.

@Test
public void saveMessageThrowsMailStoreExceptionWhenError() throws Exception {
    Environment environment = this.mocker.getInstance(Environment.class);
    when(environment.getPermanentDirectory()).thenReturn(new File(TEMPORARY_DIRECTORY));
    String batchId = UUID.randomUUID().toString();
    String messageId = "ar1vm0Wca42E/dDn3dsH8ogs3/s=";
    ExtendedMimeMessage message = mock(ExtendedMimeMessage.class);
    when(message.getUniqueMessageId()).thenReturn(messageId);
    this.thrown.expect(MailStoreException.class);
    this.thrown.expectMessage("Failed to save message (id [" + messageId + "], batch id [" + batchId + "]) into file");
    when(message.getContent()).thenReturn("Lorem ipsum dolor sit amet, consectetur adipiscing elit");
    doThrow(new IOException()).when(message).writeTo(any(OutputStream.class));
    this.mocker.getComponentUnderTest().save(batchId, message);
}
Also used : ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) OutputStream(java.io.OutputStream) Environment(org.xwiki.environment.Environment) IOException(java.io.IOException) File(java.io.File) Test(org.junit.Test)

Example 2 with ExtendedMimeMessage

use of org.xwiki.mail.ExtendedMimeMessage in project xwiki-platform by xwiki.

the class FileSystemMailContentStoreTest method saveMessageWithCustomMessageId.

@Test
public void saveMessageWithCustomMessageId() throws Exception {
    String batchId = UUID.randomUUID().toString();
    String mimeMessageId = "<1128820400.0.1419205781342.JavaMail.contact@xwiki.org>";
    ExtendedMimeMessage message = new ExtendedMimeMessage();
    message.setMessageId(mimeMessageId);
    message.setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit");
    this.mocker.getComponentUnderTest().save(batchId, message);
    File tempDir = new File(TEMPORARY_DIRECTORY);
    File batchDirectory = new File(new File(tempDir, this.mocker.getComponentUnderTest().ROOT_DIRECTORY), URLEncoder.encode(batchId, "UTF-8"));
    File messageFile = new File(batchDirectory, URLEncoder.encode(message.getUniqueMessageId(), "UTF-8"));
    InputStream in = new FileInputStream(messageFile);
    String messageContent = IOUtils.toString(in);
    assertTrue(messageContent.contains("Message-ID: " + mimeMessageId));
    assertTrue(messageContent.contains("Lorem ipsum dolor sit amet, consectetur adipiscing elit"));
}
Also used : ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 3 with ExtendedMimeMessage

use of org.xwiki.mail.ExtendedMimeMessage in project xwiki-platform by xwiki.

the class MemoryMailStatusResultTest method getAllErrorTest.

@Test
public void getAllErrorTest() throws Exception {
    MemoryMailStatusResult statusResult = new MemoryMailStatusResult();
    ExtendedMimeMessage message1 = mock(ExtendedMimeMessage.class);
    when(message1.getUniqueMessageId()).thenReturn(UNIQUE_MESSAGE_ID1);
    ExtendedMimeMessage message2 = mock(ExtendedMimeMessage.class);
    when(message2.getUniqueMessageId()).thenReturn(UNIQUE_MESSAGE_ID2);
    ExtendedMimeMessage message3 = mock(ExtendedMimeMessage.class);
    when(message3.getUniqueMessageId()).thenReturn(UNIQUE_MESSAGE_ID3);
    ExtendedMimeMessage message4 = mock(ExtendedMimeMessage.class);
    when(message4.getUniqueMessageId()).thenReturn(UNIQUE_MESSAGE_ID4);
    ExtendedMimeMessage message5 = mock(ExtendedMimeMessage.class);
    when(message5.getUniqueMessageId()).thenReturn(UNIQUE_MESSAGE_ID5);
    statusResult.setStatus(new MailStatus(BATCH_ID, message1, MailState.PREPARE_SUCCESS));
    statusResult.setStatus(new MailStatus(BATCH_ID, message2, MailState.PREPARE_ERROR));
    statusResult.setStatus(new MailStatus(BATCH_ID, message3, MailState.SEND_SUCCESS));
    statusResult.setStatus(new MailStatus(BATCH_ID, message4, MailState.SEND_ERROR));
    statusResult.setStatus(new MailStatus(BATCH_ID, message5, MailState.SEND_FATAL_ERROR));
    List<String> allErrorIds = new ArrayList<>();
    Iterator<MailStatus> it = statusResult.getAllErrors();
    while (it.hasNext()) {
        allErrorIds.add(it.next().getMessageId());
    }
    assertThat(allErrorIds, containsInAnyOrder(UNIQUE_MESSAGE_ID2, UNIQUE_MESSAGE_ID4, UNIQUE_MESSAGE_ID5));
    assertThat(allErrorIds.size(), equalTo(3));
}
Also used : ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) ArrayList(java.util.ArrayList) MailStatus(org.xwiki.mail.MailStatus) Test(org.junit.Test)

Example 4 with ExtendedMimeMessage

use of org.xwiki.mail.ExtendedMimeMessage in project xwiki-platform by xwiki.

the class AbstractTemplateMimeMessageFactory method createMessage.

@Override
public MimeMessage createMessage(Object templateReferenceObject, Map<String, Object> parameters) throws MessagingException {
    DocumentReference templateReference = getTypedSource(templateReferenceObject, DocumentReference.class);
    // Note: We don't create a Session here ATM since it's not required. The returned MimeMessage will be
    // given a valid Session when it's deserialized from the mail content store for sending.
    ExtendedMimeMessage message = new ExtendedMimeMessage();
    // Handle optional "from" address.
    Address from = this.converterManager.convert(Address.class, parameters.get("from"));
    if (from != null) {
        message.setFrom(from);
    }
    // Handle optional "to", "cc" and "bcc" addresses.
    setRecipient(message, Message.RecipientType.TO, parameters.get("to"));
    setRecipient(message, Message.RecipientType.CC, parameters.get("cc"));
    setRecipient(message, Message.RecipientType.BCC, parameters.get("bcc"));
    // Handle optional "type" parameter to set the mail type
    // Set the Message type if passed in parameters
    String type = (String) parameters.get("type");
    if (type != null) {
        message.setType(type);
    }
    // Handle the subject. Get it from the template
    Map<String, Object> velocityVariables = (Map<String, Object>) parameters.get("velocityVariables");
    Object localeValue = parameters.get("language");
    String subject = getTemplateManager().evaluate(templateReference, "subject", velocityVariables, localeValue);
    message.setSubject(subject);
    // Add a default body part taken from the template.
    Multipart multipart = new MimeMultipart("mixed");
    multipart.addBodyPart(getMimeBodyPartFactory().create(templateReference, parameters));
    message.setContent(multipart);
    return message;
}
Also used : ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) Address(javax.mail.Address) MimeMultipart(javax.mail.internet.MimeMultipart) Map(java.util.Map) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 5 with ExtendedMimeMessage

use of org.xwiki.mail.ExtendedMimeMessage in project xwiki-platform by xwiki.

the class AbstractMessageIterator method createMessage.

/**
 * @return the MimeMessage as the current element of Iterator.
 * @throws MessagingException  when an error occurs
 */
public ExtendedMimeMessage createMessage() throws MessagingException {
    ExtendedMimeMessage message = createMessageInternal();
    // Set the Message type if passed in parameters
    String type = (String) parameters.get("type");
    if (type != null) {
        message.setType(type);
    }
    return message;
}
Also used : ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage)

Aggregations

ExtendedMimeMessage (org.xwiki.mail.ExtendedMimeMessage)21 Test (org.junit.Test)13 Map (java.util.Map)7 MailStatus (org.xwiki.mail.MailStatus)6 File (java.io.File)5 MailContentStore (org.xwiki.mail.MailContentStore)5 MailListener (org.xwiki.mail.MailListener)5 DocumentReference (org.xwiki.model.reference.DocumentReference)5 FileInputStream (java.io.FileInputStream)4 InputStream (java.io.InputStream)4 Properties (java.util.Properties)4 Session (javax.mail.Session)4 MimeMessage (javax.mail.internet.MimeMessage)4 ArrayList (java.util.ArrayList)3 MailSender (org.xwiki.mail.MailSender)3 MailStoreException (org.xwiki.mail.MailStoreException)3 XWikiContext (com.xpn.xwiki.XWikiContext)2 HashMap (java.util.HashMap)2 MessagingException (javax.mail.MessagingException)2 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)2