Search in sources :

Example 1 with SendmailProtocol

use of org.opennms.netmgt.config.javamail.SendmailProtocol in project opennms by OpenNMS.

the class JavaSendMailer method send.

/**
 * Send.
 *
 * @throws JavaMailerException the java mailer exception
 */
public void send() throws JavaMailerException {
    if (m_config.getSendmailProtocol() == null || m_config.getSendmailMessage() == null) {
        throw new JavaMailerException("sendmail-protocol or sendmail-message are not configured!");
    }
    try {
        final SendmailProtocol sendmailProtocol = m_config.getSendmailProtocol();
        final String body = m_config.getSendmailMessage().getBody();
        if ("text/plain".equals(sendmailProtocol.getMessageContentType().toLowerCase())) {
            m_message.setText(body);
        } else {
            m_message.setContent(body, sendmailProtocol.getMessageContentType());
        }
    } catch (final MessagingException e) {
        LOG.error("Java Mailer messaging exception: {}", e, e);
        throw new JavaMailerException("Java Mailer messaging exception: " + e, e);
    }
    send(m_message);
}
Also used : SendmailProtocol(org.opennms.netmgt.config.javamail.SendmailProtocol) MessagingException(javax.mail.MessagingException)

Example 2 with SendmailProtocol

use of org.opennms.netmgt.config.javamail.SendmailProtocol in project opennms by OpenNMS.

the class JavaSendMailer method send.

/**
 * Send.
 *
 * @param message the message
 * @throws JavaMailerException the java mailer exception
 */
public void send(MimeMessage message) throws JavaMailerException {
    Transport t = null;
    if (m_config.getSendmailProtocol() == null || m_config.getSendmailHost() == null) {
        throw new JavaMailerException("sendmail-protocol or sendmail-host are not configured!");
    }
    try {
        SendmailProtocol protoConfig = m_config.getSendmailProtocol();
        t = m_session.getTransport(protoConfig.getTransport());
        LOG.debug("for transport name '{}' got: {}@{}", protoConfig.getTransport(), t.getClass().getName(), Integer.toHexString(t.hashCode()));
        LoggingTransportListener listener = new LoggingTransportListener();
        t.addTransportListener(listener);
        if ("mta".equals(t.getURLName().getProtocol())) {
            // JMTA throws an AuthenticationFailedException if we call connect()
            LOG.debug("transport is 'mta', not trying to connect()");
        } else {
            final SendmailHost sendmailHost = m_config.getSendmailHost();
            if (m_config.isUseAuthentication() && m_config.getUserAuth() != null) {
                LOG.debug("authenticating to {}", sendmailHost.getHost());
                final UserAuth userAuth = m_config.getUserAuth();
                t.connect(sendmailHost.getHost(), sendmailHost.getPort(), userAuth.getUserName(), userAuth.getPassword());
            } else {
                LOG.debug("not authenticating to {}", sendmailHost.getHost());
                t.connect(sendmailHost.getHost(), sendmailHost.getPort(), null, null);
            }
        }
        t.sendMessage(message, message.getAllRecipients());
        listener.assertAllMessagesDelivered();
    } catch (NoSuchProviderException e) {
        LOG.error("Couldn't get a transport: {}", e, e);
        throw new JavaMailerException("Couldn't get a transport: " + e, e);
    } catch (MessagingException e) {
        LOG.error("Java Mailer messaging exception: {}", e, e);
        throw new JavaMailerException("Java Mailer messaging exception: " + e, e);
    } finally {
        try {
            if (t != null && t.isConnected()) {
                t.close();
            }
        } catch (MessagingException e) {
            throw new JavaMailerException("Java Mailer messaging exception on transport close: " + e, e);
        }
    }
}
Also used : SendmailProtocol(org.opennms.netmgt.config.javamail.SendmailProtocol) MessagingException(javax.mail.MessagingException) Transport(javax.mail.Transport) UserAuth(org.opennms.netmgt.config.javamail.UserAuth) NoSuchProviderException(javax.mail.NoSuchProviderException) SendmailHost(org.opennms.netmgt.config.javamail.SendmailHost)

