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);
}
}
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.");
}
}
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();
}
}
}
Aggregations