Search in sources :

Example 6 with IMutableEmailData

use of com.helger.smtp.data.IMutableEmailData 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

IMutableEmailData (com.helger.smtp.data.IMutableEmailData)6 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)3 EmailAddress (com.helger.commons.email.EmailAddress)2 ClassPathResource (com.helger.commons.io.resource.ClassPathResource)2 IReadableResource (com.helger.commons.io.resource.IReadableResource)2 EmailData (com.helger.smtp.data.EmailData)2 FailedMailData (com.helger.smtp.failed.FailedMailData)2 ISMTPSettings (com.helger.smtp.settings.ISMTPSettings)2 SMTPSettings (com.helger.smtp.settings.SMTPSettings)2 Ignore (org.junit.Ignore)2 Test (org.junit.Test)2 CommonsLinkedHashMap (com.helger.commons.collection.impl.CommonsLinkedHashMap)1 ICommonsList (com.helger.commons.collection.impl.ICommonsList)1 ICommonsOrderedMap (com.helger.commons.collection.impl.ICommonsOrderedMap)1 ICommonsOrderedSet (com.helger.commons.collection.impl.ICommonsOrderedSet)1 ICommonsSet (com.helger.commons.collection.impl.ICommonsSet)1 EmailDataTransportEvent (com.helger.smtp.listener.EmailDataTransportEvent)1 IEmailDataTransportListener (com.helger.smtp.listener.IEmailDataTransportListener)1 ESMTPErrorCode (com.helger.smtp.transport.ESMTPErrorCode)1 MailSendDetails (com.helger.smtp.transport.MailSendDetails)1