Example 3 with SendmailProtocol

use of org.opennms.netmgt.config.javamail.SendmailProtocol in project opennms by OpenNMS.

the class JavaReadMailerTest method createSendMailer.

private JavaSendMailer createSendMailer(String gmailAccount, String gmailPassword) throws JavaMailerException {
    SendmailConfig config = new SendmailConfig();
    config.setAttemptInterval(1000l);
    config.setDebug(true);
    config.setName("test");
    SendmailMessage sendmailMessage = new SendmailMessage();
    sendmailMessage.setBody("service is down");
    sendmailMessage.setFrom("bamboo.opennms@gmail.com");
    sendmailMessage.setSubject("Notice #1234: service down");
    sendmailMessage.setTo("bamboo.opennms@gmail.com");
    config.setSendmailMessage(sendmailMessage);
    SendmailHost host = new SendmailHost();
    host.setHost("smtp.gmail.com");
    host.setPort(465);
    config.setSendmailHost(host);
    SendmailProtocol protocol = new SendmailProtocol();
    protocol.setSslEnable(true);
    protocol.setTransport("smtps");
    config.setSendmailProtocol(protocol);
    config.setUseAuthentication(true);
    config.setUseJmta(false);
    UserAuth auth = new UserAuth();
    auth.setUserName(gmailAccount);
    auth.setPassword(gmailPassword);
    config.setUserAuth(auth);
    return new JavaSendMailer(config);
}
Also used : SendmailProtocol(org.opennms.netmgt.config.javamail.SendmailProtocol) SendmailMessage(org.opennms.netmgt.config.javamail.SendmailMessage) SendmailConfig(org.opennms.netmgt.config.javamail.SendmailConfig) UserAuth(org.opennms.netmgt.config.javamail.UserAuth) SendmailHost(org.opennms.netmgt.config.javamail.SendmailHost)

Example 4 with SendmailProtocol

use of org.opennms.netmgt.config.javamail.SendmailProtocol in project opennms by OpenNMS.

the class JavaMailAckReaderIT method createSendMailer.

private JavaSendMailer createSendMailer(String gmailAccount, String gmailPassword) throws JavaMailerException {
    SendmailConfig config = new SendmailConfig();
    config.setAttemptInterval(1000l);
    config.setDebug(true);
    config.setName("test");
    SendmailMessage sendmailMessage = new SendmailMessage();
    sendmailMessage.setBody("service is down");
    sendmailMessage.setFrom("bamboo.opennms@gmail.com");
    sendmailMessage.setSubject("Notice #1234: service down");
    sendmailMessage.setTo("bamboo.opennms@gmail.com");
    config.setSendmailMessage(sendmailMessage);
    SendmailHost host = new SendmailHost();
    host.setHost("smtp.gmail.com");
    host.setPort(465);
    config.setSendmailHost(host);
    SendmailProtocol protocol = new SendmailProtocol();
    protocol.setSslEnable(true);
    protocol.setTransport("smtps");
    config.setSendmailProtocol(protocol);
    config.setUseAuthentication(true);
    config.setUseJmta(false);
    UserAuth auth = new UserAuth();
    auth.setUserName(gmailAccount);
    auth.setPassword(gmailPassword);
    config.setUserAuth(auth);
    return new JavaSendMailer(config);
}
Also used : SendmailProtocol(org.opennms.netmgt.config.javamail.SendmailProtocol) SendmailMessage(org.opennms.netmgt.config.javamail.SendmailMessage) JavaSendMailer(org.opennms.javamail.JavaSendMailer) SendmailConfig(org.opennms.netmgt.config.javamail.SendmailConfig) UserAuth(org.opennms.netmgt.config.javamail.UserAuth) SendmailHost(org.opennms.netmgt.config.javamail.SendmailHost)

Example 5 with SendmailProtocol

use of org.opennms.netmgt.config.javamail.SendmailProtocol in project opennms by OpenNMS.

