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);
}
}
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;
}
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());
}
}
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;
}
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);
}
}
}
Aggregations