Search in sources :

Example 6 with SendmailMessage

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

Example 7 with SendmailMessage

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

the class JavaMailAckReaderIT method testIntegration.

/**
 * This test requires that 4 emails can be read from a Google account.  The mails should be
 * in this order:
 * Subject matching ackd-configuration expression of action type ack
 * Subject matching ackd-configuration expression of action type ack
 * Subject matching ackd-configuration expression of action type ack
 * Subject matching ackd-configuration expression of action type clear
 *
 * The test has been updated to now include sending an email message to a gmail account.  Just correct
 * the account details for your own local testing.
 *
 * @throws JavaMailerException
 */
@Ignore
@Test
public void testIntegration() throws JavaMailerException {
    String gmailAccount = getUser();
    String gmailPassword = getPassword();
    JavaSendMailer sendMailer = createSendMailer(gmailAccount, gmailPassword);
    SendmailMessage sendMsg = createAckMessage(gmailAccount, "1", "ack");
    sendMailer.setMessage(sendMailer.buildMimeMessage(sendMsg));
    sendMailer.send();
    sendMsg = createAckMessage(gmailAccount, "2", "ack");
    sendMailer.setMessage(sendMailer.buildMimeMessage(sendMsg));
    sendMailer.send();
    sendMsg = createAckMessage(gmailAccount, "3", "ack");
    sendMailer.setMessage(sendMailer.buildMimeMessage(sendMsg));
    sendMailer.send();
    sendMsg = createAckMessage(gmailAccount, "4", "clear");
    sendMailer.setMessage(sendMailer.buildMimeMessage(sendMsg));
    sendMailer.send();
    ReadmailConfig config = m_processor.determineMailReaderConfig();
    Assert.assertNotNull(config);
    updateConfigWithGoogleReadConfiguration(config, gmailAccount, gmailPassword);
    List<Message> msgs = m_processor.retrieveAckMessages();
    List<OnmsAcknowledgment> acks = m_processor.createAcks(msgs);
    Assert.assertNotNull(acks);
    Assert.assertEquals(4, acks.size());
    Assert.assertEquals(AckType.NOTIFICATION, acks.get(0).getAckType());
    Assert.assertEquals(AckAction.ACKNOWLEDGE, acks.get(0).getAckAction());
    Assert.assertEquals(Integer.valueOf(1), acks.get(0).getRefId());
    Assert.assertEquals(getUser() + "@gmail.com", acks.get(0).getAckUser());
    Assert.assertEquals(AckType.NOTIFICATION, acks.get(1).getAckType());
    Assert.assertEquals(AckAction.ACKNOWLEDGE, acks.get(1).getAckAction());
    Assert.assertEquals(Integer.valueOf(2), acks.get(1).getRefId());
    Assert.assertEquals(getUser() + "@gmail.com", acks.get(1).getAckUser());
    Assert.assertEquals(AckType.NOTIFICATION, acks.get(2).getAckType());
    Assert.assertEquals(AckAction.ACKNOWLEDGE, acks.get(2).getAckAction());
    Assert.assertEquals(Integer.valueOf(3), acks.get(2).getRefId());
    Assert.assertEquals(getUser() + "@gmail.com", acks.get(2).getAckUser());
    Assert.assertEquals(AckType.NOTIFICATION, acks.get(3).getAckType());
    Assert.assertEquals(AckAction.CLEAR, acks.get(3).getAckAction());
    Assert.assertEquals(Integer.valueOf(4), acks.get(3).getRefId());
    Assert.assertEquals(getUser() + "@gmail.com", acks.get(3).getAckUser());
}
Also used : OnmsAcknowledgment(org.opennms.netmgt.model.OnmsAcknowledgment) Message(javax.mail.Message) SendmailMessage(org.opennms.netmgt.config.javamail.SendmailMessage) MimeMessage(javax.mail.internet.MimeMessage) SendmailMessage(org.opennms.netmgt.config.javamail.SendmailMessage) JavaSendMailer(org.opennms.javamail.JavaSendMailer) ReadmailConfig(org.opennms.netmgt.config.javamail.ReadmailConfig) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 8 with SendmailMessage

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

