Search in sources :

Example 1 with JavaMailerException

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

the class DefaultReportWrapperService method mailReport.

private static void mailReport(final DeliveryOptions deliveryOptions, final ByteArrayOutputStream outputStream) {
    ByteArrayInputStream inputStream = null;
    try {
        inputStream = new ByteArrayInputStream(outputStream.toByteArray());
        final JavaMailer jm = new JavaMailer();
        jm.setTo(deliveryOptions.getMailTo());
        jm.setSubject(deliveryOptions.getInstanceId());
        jm.setMessageText("Here is your report from the OpenNMS report service.");
        jm.setInputStream(inputStream);
        switch(deliveryOptions.getFormat()) {
            case PDF:
            case SVG:
                jm.setInputStreamName(deliveryOptions.getInstanceId() + ".pdf");
                jm.setInputStreamContentType("application/pdf");
                break;
            case CSV:
                jm.setInputStreamName(deliveryOptions.getInstanceId() + ".csv");
                jm.setInputStreamContentType("text/csv");
                break;
            case HTML:
            default:
                jm.setInputStreamName(deliveryOptions.getInstanceId() + ".htm");
                jm.setInputStreamContentType("text/html");
        }
        jm.mailSend();
    } catch (final JavaMailerException e) {
        LOG.error("Caught JavaMailer exception sending report", e);
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) JavaMailerException(org.opennms.javamail.JavaMailerException) JavaMailer(org.opennms.javamail.JavaMailer)

Example 2 with JavaMailerException

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

the class MailTransportMonitor method readTestMessage.

private PollStatus readTestMessage(final MailTransportParameters mailParms) {
    LOG.debug("readTestMessage: Beginning read mail test.");
    PollStatus status = PollStatus.unavailable("Test not completed.");
    final long interval = mailParms.getReadTestAttemptInterval();
    if (mailParms.isEnd2EndTestInProgress()) {
        LOG.debug("Initially delaying read test: {} because end to end test is in progress.", mailParms.getReadTestAttemptInterval());
        if (delayTest(status, interval) == PollStatus.SERVICE_UNKNOWN) {
            return status;
        }
    }
    Store mailStore = null;
    Folder mailFolder = null;
    try {
        final JavaMailer readMailer = new JavaMailer(mailParms.getJavamailProperties());
        setReadMailProperties(mailParms, readMailer);
        final TimeoutTracker tracker = new TimeoutTracker(mailParms.getParameterMap(), mailParms.getRetries(), mailParms.getTimeout());
        for (tracker.reset(); tracker.shouldRetry(); tracker.nextAttempt()) {
            tracker.startAttempt();
            if (tracker.getAttempt() > 0) {
                if (delayTest(status, interval) == PollStatus.SERVICE_UNKNOWN) {
                    LOG.warn("readTestMessage: Status set to: {} during delay, exiting test.", status);
                    break;
                }
            }
            LOG.debug("readTestMessage: reading mail attempt: {}, elapsed time: {}ms.", (tracker.getAttempt() + 1), String.format("%.2f", tracker.elapsedTimeInMillis()));
            try {
                mailStore = readMailer.getSession().getStore();
                mailFolder = retrieveMailFolder(mailParms, mailStore);
                mailFolder.open(Folder.READ_WRITE);
            } catch (final MessagingException e) {
                if (tracker.shouldRetry()) {
                    LOG.warn("readTestMessage: error reading INBOX", e);
                    closeStore(mailStore, mailFolder);
                    // try again to get mail Folder from Store
                    continue;
                } else {
                    LOG.warn("readTestMessage: error reading INBOX", e);
                    return PollStatus.down(e.getLocalizedMessage());
                }
            }
            if (mailFolder.isOpen() && (mailParms.getReadTest().getSubjectMatch() != null || mailParms.isEnd2EndTestInProgress())) {
                status = processMailSubject(mailParms, mailFolder);
                if (status.getStatusCode() == PollStatus.SERVICE_AVAILABLE) {
                    break;
                }
            }
        }
    } catch (final JavaMailerException e) {
        status = PollStatus.down(e.getLocalizedMessage());
    } finally {
        closeStore(mailStore, mailFolder);
    }
    return status;
}
Also used : PollStatus(org.opennms.netmgt.poller.PollStatus) TimeoutTracker(org.opennms.core.utils.TimeoutTracker) MessagingException(javax.mail.MessagingException) Store(javax.mail.Store) JavaMailerException(org.opennms.javamail.JavaMailerException) JavaMailer(org.opennms.javamail.JavaMailer) Folder(javax.mail.Folder)

Example 3 with JavaMailerException

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

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

the class MailTransportMonitor method sendTestMessage.

/**
 * Sends message based on properties and fields configured for the service.
 *
 * @param mailParms
 * @return a PollStatus
 */
private PollStatus sendTestMessage(final MailTransportParameters mailParms) {
    PollStatus status = PollStatus.unavailable("Test not completed.");
    final long interval = mailParms.getSendTestAttemptInterval();
    final TimeoutTracker tracker = new TimeoutTracker(mailParms.getParameterMap(), mailParms.getRetries(), mailParms.getTimeout());
    for (tracker.reset(); tracker.shouldRetry(); tracker.nextAttempt()) {
        tracker.startAttempt();
        LOG.debug("sendTestMessage: sending mail attempt: {}, elapsed time: {}ms", (tracker.getAttempt() + 1), String.format("%.2f", tracker.elapsedTimeInMillis()));
        try {
            final JavaMailer sendMailer = createMailer(mailParms);
            overRideDefaultProperties(mailParms, sendMailer);
            sendMailer.mailSend();
            status = PollStatus.available();
            break;
        } catch (final JavaMailerException e) {
            status = PollStatus.unavailable(e.getLocalizedMessage());
        }
        if (tracker.shouldRetry()) {
            delayTest(status, interval);
        }
    }
    return status;
}
Also used : PollStatus(org.opennms.netmgt.poller.PollStatus) TimeoutTracker(org.opennms.core.utils.TimeoutTracker) JavaMailerException(org.opennms.javamail.JavaMailerException) JavaMailer(org.opennms.javamail.JavaMailer)

Example 5 with JavaMailerException

use of org.opennms.javamail.JavaMailerException 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

JavaMailerException (org.opennms.javamail.JavaMailerException)6 JavaMailer (org.opennms.javamail.JavaMailer)4 MessagingException (javax.mail.MessagingException)2 TimeoutTracker (org.opennms.core.utils.TimeoutTracker)2 JavaSendMailer (org.opennms.javamail.JavaSendMailer)2 PollStatus (org.opennms.netmgt.poller.PollStatus)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 Folder (javax.mail.Folder)1 Store (javax.mail.Store)1 MimeMessage (javax.mail.internet.MimeMessage)1 NorthboundAlarm (org.opennms.netmgt.alarmd.api.NorthboundAlarm)1 NorthbounderException (org.opennms.netmgt.alarmd.api.NorthbounderException)1 SendmailConfig (org.opennms.netmgt.config.javamail.SendmailConfig)1 SendmailMessage (org.opennms.netmgt.config.javamail.SendmailMessage)1 SendmailProtocol (org.opennms.netmgt.config.javamail.SendmailProtocol)1 MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)1