Search in sources :

Example 16 with ExtendedMimeMessage

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

the class FileSystemMailContentStoreTest method saveMessageWhenInstableCustomMessageID.

@Test
public void saveMessageWhenInstableCustomMessageID() throws Exception {
    String batchId = UUID.randomUUID().toString();
    String mimeMessageId = "<1128820400.0.1419205781342.JavaMail.contact@xwiki.org>";
    ExtendedMimeMessage message = new ExtendedMimeMessage();
    message.setHeader("Message-ID", 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: " + message.getMessageID()));
    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 17 with ExtendedMimeMessage

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

the class FileSystemMailContentStoreTest method saveMessage.

@Test
public void saveMessage() throws Exception {
    String batchId = UUID.randomUUID().toString();
    ExtendedMimeMessage message = new ExtendedMimeMessage();
    message.setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit");
    this.mocker.getComponentUnderTest().save(batchId, message);
    String messageId = message.getMessageID();
    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: " + messageId));
    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 18 with ExtendedMimeMessage

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

the class MemoryMailListenerTest method onErrorAndGetMailStatusResult.

@Test
public void onErrorAndGetMailStatusResult() throws Exception {
    MemoryMailListener listener = this.mocker.getComponentUnderTest();
    String batchId = UUID.randomUUID().toString();
    listener.onPrepareBegin(batchId, Collections.<String, Object>emptyMap());
    ExtendedMimeMessage message1 = mock(ExtendedMimeMessage.class);
    when(message1.getUniqueMessageId()).thenReturn(UNIQUE_MESSAGE_ID1);
    when(message1.getType()).thenReturn("mailtype1");
    listener.onPrepareMessageError(message1, new Exception("error1"), Collections.<String, Object>emptyMap());
    ExtendedMimeMessage message2 = mock(ExtendedMimeMessage.class);
    when(message2.getUniqueMessageId()).thenReturn(UNIQUE_MESSAGE_ID2);
    when(message2.getType()).thenReturn("mailtype2");
    listener.onPrepareMessageError(message2, new Exception("error2"), Collections.<String, Object>emptyMap());
    Iterator<MailStatus> results = listener.getMailStatusResult().getByState(MailState.PREPARE_ERROR);
    assertTrue("These should be mails in error!", results.hasNext());
    MailStatus status = results.next();
    assertEquals("Exception: error1", status.getErrorSummary());
    assertTrue(status.getErrorDescription().contains("error1"));
    assertEquals(batchId, status.getBatchId());
    assertEquals(UNIQUE_MESSAGE_ID1, status.getMessageId());
    assertEquals("mailtype1", status.getType());
    status = results.next();
    assertEquals("Exception: error2", status.getErrorSummary());
    assertTrue(status.getErrorDescription().contains("error2"));
    assertEquals(batchId, status.getBatchId());
    assertEquals(UNIQUE_MESSAGE_ID2, status.getMessageId());
    assertEquals("mailtype2", status.getType());
    assertFalse(results.hasNext());
}
Also used : ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) MailStatus(org.xwiki.mail.MailStatus) Test(org.junit.Test)

Example 19 with ExtendedMimeMessage

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

the class GroupMimeMessageIterator method createMessageInternal.

@Override
protected ExtendedMimeMessage createMessageInternal() throws MessagingException {
    ExtendedMimeMessage mimeMessage;
    DocumentReference groupsClassReference = this.stringResolver.resolve(USER_SPACE + ".XWikiGroups");
    String userFullName = this.documentAccessBridge.getProperty(this.groupReference, groupsClassReference, this.position, "member").toString();
    DocumentReference userReference = this.stringResolver.resolve(userFullName);
    // If the user has no email address then return a null Mime Message so that it's skipped
    Object emailObject = this.documentAccessBridge.getProperty(userReference, new DocumentReference(userReference.getWikiReference().getName(), USER_SPACE, "XWikiUsers"), "email");
    if (emailObject != null) {
        String email = emailObject.toString();
        Map<String, Object> parameters = (Map<String, Object>) this.parameters.get("parameters");
        mimeMessage = ExtendedMimeMessage.wrap(this.factory.createMessage(this.parameters.get("source"), parameters));
        mimeMessage.addRecipients(Message.RecipientType.TO, email);
    } else {
        getLogger().warn("User [{}] has no email defined. Email has not been sent to that user.", userReference);
        mimeMessage = null;
    }
    return mimeMessage;
}
Also used : ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) Map(java.util.Map) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 20 with ExtendedMimeMessage

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

the class FileSystemMailContentStore method load.

@Override
public ExtendedMimeMessage load(Session session, String batchId, String uniqueMessageId) throws MailStoreException {
    File messageFile = null;
    try {
        messageFile = getMessageFile(batchId, uniqueMessageId);
        InputStream is = new FileInputStream(messageFile);
        return new ExtendedMimeMessage(session, is);
    } catch (Exception e) {
        throw new MailStoreException(String.format("Failed to load message (id [%s], batch id [%s]) from file [%s]", uniqueMessageId, batchId, messageFile), e);
    }
}
Also used : ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) MailStoreException(org.xwiki.mail.MailStoreException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) MailStoreException(org.xwiki.mail.MailStoreException) InitializationException(org.xwiki.component.phase.InitializationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

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