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;
}
Aggregations