Search in sources :

Example 21 with ExtendedMimeMessage

use of org.xwiki.mail.ExtendedMimeMessage 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)

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