use of com.helger.smtp.settings.ISMTPSettings 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.settings.ISMTPSettings in project ph-web by phax.
the class FailedMailDataTest method testWithExceptionAndData.
@Test
public void testWithExceptionAndData() {
final ISMTPSettings aSettings = _createSMTPSettings();
final EmailData aData = _createEmailData();
final Throwable aError = new MockException("Test error");
final FailedMailData aFMD = new FailedMailData(aSettings, aData, new MailTransportError(aError));
assertNotNull(aFMD.getID());
assertNotNull(aFMD.getErrorDateTime());
assertEquals(aSettings, aFMD.getSMTPSettings());
assertEquals(aData.getSentDateTime(), aFMD.getOriginalSentDateTime());
assertEquals(aData, aFMD.getEmailData());
assertTrue(aFMD.hasTransportError());
assertNotNull(aFMD.getTransportThrowableMessage());
assertNotNull(aFMD.getTransportError());
XMLTestHelper.testMicroTypeConversion(aFMD);
}
use of com.helger.smtp.settings.ISMTPSettings in project ph-web by phax.
the class FailedMailDataTest method testWithException.
@Test
public void testWithException() {
final ISMTPSettings aSettings = _createSMTPSettings();
final Throwable aError = new MockException("Test error");
final FailedMailData aFMD = new FailedMailData(aSettings, new MailTransportError(aError));
assertNotNull(aFMD.getID());
assertNotNull(aFMD.getErrorDateTime());
assertEquals(aSettings, aFMD.getSMTPSettings());
assertNull(aFMD.getOriginalSentDateTime());
assertNull(aFMD.getEmailData());
assertTrue(aFMD.hasTransportError());
assertNotNull(aFMD.getTransportThrowableMessage());
assertNotNull(aFMD.getTransportError());
XMLTestHelper.testMicroTypeConversion(aFMD);
}
use of com.helger.smtp.settings.ISMTPSettings in project ph-web by phax.
the class FailedMailDataMicroTypeConverter method convertToNative.
@Nullable
public FailedMailData convertToNative(@Nonnull final IMicroElement eFailedMail) {
final String sID = eFailedMail.getAttributeValue(ATTR_ID);
if (sID == null) {
LOGGER.error("Failed to read ID");
return null;
}
// Read error date/time
final String sErrorDT = eFailedMail.getAttributeValue(ATTR_ERRORDT);
if (sErrorDT == null) {
LOGGER.error("Failed to read error date/time");
return null;
}
LocalDateTime aErrorDT = PDTWebDateHelper.getLocalDateTimeFromXSD(sErrorDT);
if (aErrorDT == null)
aErrorDT = TypeConverter.convert(sErrorDT, LocalDateTime.class);
if (aErrorDT == null) {
if (LOGGER.isErrorEnabled())
LOGGER.error("Failed to parse error date '" + sErrorDT + "'");
return null;
}
// read original sent date/time
final String sOriginalSentDT = eFailedMail.getAttributeValue(ATTR_ORIGINALSENT_DT);
LocalDateTime aOriginalSentDT = null;
if (sOriginalSentDT != null) {
aOriginalSentDT = PDTWebDateHelper.getLocalDateTimeFromXSD(sOriginalSentDT);
if (aOriginalSentDT == null)
aOriginalSentDT = TypeConverter.convert(sOriginalSentDT, LocalDateTime.class);
}
// SMTP settings
final IMicroElement eSMTPSettings = eFailedMail.getFirstChildElement(ELEMENT_SMTP_SETTINGS);
if (eSMTPSettings == null) {
LOGGER.error("Failed to get child element of SMTP settings!");
return null;
}
final ISMTPSettings aSMTPSettings = MicroTypeConverter.convertToNative(eSMTPSettings, SMTPSettings.class);
// email data (may be null)
final IMicroElement eEmailData = eFailedMail.getFirstChildElement(ELEMENT_EMAIL_DATA);
final IMutableEmailData aEmailData = MicroTypeConverter.convertToNative(eEmailData, EmailData.class);
// error message
final String sErrorMessage = MicroHelper.getChildTextContent(eFailedMail, ELEMENT_ERROR_MSG);
final Exception aException = StringHelper.hasNoText(sErrorMessage) ? null : new Exception(sErrorMessage);
MailTransportError aError = null;
if (aException != null) {
final ICommonsList<MailSendDetails> aDetails = new CommonsArrayList<>();
for (final IMicroElement eDetails : eFailedMail.getAllChildElements(ELEMENT_DETAILS)) {
final boolean bAddressValid = StringParser.parseBool(eDetails.getAttributeValue(ATTR_ADDRESS_VALID));
final String sAddress = eDetails.getAttributeValue(ATTR_ADDRESS);
final String sCommand = eDetails.getAttributeValue(ATTR_COMMAND);
final String sDetailsErrorMessage = eDetails.getAttributeValue(ATTR_ERROR_MESSAGE);
final int nErrorCode = StringParser.parseInt(eDetails.getAttributeValue(ATTR_ERROR_CODE), -1);
final ESMTPErrorCode eErrorCode = ESMTPErrorCode.getFromIDOrDefault(nErrorCode, ESMTPErrorCode.FALLBACK);
aDetails.add(new MailSendDetails(bAddressValid, sAddress, sCommand, sDetailsErrorMessage, eErrorCode));
}
aError = new MailTransportError(aException, aDetails);
}
return new FailedMailData(sID, aErrorDT, aSMTPSettings, aOriginalSentDT, aEmailData, aError);
}
use of com.helger.smtp.settings.ISMTPSettings in project peppol-practical by phax.
the class AppInternalErrorHandler method doSetup.
public static void doSetup() {
// Set global internal error handlers
new AppInternalErrorHandler().install();
final NamedSMTPSettings aNamedSettings = PhotonCoreManager.getSMTPSettingsMgr().getSettings(CNamedSMTPSettings.NAMED_SMTP_SETTINGS_DEFAULT_ID);
final ISMTPSettings aSMTPSettings = aNamedSettings == null ? null : aNamedSettings.getSMTPSettings();
InternalErrorSettings.setSMTPSenderAddress(new EmailAddress("peppol@helger.com", "peppol.helger.com application"));
InternalErrorSettings.setSMTPReceiverAddress(new EmailAddress("philip@helger.com", "Philip"));
InternalErrorSettings.setSMTPSettings(aSMTPSettings);
InternalErrorSettings.setFallbackLocale(CPPApp.DEFAULT_LOCALE);
}
Aggregations