Search in sources :

Example 1 with JavaSendMailer

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

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

use of org.opennms.javamail.JavaSendMailer 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 4 with JavaSendMailer

use of org.opennms.javamail.JavaSendMailer in project opennms by OpenNMS.

the class EmailNorthbounder method forwardAlarms.

/**
 * Each implementation of the AbstractNorthbounder has a nice queue (Nagle's algorithmic) and the worker thread that processes the queue
 * calls this method to send alarms to the northern NMS.
 *
 * @param alarms the alarms
 * @throws NorthbounderException the northbounder exception
 */
@Override
public void forwardAlarms(List<NorthboundAlarm> alarms) throws NorthbounderException {
    if (alarms == null) {
        String errorMsg = "No alarms in alarms list for syslog forwarding.";
        NorthbounderException e = new NorthbounderException(errorMsg);
        LOG.error(errorMsg, e);
        throw e;
    }
    LOG.info("Forwarding {} alarms to destination {}", alarms.size(), m_destination.getName());
    for (NorthboundAlarm alarm : alarms) {
        try {
            JavaSendMailer mailer = new JavaSendMailer(getSendmailConfig(alarm), false);
            mailer.send();
        } catch (JavaMailerException e) {
            LOG.error("Can't send email for {}", alarm, e);
        }
    }
}
Also used : NorthbounderException(org.opennms.netmgt.alarmd.api.NorthbounderException) JavaSendMailer(org.opennms.javamail.JavaSendMailer) NorthboundAlarm(org.opennms.netmgt.alarmd.api.NorthboundAlarm) JavaMailerException(org.opennms.javamail.JavaMailerException)

Aggregations

JavaSendMailer (org.opennms.javamail.JavaSendMailer)4 SendmailMessage (org.opennms.netmgt.config.javamail.SendmailMessage)3 MimeMessage (javax.mail.internet.MimeMessage)2 JavaMailerException (org.opennms.javamail.JavaMailerException)2 SendmailConfig (org.opennms.netmgt.config.javamail.SendmailConfig)2 SendmailProtocol (org.opennms.netmgt.config.javamail.SendmailProtocol)2 File (java.io.File)1 Message (javax.mail.Message)1 MessagingException (javax.mail.MessagingException)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1 NorthboundAlarm (org.opennms.netmgt.alarmd.api.NorthboundAlarm)1 NorthbounderException (org.opennms.netmgt.alarmd.api.NorthbounderException)1 ReadmailConfig (org.opennms.netmgt.config.javamail.ReadmailConfig)1 SendmailHost (org.opennms.netmgt.config.javamail.SendmailHost)1 UserAuth (org.opennms.netmgt.config.javamail.UserAuth)1 OnmsAcknowledgment (org.opennms.netmgt.model.OnmsAcknowledgment)1 MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)1