Search in sources :

Example 1 with JavaMailer

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

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

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

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

the class MailTransportMonitor method createMailer.

private JavaMailer createMailer(final MailTransportParameters mailParms) throws JavaMailerException {
    final JavaMailer sendMailer = new JavaMailer(mailParms.getJavamailProperties());
    final String mailPropsPrefix = new StringBuilder("mail.").append(mailParms.getSendTestTransport()).append('.').toString();
    final Properties props = sendMailer.getSession().getProperties();
    //user
    props.setProperty(mailPropsPrefix + "user", mailParms.getSendTestUserName());
    sendMailer.setUser(mailParms.getSendTestUserName());
    sendMailer.setPassword(mailParms.getSendTestPassword());
    //host
    props.setProperty(mailPropsPrefix + "host", mailParms.getSendTestHost());
    sendMailer.setMailHost(mailParms.getSendTestHost());
    //port
    props.setProperty(mailPropsPrefix + "port", String.valueOf(mailParms.getSendTestPort()));
    sendMailer.setSmtpPort(mailParms.getSendTestPort());
    //Override this with configured javamail property because this setting is a generic timeout value
    if (!props.containsKey(mailPropsPrefix + "connectiontimeout")) {
        props.setProperty(mailPropsPrefix + "connectiontimeout", String.valueOf(mailParms.getTimeout()));
    }
    //Override this with configured javamail property because this setting is a generic timeout value
    if (!props.containsKey(mailPropsPrefix + "timeout")) {
        props.setProperty(mailPropsPrefix + "timeout", String.valueOf(mailParms.getTimeout()));
    }
    //from
    props.setProperty(mailPropsPrefix + "from", mailParms.getSendTestFrom());
    sendMailer.setFrom(mailParms.getSendTestFrom());
    //auth
    props.setProperty(mailPropsPrefix + "auth", String.valueOf(mailParms.isSendTestUseAuth()));
    sendMailer.setAuthenticate(mailParms.isSendTestUseAuth());
    //quitwait
    props.setProperty(mailPropsPrefix + "quitwait", String.valueOf(mailParms.isSendTestIsQuitWait()));
    sendMailer.setQuitWait(mailParms.isSendTestIsQuitWait());
    //socketFactory.port
    if (mailParms.isSendTestIsSslEnable()) {
        //override this hard coded default if this property is specified
        if (!props.containsKey(mailPropsPrefix + "socketFactory.class")) {
            props.setProperty(mailPropsPrefix + "socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        }
        props.setProperty(mailPropsPrefix + "socketFactory.port", String.valueOf(mailParms.getSendTestPort()));
        sendMailer.setSmtpPort(mailParms.getSendTestPort());
    }
    sendMailer.setSmtpSsl(mailParms.isSendTestIsSslEnable());
    //starttls.enable
    props.setProperty(mailPropsPrefix + "starttls.enable", String.valueOf(mailParms.isSendTestStartTls()));
    sendMailer.setStartTlsEnabled(mailParms.isSendTestStartTls());
    sendMailer.addExtraHeader(MTM_HEADER_KEY, m_headerValue);
    sendMailer.setSession(Session.getInstance(props, sendMailer.createAuthenticator()));
    return sendMailer;
}
Also used : JavaMailer(org.opennms.javamail.JavaMailer) Properties(java.util.Properties)

Example 5 with JavaMailer

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

the class JavaMailNotificationStrategy method send.

/*
     * (non-Javadoc)
     * 
     * @see org.opennms.netmgt.notifd.NotificationStrategy#send(java.util.List)
     */
/** {@inheritDoc} */
@Override
public int send(List<Argument> arguments) {
    LOG.debug("In the JavaMailNotification class.");
    try {
        JavaMailer jm = buildMessage(arguments);
        jm.mailSend();
    } catch (JavaMailerException e) {
        LOG.error("send: Error sending notification.", e);
        return 1;
    }
    return 0;
}
Also used : JavaMailerException(org.opennms.javamail.JavaMailerException) JavaMailer(org.opennms.javamail.JavaMailer)

Aggregations

JavaMailer (org.opennms.javamail.JavaMailer)6 JavaMailerException (org.opennms.javamail.JavaMailerException)4 TimeoutTracker (org.opennms.core.utils.TimeoutTracker)2 PollStatus (org.opennms.netmgt.poller.PollStatus)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 Properties (java.util.Properties)1 Folder (javax.mail.Folder)1 MessagingException (javax.mail.MessagingException)1 Store (javax.mail.Store)1 Argument (org.opennms.netmgt.model.notifd.Argument)1