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