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