Search in sources :

Example 1 with SMTPSettings

use of com.helger.smtp.settings.SMTPSettings in project ph-web by phax.

the class MailAPITest method testStopImmediately.

@Ignore("to avoid spamming my mailbox")
@Test
public void testStopImmediately() {
    /*
     * This file might not be present, as it contains the real-life SMTP
     * settings. It should reside in src/test/resource and is SVN ignored by
     * name
     */
    final IReadableResource aRes = new ClassPathResource("smtp-settings.xml");
    if (aRes.exists()) {
        final SMTPSettings aSMTPSettings = MicroTypeConverter.convertToNative(MicroReader.readMicroXML(aRes).getDocumentElement(), SMTPSettings.class);
        final IMutableEmailData aMailData = new EmailData(EEmailType.TEXT);
        aMailData.to().add(new EmailAddress("ph@helger.com"));
        aMailData.setFrom(new EmailAddress("auto@helger.com"));
        aMailData.setSubject("JÜnit test with späcial käräktärs");
        aMailData.setBody("Hi there\nLine 2\n4 special chars: äöüß\n123456789\nBest regards: ph-smtp");
        final ICommonsList<IMutableEmailData> aMails = new CommonsArrayList<>();
        for (int i = 0; i < 10; ++i) aMails.add(aMailData);
        MailAPI.queueMails(aSMTPSettings, aMails);
        ThreadHelper.sleep(20);
        // Stop immediately
        MailAPI.stop(true);
    }
}
Also used : SMTPSettings(com.helger.smtp.settings.SMTPSettings) IReadableResource(com.helger.commons.io.resource.IReadableResource) IMutableEmailData(com.helger.smtp.data.IMutableEmailData) EmailData(com.helger.smtp.data.EmailData) IMutableEmailData(com.helger.smtp.data.IMutableEmailData) ClassPathResource(com.helger.commons.io.resource.ClassPathResource) EmailAddress(com.helger.commons.email.EmailAddress) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with SMTPSettings

use of com.helger.smtp.settings.SMTPSettings in project phoss-smp by phax.

the class SMPInternalErrorHandler method doSetup.

public static void doSetup() {
    final ConfigFile aCF = SMPServerConfiguration.getConfigFile();
    final String sSenderAddress = aCF.getAsString("smp.errorhandler.sender.email");
    final String sSenderName = aCF.getAsString("smp.errorhandler.sender.name", "SMP Internal Error Sender");
    final String sReceiverAddress = aCF.getAsString("smp.errorhandler.receiver.email");
    final String sReceiverName = aCF.getAsString("smp.errorhandler.receiver.name");
    final String sSMTPHostName = aCF.getAsString("smp.smtp.hostname");
    final int nSMTPPort = aCF.getAsInt("smp.smtp.port", -1);
    final String sSMTPUserName = aCF.getAsString("smp.smtp.username");
    final String sSMTPPassword = aCF.getAsString("smp.smtp.password");
    final boolean bSMTPSSLEnabled = aCF.getAsBoolean("smp.smtp.ssl", false);
    final boolean bSMTPSTARTTLSEnabled = aCF.getAsBoolean("smp.smtp.starttls", false);
    final long nSMTPConnectionTimeoutMS = aCF.getAsLong("smp.smtp.connectiontimeoutms", 10_000);
    final long nSMTPSocketTimeoutMS = aCF.getAsLong("smp.smtp.sockettimeoutms", 10_000);
    final boolean bSMTPDebug = aCF.getAsBoolean("smp.smtp.debug", false);
    final SMTPSettings aSMTPSettings = StringHelper.hasText(sSMTPHostName) ? new SMTPSettings(sSMTPHostName, nSMTPPort, sSMTPUserName, sSMTPPassword, StandardCharsets.UTF_8, bSMTPSSLEnabled, bSMTPSTARTTLSEnabled, nSMTPConnectionTimeoutMS, nSMTPSocketTimeoutMS, bSMTPDebug) : null;
    if (StringHelper.hasText(sSenderAddress) && StringHelper.hasText(sReceiverAddress) && aSMTPSettings != null && aSMTPSettings.areRequiredFieldsSet()) {
        // Set global internal error handlers
        new SMPInternalErrorHandler().install();
        InternalErrorSettings.setSMTPSenderAddress(new EmailAddress(sSenderAddress, sSenderName));
        InternalErrorSettings.setSMTPReceiverAddresses(new EmailAddress(sReceiverAddress, sReceiverName));
        InternalErrorSettings.setSMTPSettings(aSMTPSettings);
        InternalErrorSettings.setFallbackLocale(CSMPServer.DEFAULT_LOCALE);
        if (LOGGER.isInfoEnabled())
            LOGGER.info("Setup internal error handler to send emails on internal errors to " + sReceiverAddress);
    } else {
        LOGGER.info("No internal error handler configuration was found. So not sending emails in case of error.");
    }
}
Also used : SMTPSettings(com.helger.smtp.settings.SMTPSettings) ConfigFile(com.helger.settings.exchange.configfile.ConfigFile) EmailAddress(com.helger.commons.email.EmailAddress)

