Search in sources :

Example 1 with EmailSender

use of com.serotonin.web.mail.EmailSender in project ma-core-public by infiniteautomation.

the class EmailWorkItem method execute.

@Override
public void execute() {
    Exception failedEx = null;
    boolean success = true;
    try {
        if (fromAddress == null) {
            String addr = SystemSettingsDao.getValue(SystemSettingsDao.EMAIL_FROM_ADDRESS);
            String pretty = SystemSettingsDao.getValue(SystemSettingsDao.EMAIL_FROM_NAME);
            fromAddress = new InternetAddress(addr, pretty);
        }
        EmailSender emailSender = new EmailSender(SystemSettingsDao.getValue(SystemSettingsDao.EMAIL_SMTP_HOST), SystemSettingsDao.getIntValue(SystemSettingsDao.EMAIL_SMTP_PORT), SystemSettingsDao.getBooleanValue(SystemSettingsDao.EMAIL_AUTHORIZATION), SystemSettingsDao.getValue(SystemSettingsDao.EMAIL_SMTP_USERNAME), SystemSettingsDao.getValue(SystemSettingsDao.EMAIL_SMTP_PASSWORD), SystemSettingsDao.getBooleanValue(SystemSettingsDao.EMAIL_TLS));
        emailSender.send(fromAddress, toAddresses, subject, content);
    } catch (Exception e) {
        LOG.warn("Error sending email", e);
        failedEx = e;
        success = false;
        String to = "";
        for (InternetAddress addr : toAddresses) {
            if (to.length() > 0)
                to += ", ";
            to += addr.getAddress();
        }
        SystemEventType.raiseEvent(new SystemEventType(SystemEventType.TYPE_EMAIL_SEND_FAILURE), Common.timer.currentTimeMillis(), false, new TranslatableMessage("event.email.failure", subject, to, e.getMessage()));
    } finally {
        if (postSendExecution != null) {
            for (PostEmailRunnable runnable : postSendExecution) runnable.emailFinished(success, failedEx);
        }
    }
}
Also used : PostEmailRunnable(com.serotonin.m2m2.email.PostEmailRunnable) SystemEventType(com.serotonin.m2m2.rt.event.type.SystemEventType) InternetAddress(javax.mail.internet.InternetAddress) EmailSender(com.serotonin.web.mail.EmailSender) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) AddressException(javax.mail.internet.AddressException)

Aggregations

PostEmailRunnable (com.serotonin.m2m2.email.PostEmailRunnable)1 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)1 SystemEventType (com.serotonin.m2m2.rt.event.type.SystemEventType)1 EmailSender (com.serotonin.web.mail.EmailSender)1 AddressException (javax.mail.internet.AddressException)1 InternetAddress (javax.mail.internet.InternetAddress)1