use of com.helger.smtp.failed.FailedMailData in project ph-web by phax.
the class MailAPI method queueMails.
/**
* Queue more than one mail.
*
* @param aSMTPSettings
* The SMTP settings to be used.
* @param aMailDataList
* The mail messages to queue. May not be <code>null</code>.
* @return The number of queued emails. Always ≥ 0. Maximum value is the
* number of {@link IMutableEmailData} objects in the argument.
*/
@Nonnegative
public static int queueMails(@Nonnull final ISMTPSettings aSMTPSettings, @Nonnull final Collection<? extends IMutableEmailData> aMailDataList) {
ValueEnforcer.notNull(aSMTPSettings, "SmtpSettings");
ValueEnforcer.notNull(aMailDataList, "MailDataList");
if (aMailDataList.isEmpty())
throw new IllegalArgumentException("At least one message has to be supplied!");
MailQueuePerSMTP aSMTPQueue;
RW_LOCK.writeLock().lock();
try {
// get queue per SMTP settings
try {
aSMTPQueue = _getOrCreateMailQueuePerSMTP(aSMTPSettings);
} catch (final IllegalStateException ex) {
// Happens if queue is already stopped
// Put all errors in failed mail queue
final MailTransportError aError = new MailTransportError(ex);
for (final IMutableEmailData aEmailData : aMailDataList) getFailedMailQueue().add(new FailedMailData(aSMTPSettings, aEmailData, aError));
return 0;
}
} finally {
RW_LOCK.writeLock().unlock();
}
int nQueuedMails = 0;
final boolean bSendVendorOnlyMails = GlobalDebug.isDebugMode();
// submit all messages
for (final IMutableEmailData aEmailData : aMailDataList) {
if (aEmailData == null) {
LOGGER.error("Mail data is null! Ignoring this item completely.");
continue;
}
// queue the mail
STATS_MAILS_QUEUED.increment();
// Do some consistency checks to ensure this particular email can be
// send
boolean bCanQueue = true;
if (aEmailData.getFrom() == null) {
if (LOGGER.isErrorEnabled())
LOGGER.error("Mail data has no sender address: " + aEmailData + " - not queuing!");
bCanQueue = false;
}
if (aEmailData.to().isEmpty()) {
if (LOGGER.isErrorEnabled())
LOGGER.error("Mail data has no receiver address: " + aEmailData + " - not queuing!");
bCanQueue = false;
}
if (bSendVendorOnlyMails) {
// In the debug version we can *only* send to vendor addresses!
if (hasNonVendorEmailAddress(aEmailData.to()) || hasNonVendorEmailAddress(aEmailData.cc()) || hasNonVendorEmailAddress(aEmailData.bcc())) {
if (LOGGER.isErrorEnabled())
LOGGER.error("Debug mode: ignoring mail TO '" + aEmailData.to() + "'" + (aEmailData.cc().isNotEmpty() ? " and CC '" + aEmailData.cc() + "'" : "") + (aEmailData.bcc().isNotEmpty() ? " and BCC '" + aEmailData.bcc() + "'" : "") + " because at least one address is not targeted to the vendor domain '" + VendorInfo.getVendorEmailSuffix() + "'");
bCanQueue = false;
}
}
// Check if queue is already stopped
if (aSMTPQueue.isStopped()) {
LOGGER.error("Queue is already stopped - not queuing!");
bCanQueue = false;
}
boolean bWasQueued = false;
Exception aException = null;
if (bCanQueue) {
// Check if a subject is present
if (StringHelper.hasNoText(aEmailData.getSubject())) {
if (LOGGER.isWarnEnabled())
LOGGER.warn("Mail data has no subject: " + aEmailData + " - defaulting to " + DEFAULT_SUBJECT);
aEmailData.setSubject(DEFAULT_SUBJECT);
}
// Check if a body is present
if (StringHelper.hasNoText(aEmailData.getBody()))
if (LOGGER.isWarnEnabled())
LOGGER.warn("Mail data has no body: " + aEmailData);
if (bSendVendorOnlyMails) {
// Add special debug prefix
if (!StringHelper.startsWith(aEmailData.getSubject(), DEBUG_SUBJECT_PREFIX))
aEmailData.setSubject(DEBUG_SUBJECT_PREFIX + aEmailData.getSubject());
if (LOGGER.isInfoEnabled())
LOGGER.info("Sending only-to-vendor mail in debug version:\n" + aSMTPSettings + "\n" + aEmailData);
}
// Uses UTC timezone!
aEmailData.setSentDateTime(PDTFactory.getCurrentLocalDateTime());
try {
if (aSMTPQueue.queueObject(aEmailData).isSuccess()) {
bWasQueued = true;
++nQueuedMails;
}
// else an error message was already logged
} catch (final Exception ex) {
// Occurs if queue is already stopped
aException = ex;
}
} else {
aException = new MailSendException("Email cannot be queued because internal constraints are not fulfilled!");
}
if (!bWasQueued) {
// Mail was not queued - put in failed mail queue
aSMTPQueue.getFailedMailQueue().add(new FailedMailData(aSMTPSettings, aEmailData, new MailTransportError(aException)));
}
}
return nQueuedMails;
}
use of com.helger.smtp.failed.FailedMailData in project ph-web by phax.
the class MailQueuePerSMTP method runAsync.
/**
* This is the callback to be invoked every time something is in the queue.
*
* @param aMessages
* The non-null and non-empty list of messages to be send
*/
public void runAsync(@Nullable final List<IMutableEmailData> aMessages) {
// Expect the worst
if (CollectionHelper.isNotEmpty(aMessages)) {
final ISMTPSettings aSettings = m_aTransport.getSMTPSettings();
try {
final int nMessages = aMessages.size();
if (LOGGER.isInfoEnabled())
LOGGER.info("Sending " + nMessages + " mail message" + (nMessages == 1 ? "" : "s") + "!");
// send messages
final ICommonsOrderedMap<IMutableEmailData, MailTransportError> aFailedMessages = m_aTransport.send(aMessages);
// handle failed messages
for (final Map.Entry<IMutableEmailData, MailTransportError> aEntry : aFailedMessages.entrySet()) m_aFailedMailQueue.add(new FailedMailData(aSettings, aEntry.getKey(), aEntry.getValue()));
} catch (final Exception ex) {
// No message specific error, but a settings specific error
if (LOGGER.isErrorEnabled())
LOGGER.error("Generic error sending mail: " + ex.getMessage(), ex.getCause());
// mark all mails as failed even though some may have been re-send
// already
final MailTransportError aError = new MailTransportError(ex);
for (final IMutableEmailData aMessage : aMessages) m_aFailedMailQueue.add(new FailedMailData(aSettings, aMessage, aError));
}
}
}
use of com.helger.smtp.failed.FailedMailData in project ph-web by phax.
the class MailQueuePerSMTP method stopQueuingNewObjects.
/**
* Stop this queue
*
* @param bStopImmediately
* <code>true</code> if all mails currently in the queue should be
* removed and put in the failed mail queue. Only the emails currently
* in sending are continued to be sent out.
* @return {@link ESuccess}
*/
@Nonnull
public ESuccess stopQueuingNewObjects(final boolean bStopImmediately) {
if (bStopImmediately) {
// Remove all mails from the queue and put it in the failed mail queue
final ICommonsList<Object> aLeftOvers = new CommonsArrayList<>();
m_aQueue.drainTo(aLeftOvers);
if (!aLeftOvers.isEmpty()) {
final ISMTPSettings aSMTPSettings = getSMTPSettings();
for (final Object aLeftOver : aLeftOvers) m_aFailedMailQueue.add(new FailedMailData(aSMTPSettings, (IMutableEmailData) aLeftOver));
if (LOGGER.isInfoEnabled())
LOGGER.info("Put " + aLeftOvers + " unsent mails into the failed mail queue because of immediate stop.");
}
}
// Regular stop
return super.stopQueuingNewObjects();
}
Aggregations