the class JavaMailDeliveryService method deliverReport.

/* (non-Javadoc)
     * @see org.opennms.netmgt.reporting.service.ReportDeliveryService#deliverReport(org.opennms.netmgt.config.reportd.Report, java.lang.String)
     */
@Override
public void deliverReport(Report report, String fileName) throws ReportDeliveryException {
    try {
        SendmailConfig config = null;
        if (report.getMailer().isPresent()) {
            final String mailer = report.getMailer().get();
            LOG.debug("deliverReport with mailer={}", mailer);
            config = m_JavamailConfigDao.getSendMailConfig(mailer);
        } else {
            LOG.debug("deliverReport with default sendmail config");
            config = m_JavamailConfigDao.getDefaultSendmailConfig();
        }
        JavaSendMailer sm = new JavaSendMailer(config);
        MimeMessage msg = new MimeMessage(sm.getSession());
        if (config.getSendmailMessage() != null && config.getSendmailProtocol() != null) {
            final SendmailMessage sendmailMessage = config.getSendmailMessage();
            final SendmailProtocol sendmailProtocol = config.getSendmailProtocol();
            MimeMessageHelper helper = new MimeMessageHelper(msg, true, sendmailProtocol.getCharSet());
            helper.setFrom(sendmailMessage.getFrom());
            helper.setTo(report.getRecipients().toArray(new String[0]));
            helper.setSubject("OpenNMS Report: " + report.getReportName());
            if ("text/html".equals(sendmailProtocol.getMessageContentType().toLowerCase())) {
                helper.setText(sendmailMessage.getBody().replaceAll("\\<[^>]*>", ""), sendmailMessage.getBody());
            } else {
                helper.setText(sendmailMessage.getBody());
            }
            helper.addAttachment(fileName, new File(fileName));
            sm.send(msg);
        } else {
            LOG.error("sendmail-message or sendmail-protocol is not configured!");
        }
    } catch (JavaMailerException e) {
        LOG.error("Problem with JavaMailer {}", e.getMessage(), e);
        throw new ReportDeliveryException("Caught JavaMailerException: " + e.getMessage());
    } catch (MessagingException e) {
        LOG.error("Problem with Messaging {}", e.getMessage(), e);
        throw new ReportDeliveryException("Caught MessagingException: " + e.getMessage());
    } catch (Throwable e) {
        LOG.error("Unexpected exception: {}", e.getMessage(), e);
        throw new ReportDeliveryException("Caught unexpected " + e.getClass().getName() + ": " + e.getMessage());
    }
}
Also used : SendmailProtocol(org.opennms.netmgt.config.javamail.SendmailProtocol) MimeMessage(javax.mail.internet.MimeMessage) SendmailMessage(org.opennms.netmgt.config.javamail.SendmailMessage) MessagingException(javax.mail.MessagingException) JavaSendMailer(org.opennms.javamail.JavaSendMailer) SendmailConfig(org.opennms.netmgt.config.javamail.SendmailConfig) JavaMailerException(org.opennms.javamail.JavaMailerException) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) File(java.io.File)

Aggregations

SendmailProtocol (org.opennms.netmgt.config.javamail.SendmailProtocol)6 SendmailHost (org.opennms.netmgt.config.javamail.SendmailHost)4 SendmailMessage (org.opennms.netmgt.config.javamail.SendmailMessage)4 UserAuth (org.opennms.netmgt.config.javamail.UserAuth)4 MessagingException (javax.mail.MessagingException)3 SendmailConfig (org.opennms.netmgt.config.javamail.SendmailConfig)3 JavaSendMailer (org.opennms.javamail.JavaSendMailer)2 File (java.io.File)1 IOException (java.io.IOException)1 Properties (java.util.Properties)1 NoSuchProviderException (javax.mail.NoSuchProviderException)1 Transport (javax.mail.Transport)1 MimeMessage (javax.mail.internet.MimeMessage)1 JavaMailerException (org.opennms.javamail.JavaMailerException)1 MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)1