Search in sources :

Example 1 with LoggingConnectionListener

use of com.helger.smtp.transport.listener.LoggingConnectionListener 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)1 ClassPathResource (com.helger.commons.io.resource.ClassPathResource)1 IReadableResource (com.helger.commons.io.resource.IReadableResource)1 EmailData (com.helger.smtp.data.EmailData)1 IMutableEmailData (com.helger.smtp.data.IMutableEmailData)1 SMTPSettings (com.helger.smtp.settings.SMTPSettings)1 LoggingConnectionListener (com.helger.smtp.transport.listener.LoggingConnectionListener)1 LoggingTransportListener (com.helger.smtp.transport.listener.LoggingTransportListener)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1