the class EmailNorthbounder method getSendmailConfig.

/**
 * Gets the sendmail configuration.
 *
 * @param alarm the northbound alarm
 * @return the sendmail configuration
 */
protected SendmailConfig getSendmailConfig(NorthboundAlarm alarm) {
    SendmailMessage message = new SendmailMessage();
    message.setFrom(m_emailFrom);
    message.setTo(m_emailTo);
    message.setSubject(m_emailSubjectFormat);
    message.setBody(m_emailBodyFormat);
    for (EmailFilter filter : m_destination.getFilters()) {
        if (filter.accepts(alarm)) {
            filter.update(message);
            continue;
        }
    }
    LOG.debug("getSendmailConfig: from = {}", message.getFrom());
    LOG.debug("getSendmailConfig: to = {}", message.getTo());
    Map<String, Object> mapping = createMapping(alarm);
    final String subject = PropertiesUtils.substitute(message.getSubject(), mapping);
    LOG.debug("getSendmailConfig: subject = {}", subject);
    message.setSubject(subject);
    final String body = PropertiesUtils.substitute(message.getBody(), mapping);
    LOG.debug("getSendmailConfig: body = {}", body);
    message.setBody(body);
    m_sendmail.setSendmailMessage(message);
    return m_sendmail;
}
Also used : SendmailMessage(org.opennms.netmgt.config.javamail.SendmailMessage)

Example 9 with SendmailMessage

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

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

the class JavaSendMailer method buildMimeMessage.

/**
 * Builds the mime message.
 *
 * @param msg the sendmail message
 * @return the mime message
 */
public MimeMessage buildMimeMessage(final SendmailMessage msg) {
    MimeMessage mimeMsg = new MimeMessage(m_session);
    if (m_config.getSendmailMessage() != msg) {
        m_config.setSendmailMessage(msg);
    }
    if (m_config.getSendmailMessage() != null) {
        final SendmailMessage configMsg = m_config.getSendmailMessage();
        try {
            final String charset = m_config.getSendmailProtocol() != null ? m_config.getSendmailProtocol().getCharSet() : Charset.defaultCharset().name();
            final MimeMessageHelper helper = new MimeMessageHelper(mimeMsg, false, charset);
            helper.setFrom(configMsg.getFrom());
            helper.setTo(configMsg.getTo());
            helper.setSubject(configMsg.getSubject());
        } catch (final MessagingException e) {
            LOG.warn("found a problem building message: {}", e.getMessage());
        }
    } else {
        LOG.warn("Missing sendmail message configuration. This MIME message will probably be wrong.");
    }
    return mimeMsg;
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) SendmailMessage(org.opennms.netmgt.config.javamail.SendmailMessage) MessagingException(javax.mail.MessagingException) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper)

Aggregations

SendmailMessage (org.opennms.netmgt.config.javamail.SendmailMessage)10 SendmailProtocol (org.opennms.netmgt.config.javamail.SendmailProtocol)4 MimeMessage (javax.mail.internet.MimeMessage)3 JavaSendMailer (org.opennms.javamail.JavaSendMailer)3 SendmailConfig (org.opennms.netmgt.config.javamail.SendmailConfig)3 SendmailHost (org.opennms.netmgt.config.javamail.SendmailHost)3 UserAuth (org.opennms.netmgt.config.javamail.UserAuth)3 Message (javax.mail.Message)2 MessagingException (javax.mail.MessagingException)2 Ignore (org.junit.Ignore)2 Test (org.junit.Test)2 MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)2 File (java.io.File)1 IOException (java.io.IOException)1 Properties (java.util.Properties)1 OrTerm (javax.mail.search.OrTerm)1 SearchTerm (javax.mail.search.SearchTerm)1 SubjectTerm (javax.mail.search.SubjectTerm)1 JavaMailerException (org.opennms.javamail.JavaMailerException)1 ReadmailConfig (org.opennms.netmgt.config.javamail.ReadmailConfig)1