Search in sources :

Example 1 with MailParseException

use of com.manydesigns.mail.queue.MailParseException in project Portofino by ManyDesigns.

the class DefaultMailSender method runOnce.

public int runOnce(Set<String> idsToMarkAsSent) {
    List<String> ids;
    try {
        ids = queue.getEnqueuedEmailIds();
    } catch (Throwable e) {
        logger.error("Couldn't read email queue", e);
        return -1;
    }
    int serverErrors = 0;
    for (String id : ids) {
        if (idsToMarkAsSent.contains(id)) {
            logger.info("Mail with id {} already sent but mark failed, retrying", id);
            try {
                queue.markSent(id);
                idsToMarkAsSent.remove(id);
            } catch (Throwable e) {
                logger.error("Couldn't mark mail as sent", e);
            }
            continue;
        }
        Email email;
        try {
            email = queue.loadEmail(id);
        } catch (MailParseException e) {
            logger.error("Mail with id " + id + " is corrupted, marking as failed", e);
            markFailed(id, e);
            continue;
        } catch (Throwable e) {
            logger.error("Unexpected error loading mail with id " + id + ", skipping", e);
            continue;
        }
        if (email != null) {
            boolean sent = false;
            try {
                logger.info("Sending email with id {}", id);
                send(email);
                sent = true;
            } catch (EmailException e) {
                Throwable cause = e.getCause();
                if (cause instanceof ParseException || cause instanceof IllegalWriteException || cause instanceof MethodNotSupportedException) {
                    markFailed(id, cause);
                } else if (cause instanceof MessagingException) {
                    if (e.getCause() instanceof SendFailedException && e.getCause().getCause() instanceof SMTPAddressFailedException) {
                        logger.warn("Mail not sent due to known server error, marking as failed");
                        markFailed(id, e);
                    } else {
                        logger.warn("Mail not sent due to known server error, NOT marking as failed", e);
                        serverErrors++;
                    }
                } else {
                    markFailed(id, e);
                }
            } catch (Throwable e) {
                markFailed(id, e);
            }
            if (sent)
                try {
                    logger.info("Email with id {} sent, marking as sent ", id);
                    queue.markSent(id);
                } catch (Throwable e) {
                    logger.error("Couldn't mark mail as sent", e);
                    idsToMarkAsSent.add(id);
                }
        }
    }
    return serverErrors;
}
Also used : SendFailedException(javax.mail.SendFailedException) Email(com.manydesigns.mail.queue.model.Email) MessagingException(javax.mail.MessagingException) SMTPAddressFailedException(com.sun.mail.smtp.SMTPAddressFailedException) IllegalWriteException(javax.mail.IllegalWriteException) MethodNotSupportedException(javax.mail.MethodNotSupportedException) MailParseException(com.manydesigns.mail.queue.MailParseException) ParseException(javax.mail.internet.ParseException) MailParseException(com.manydesigns.mail.queue.MailParseException)

Aggregations

MailParseException (com.manydesigns.mail.queue.MailParseException)1 Email (com.manydesigns.mail.queue.model.Email)1 SMTPAddressFailedException (com.sun.mail.smtp.SMTPAddressFailedException)1 IllegalWriteException (javax.mail.IllegalWriteException)1 MessagingException (javax.mail.MessagingException)1 MethodNotSupportedException (javax.mail.MethodNotSupportedException)1 SendFailedException (javax.mail.SendFailedException)1 ParseException (javax.mail.internet.ParseException)1