Search in sources :

Example 16 with MailListener

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

the class DatabaseMailListenerTest method onPrepareError.

@Test
public void onPrepareError() throws Exception {
    MailStatusStore mailStatusStore = this.mocker.getInstance(MailStatusStore.class, "database");
    MailListener listener = this.mocker.getComponentUnderTest();
    listener.onPrepareBegin(batchId, Collections.<String, Object>emptyMap());
    listener.onPrepareMessageError(this.message, new Exception("Error"), Collections.<String, Object>emptyMap());
    verify(mailStatusStore).save(argThat(new isSameMailStatus(MailState.PREPARE_ERROR, "mywiki")), anyMap());
}
Also used : MailListener(org.xwiki.mail.MailListener) MailStatusStore(org.xwiki.mail.MailStatusStore) MailStoreException(org.xwiki.mail.MailStoreException) Test(org.junit.Test)

Example 17 with MailListener

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

the class DatabaseMailListenerTest method onSuccessWhenMailContentDeletionFails.

@Test
public void onSuccessWhenMailContentDeletionFails() throws Exception {
    MailStatusStore mailStatusStore = this.mocker.getInstance(MailStatusStore.class, "database");
    MailStatus status = new MailStatus(this.batchId, this.message, MailState.PREPARE_SUCCESS);
    status.setWiki("otherwiki");
    when(mailStatusStore.load(this.messageId)).thenReturn(status);
    MailContentStore mailContentStore = this.mocker.getInstance(MailContentStore.class, "filesystem");
    doThrow(new MailStoreException("error")).when(mailContentStore).delete(this.batchId, this.messageId);
    MailListener listener = this.mocker.getComponentUnderTest();
    listener.onPrepareBegin(batchId, Collections.<String, Object>emptyMap());
    listener.onSendMessageSuccess(this.message, Collections.<String, Object>emptyMap());
    assertEquals("Failed to remove previously failing message [" + this.messageId + "] (batch id [" + this.batchId + "]) from the file system. Reason [MailStoreException: error].", this.logRule.getMessage(0));
}
Also used : MailListener(org.xwiki.mail.MailListener) MailStatusStore(org.xwiki.mail.MailStatusStore) MailStoreException(org.xwiki.mail.MailStoreException) MailContentStore(org.xwiki.mail.MailContentStore) MailStatus(org.xwiki.mail.MailStatus) Test(org.junit.Test)

Example 18 with MailListener

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

the class SendMailRunnable method sendMail.

/**
 * Send the mail.
 *
 * @param item the queue item containing all the data for sending the mail
 */
protected void sendMail(SendMailQueueItem item) {
    prepareContextForQueueItem(item);
    MailListener listener = item.getListener();
    ExtendedMimeMessage message;
    try {
        // Step 1: Load the message from the filesystem store
        message = this.mailContentStore.load(item.getSession(), item.getBatchId(), item.getUniqueMessageId());
    } catch (Exception e) {
        if (listener != null) {
            listener.onSendMessageFatalError(item.getUniqueMessageId(), e, Collections.<String, Object>emptyMap());
        }
        return;
    }
    try {
        // TODO: explain why!
        if (item.getSession() != this.currentSession || (this.count % 100) == 0) {
            closeTransport();
            this.currentSession = item.getSession();
            this.currentTransport = this.currentSession.getTransport("smtp");
            this.currentTransport.connect();
        } else if (!this.currentTransport.isConnected()) {
            this.currentTransport.connect();
        }
        // Step 3: Send the mail
        // Unlike the static send method, the sendMessage method does not call the saveChanges method on the
        // message; this prevent the MessageID header to be changed.
        this.currentTransport.sendMessage(message, message.getAllRecipients());
        this.count++;
        // Step 4: Notify the user of the success if a listener has been provided
        if (listener != null) {
            listener.onSendMessageSuccess(message, Collections.<String, Object>emptyMap());
        }
    } catch (Exception e) {
        // An error occurred, notify the user if a listener has been provided.
        if (listener != null) {
            listener.onSendMessageError(message, e, Collections.<String, Object>emptyMap());
        }
    }
}
Also used : MailListener(org.xwiki.mail.MailListener) ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) MessagingException(javax.mail.MessagingException) ExecutionContextException(org.xwiki.context.ExecutionContextException)

Example 19 with MailListener

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

the class NotificationEmailSender method sendEmails.

/**
 * Send notifications emails for specified users.
 * @param fromDate only send notifications about events that happened after this date
 * @param notificationUserIterator iterator for users interested in the notifications emails
 * @throws JobExecutionException if error happens
 */
public void sendEmails(Date fromDate, NotificationUserIterator notificationUserIterator) throws JobExecutionException {
    Map<String, Object> emailFactoryParameters = new HashMap<>();
    DocumentReference templateReference = new DocumentReference(wikiDescriptorManager.getCurrentWikiId(), Arrays.asList("XWiki", "Notifications"), "MailTemplate");
    PeriodicMimeMessageIterator periodicMimeMessageIterator = notificationMimeMessageIteratorProvider.get();
    periodicMimeMessageIterator.initialize(notificationUserIterator, emailFactoryParameters, fromDate, templateReference);
    Session session = this.sessionFactory.create(Collections.emptyMap());
    MailListener mailListener = mailListenerProvider.get();
    // Pass it to the message sender to send it asynchronously.
    mailSender.sendAsynchronously(periodicMimeMessageIterator, session, mailListener);
}
Also used : MailListener(org.xwiki.mail.MailListener) HashMap(java.util.HashMap) DocumentReference(org.xwiki.model.reference.DocumentReference) Session(javax.mail.Session)

Aggregations

MailListener (org.xwiki.mail.MailListener)19 Test (org.junit.Test)11 MailStatusStore (org.xwiki.mail.MailStatusStore)8 Session (javax.mail.Session)7 MimeMessage (javax.mail.internet.MimeMessage)6 MailStatus (org.xwiki.mail.MailStatus)6 MailStoreException (org.xwiki.mail.MailStoreException)6 ExtendedMimeMessage (org.xwiki.mail.ExtendedMimeMessage)4 MailContentStore (org.xwiki.mail.MailContentStore)4 MessagingException (javax.mail.MessagingException)3 InternetAddress (javax.mail.internet.InternetAddress)3 ExecutionContextException (org.xwiki.context.ExecutionContextException)3 DocumentReference (org.xwiki.model.reference.DocumentReference)3 ServerSetupTest (com.icegreen.greenmail.util.ServerSetupTest)2 HashMap (java.util.HashMap)2 MailSender (org.xwiki.mail.MailSender)2 MemoryMailListener (org.xwiki.mail.internal.MemoryMailListener)2 XWikiException (com.xpn.xwiki.XWikiException)1 ParseGroovyFromString (com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString)1 IncludeServletAsString (com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString)1