Search in sources :

Example 1 with ISMTPSettings

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));
        }
    }
}
Also used : FailedMailData(com.helger.smtp.failed.FailedMailData) IMutableEmailData(com.helger.smtp.data.IMutableEmailData) ISMTPSettings(com.helger.smtp.settings.ISMTPSettings) ICommonsOrderedMap(com.helger.commons.collection.impl.ICommonsOrderedMap) Map(java.util.Map)

Example 2 with ISMTPSettings

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);
}
Also used : MockException(com.helger.commons.exception.mock.MockException) MailTransportError(com.helger.smtp.transport.MailTransportError) ISMTPSettings(com.helger.smtp.settings.ISMTPSettings) EmailData(com.helger.smtp.data.EmailData) Test(org.junit.Test)

Example 3 with ISMTPSettings

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);
}
Also used : MockException(com.helger.commons.exception.mock.MockException) MailTransportError(com.helger.smtp.transport.MailTransportError) ISMTPSettings(com.helger.smtp.settings.ISMTPSettings) Test(org.junit.Test)

Example 4 with ISMTPSettings

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);
}
Also used : LocalDateTime(java.time.LocalDateTime) ESMTPErrorCode(com.helger.smtp.transport.ESMTPErrorCode) MailSendDetails(com.helger.smtp.transport.MailSendDetails) IMutableEmailData(com.helger.smtp.data.IMutableEmailData) IMicroElement(com.helger.xml.microdom.IMicroElement) MailTransportError(com.helger.smtp.transport.MailTransportError) ISMTPSettings(com.helger.smtp.settings.ISMTPSettings) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) Nullable(javax.annotation.Nullable)

Example 5 with ISMTPSettings

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);
}
Also used : ISMTPSettings(com.helger.smtp.settings.ISMTPSettings) EmailAddress(com.helger.commons.email.EmailAddress) NamedSMTPSettings(com.helger.photon.core.smtp.NamedSMTPSettings) CNamedSMTPSettings(com.helger.photon.core.smtp.CNamedSMTPSettings)

Aggregations

ISMTPSettings (com.helger.smtp.settings.ISMTPSettings)8 MailTransportError (com.helger.smtp.transport.MailTransportError)3 Test (org.junit.Test)3 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)2 EmailAddress (com.helger.commons.email.EmailAddress)2 MockException (com.helger.commons.exception.mock.MockException)2 CNamedSMTPSettings (com.helger.photon.core.smtp.CNamedSMTPSettings)2 NamedSMTPSettings (com.helger.photon.core.smtp.NamedSMTPSettings)2 EmailData (com.helger.smtp.data.EmailData)2 IMutableEmailData (com.helger.smtp.data.IMutableEmailData)2 FailedMailData (com.helger.smtp.failed.FailedMailData)2 ICommonsOrderedMap (com.helger.commons.collection.impl.ICommonsOrderedMap)1 ESMTPErrorCode (com.helger.smtp.transport.ESMTPErrorCode)1 MailSendDetails (com.helger.smtp.transport.MailSendDetails)1 IMicroElement (com.helger.xml.microdom.IMicroElement)1 LocalDateTime (java.time.LocalDateTime)1 Map (java.util.Map)1 Nonnull (javax.annotation.Nonnull)1 Nullable (javax.annotation.Nullable)1