Search in sources :

Example 1 with SendmailHost

use of org.opennms.netmgt.config.javamail.SendmailHost 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 2 with SendmailHost

use of org.opennms.netmgt.config.javamail.SendmailHost 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 3 with SendmailHost

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

the class JavaSendMailer method configureProperties.

/**
     * Configure properties.
     * <p>This method uses a properties file reader to pull in opennms styled javamail properties and sets
     * the actual javamail properties.  This is here to preserve the backwards compatibility but configuration
     * will probably change soon.</p>
     * <p>FIXME definitely will change soon, will be deprecated.</p>
     *
     * @param sendmailConfigDefinedProps the sendmail configuration defined properties
     * @param useJmProps a boolean representing the handling of the deprecated javamail-configuration.properties file.
     */
private void configureProperties(Properties sendmailConfigDefinedProps, boolean useJmProps) {
    //this loads the properties from the old style javamail-configuration.properties
    //TODO: deprecate this
    Properties props = null;
    try {
        props = JavaMailerConfig.getProperties();
        /* These strange properties from javamail-configuration.properties need to be translated into actual javax.mail properties
             * FIXME: The precedence of the properties file vs. the SendmailConfiguration should probably be addressed here
             * FIXME: if using a valid sendmail config, it probably doesn't make sense to use any of these properties
             */
        if (useJmProps) {
            m_config.setDebug(PropertiesUtils.getProperty(props, "org.opennms.core.utils.debug", m_config.isDebug()));
            if (m_config.getSendmailHost() != null) {
                final SendmailHost sendmailHost = m_config.getSendmailHost();
                sendmailHost.setHost(PropertiesUtils.getProperty(props, "org.opennms.core.utils.mailHost", sendmailHost.getHost()));
                sendmailHost.setPort(PropertiesUtils.getProperty(props, "org.opennms.core.utils.smtpport", sendmailHost.getPort()));
            }
            if (m_config.getSendmailProtocol() != null) {
                final SendmailProtocol sendmailProtocol = m_config.getSendmailProtocol();
                sendmailProtocol.setMailer(PropertiesUtils.getProperty(props, "org.opennms.core.utils.mailer", sendmailProtocol.getMailer()));
                sendmailProtocol.setTransport(PropertiesUtils.getProperty(props, "org.opennms.core.utils.transport", sendmailProtocol.getTransport()));
                sendmailProtocol.setMessageContentType(PropertiesUtils.getProperty(props, "org.opennms.core.utils.messageContentType", sendmailProtocol.getMessageContentType()));
                sendmailProtocol.setCharSet(PropertiesUtils.getProperty(props, "org.opennms.core.utils.charset", sendmailProtocol.getCharSet()));
                sendmailProtocol.setMessageEncoding(PropertiesUtils.getProperty(props, "org.opennms.core.utils.encoding", sendmailProtocol.getMessageEncoding()));
                sendmailProtocol.setStartTls(PropertiesUtils.getProperty(props, "org.opennms.core.utils.starttls.enable", sendmailProtocol.isStartTls()));
                sendmailProtocol.setQuitWait(PropertiesUtils.getProperty(props, "org.opennms.core.utils.quitwait", sendmailProtocol.isQuitWait()));
                sendmailProtocol.setSslEnable(PropertiesUtils.getProperty(props, "org.opennms.core.utils.smtpssl.enable", sendmailProtocol.isSslEnable()));
            }
            if (m_config.getUserAuth() != null) {
                final UserAuth userAuth = m_config.getUserAuth();
                userAuth.setUserName(PropertiesUtils.getProperty(props, "org.opennms.core.utils.authenticateUser", userAuth.getUserName()));
                userAuth.setPassword(PropertiesUtils.getProperty(props, "org.opennms.core.utils.authenticatePassword", userAuth.getPassword()));
            }
            if (m_config.getSendmailMessage() != null) {
                final SendmailMessage sendmailMessage = m_config.getSendmailMessage();
                sendmailMessage.setFrom(PropertiesUtils.getProperty(props, "org.opennms.core.utils.fromAddress", sendmailMessage.getFrom()));
            }
            m_config.setUseJmta(PropertiesUtils.getProperty(props, "org.opennms.core.utils.useJMTA", m_config.isUseJmta()));
            m_config.setUseAuthentication(PropertiesUtils.getProperty(props, "org.opennms.core.utils.authenticate", m_config.isUseAuthentication()));
        }
    } catch (IOException e) {
        LOG.info("configureProperties: could not load javamail.properties, continuing for is no longer required", e);
    }
    //this sets any javamail properties that were set in the SendmailConfig object
    if (props == null) {
        props = new Properties();
    }
    props.putAll(sendmailConfigDefinedProps);
    if (m_config.getSendmailProtocol() != null) {
        final SendmailProtocol sendmailProtocol = m_config.getSendmailProtocol();
        if (!props.containsKey("mail.smtp.starttls.enable")) {
            props.setProperty("mail.smtp.starttls.enable", String.valueOf(sendmailProtocol.isStartTls()));
        }
        if (!props.containsKey("mail.smtp.quitwait")) {
            props.setProperty("mail.smtp.quitwait", String.valueOf(sendmailProtocol.isQuitWait()));
        }
        if (!props.containsKey("mail.smtp.quitwait")) {
            props.setProperty("mail.smtp.quitwait", String.valueOf(sendmailProtocol.isQuitWait()));
        }
        if (sendmailProtocol.isSslEnable()) {
            if (!props.containsKey("mail.smtps.auth")) {
                props.setProperty("mail.smtps.auth", String.valueOf(m_config.isUseAuthentication()));
            }
            if (!props.containsKey("mail.smtps.socketFactory.class")) {
                props.setProperty("mail.smtps.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            }
            if (!props.containsKey("mail.smtps.socketFactory.port") && m_config.getSendmailHost() != null) {
                props.setProperty("mail.smtps.socketFactory.port", String.valueOf(m_config.getSendmailHost().getPort()));
            }
        }
    }
    if (!props.containsKey("mail.smtp.auth")) {
        props.setProperty("mail.smtp.auth", String.valueOf(m_config.isUseAuthentication()));
    }
    if (!props.containsKey("mail.smtp.port") && m_config.getSendmailHost() != null) {
        props.setProperty("mail.smtp.port", String.valueOf(m_config.getSendmailHost().getPort()));
    }
}
Also used : SendmailProtocol(org.opennms.netmgt.config.javamail.SendmailProtocol) SendmailMessage(org.opennms.netmgt.config.javamail.SendmailMessage) IOException(java.io.IOException) Properties(java.util.Properties) UserAuth(org.opennms.netmgt.config.javamail.UserAuth) SendmailHost(org.opennms.netmgt.config.javamail.SendmailHost)

Example 4 with SendmailHost

use of org.opennms.netmgt.config.javamail.SendmailHost 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)

Aggregations

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