Example 3 with SMTPSettings

use of com.helger.smtp.settings.SMTPSettings in project ph-web by phax.

the class MailAPITest method testBasic.

@Ignore("to avoid spamming my mailbox")
@Test
public void testBasic() {
    /*
     * This file might not be present, as it contains the real-life SMTP
     * settings. It should reside in src/test/resource and is SVN ignored by
     * name
     */
    final IReadableResource aRes = new ClassPathResource("smtp-settings.xml");
    if (aRes.exists()) {
        final boolean bOldIsDebug = GlobalDebug.isDebugMode();
        GlobalDebug.setDebugModeDirect(true);
        try {
            EmailGlobalSettings.enableJavaxMailDebugging(GlobalDebug.isDebugMode());
            // Setup debug listeners
            EmailGlobalSettings.addConnectionListener(new LoggingConnectionListener());
            EmailGlobalSettings.addConnectionListener(new LoggingConnectionListener(EErrorLevel.WARN));
            EmailGlobalSettings.addEmailDataTransportListener(new LoggingTransportListener());
            EmailGlobalSettings.addEmailDataTransportListener(new LoggingTransportListener(EErrorLevel.WARN));
            final SMTPSettings aSMTPSettings = MicroTypeConverter.convertToNative(MicroReader.readMicroXML(aRes).getDocumentElement(), SMTPSettings.class);
            final IMutableEmailData aMailData = new EmailData(EEmailType.TEXT);
            aMailData.to().add(new EmailAddress("ph@helger.com"));
            aMailData.cc().add(new EmailAddress("hudri@helger.com"));
            aMailData.setFrom(new EmailAddress("auto@helger.com"));
            aMailData.setSubject("JÜnit test with späcial käräktärs");
            aMailData.setBody("Hi there\nLine 2\n4 special chars: äöüß\n123456789\nBest regards: ph-smtp");
            MailAPI.queueMail(aSMTPSettings, aMailData);
            MailAPI.stop();
            /*
         * try to queue again after MailAPI was stopped - should end up in
         * failed mail queue
         */
            assertEquals(0, MailAPI.getFailedMailQueue().size());
            MailAPI.queueMail(aSMTPSettings, aMailData);
            assertEquals(1, MailAPI.getFailedMailQueue().size());
        } finally {
            GlobalDebug.setDebugModeDirect(bOldIsDebug);
            EmailGlobalSettings.setToDefault();
        }
    }
}
Also used : SMTPSettings(com.helger.smtp.settings.SMTPSettings) LoggingConnectionListener(com.helger.smtp.transport.listener.LoggingConnectionListener) IReadableResource(com.helger.commons.io.resource.IReadableResource) LoggingTransportListener(com.helger.smtp.transport.listener.LoggingTransportListener) IMutableEmailData(com.helger.smtp.data.IMutableEmailData) EmailData(com.helger.smtp.data.EmailData) IMutableEmailData(com.helger.smtp.data.IMutableEmailData) ClassPathResource(com.helger.commons.io.resource.ClassPathResource) EmailAddress(com.helger.commons.email.EmailAddress) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

EmailAddress (com.helger.commons.email.EmailAddress)3 SMTPSettings (com.helger.smtp.settings.SMTPSettings)3 ClassPathResource (com.helger.commons.io.resource.ClassPathResource)2 IReadableResource (com.helger.commons.io.resource.IReadableResource)2 EmailData (com.helger.smtp.data.EmailData)2 IMutableEmailData (com.helger.smtp.data.IMutableEmailData)2 Ignore (org.junit.Ignore)2 Test (org.junit.Test)2 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)1 ConfigFile (com.helger.settings.exchange.configfile.ConfigFile)1 LoggingConnectionListener (com.helger.smtp.transport.listener.LoggingConnectionListener)1 LoggingTransportListener (com.helger.smtp.transport.listener.LoggingTransportListener)1