use of org.opennms.netmgt.config.javamail.SendmailMessage 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.netmgt.config.javamail.SendmailMessage 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.netmgt.config.javamail.SendmailMessage in project opennms by OpenNMS.
the class EmailNorthbounder method getSendmailConfig.
/**
* Gets the sendmail configuration.
*
* @param alarm the northbound alarm
* @return the sendmail configuration
*/
protected SendmailConfig getSendmailConfig(NorthboundAlarm alarm) {
SendmailMessage message = new SendmailMessage();
message.setFrom(m_emailFrom);
message.setTo(m_emailTo);
message.setSubject(m_emailSubjectFormat);
message.setBody(m_emailBodyFormat);
for (EmailFilter filter : m_destination.getFilters()) {
if (filter.accepts(alarm)) {
filter.update(message);
continue;
}
}
LOG.debug("getSendmailConfig: from = {}", message.getFrom());
LOG.debug("getSendmailConfig: to = {}", message.getTo());
Map<String, Object> mapping = createMapping(alarm);
final String subject = PropertiesUtils.substitute(message.getSubject(), mapping);
LOG.debug("getSendmailConfig: subject = {}", subject);
message.setSubject(subject);
final String body = PropertiesUtils.substitute(message.getBody(), mapping);
LOG.debug("getSendmailConfig: body = {}", body);
message.setBody(body);
m_sendmail.setSendmailMessage(message);
return m_sendmail;
}
use of org.opennms.netmgt.config.javamail.SendmailMessage in project opennms by OpenNMS.
the class JavaSendMailer method configureProperties.
/**
* Configure properties.
* <p>This method uses a properties file reader to pull in opennms styled javamail properties and sets
* the actual javamail properties. This is here to preserve the backwards compatibility but configuration
* will probably change soon.</p>
* <p>FIXME definitely will change soon, will be deprecated.</p>
*
* @param sendmailConfigDefinedProps the sendmail configuration defined properties
* @param useJmProps a boolean representing the handling of the deprecated javamail-configuration.properties file.
*/
private void configureProperties(Properties sendmailConfigDefinedProps, boolean useJmProps) {
// this loads the properties from the old style javamail-configuration.properties
// TODO: deprecate this
Properties props = null;
try {
props = JavaMailerConfig.getProperties();
/* These strange properties from javamail-configuration.properties need to be translated into actual javax.mail properties
* FIXME: The precedence of the properties file vs. the SendmailConfiguration should probably be addressed here
* FIXME: if using a valid sendmail config, it probably doesn't make sense to use any of these properties
*/
if (useJmProps) {
m_config.setDebug(PropertiesUtils.getProperty(props, "org.opennms.core.utils.debug", m_config.isDebug()));
if (m_config.getSendmailHost() != null) {
final SendmailHost sendmailHost = m_config.getSendmailHost();
sendmailHost.setHost(PropertiesUtils.getProperty(props, "org.opennms.core.utils.mailHost", sendmailHost.getHost()));
sendmailHost.setPort(PropertiesUtils.getProperty(props, "org.opennms.core.utils.smtpport", sendmailHost.getPort()));
}
if (m_config.getSendmailProtocol() != null) {
final SendmailProtocol sendmailProtocol = m_config.getSendmailProtocol();
sendmailProtocol.setMailer(PropertiesUtils.getProperty(props, "org.opennms.core.utils.mailer", sendmailProtocol.getMailer()));
sendmailProtocol.setTransport(PropertiesUtils.getProperty(props, "org.opennms.core.utils.transport", sendmailProtocol.getTransport()));
sendmailProtocol.setMessageContentType(PropertiesUtils.getProperty(props, "org.opennms.core.utils.messageContentType", sendmailProtocol.getMessageContentType()));
sendmailProtocol.setCharSet(PropertiesUtils.getProperty(props, "org.opennms.core.utils.charset", sendmailProtocol.getCharSet()));
sendmailProtocol.setMessageEncoding(PropertiesUtils.getProperty(props, "org.opennms.core.utils.encoding", sendmailProtocol.getMessageEncoding()));
sendmailProtocol.setStartTls(PropertiesUtils.getProperty(props, "org.opennms.core.utils.starttls.enable", sendmailProtocol.isStartTls()));
sendmailProtocol.setQuitWait(PropertiesUtils.getProperty(props, "org.opennms.core.utils.quitwait", sendmailProtocol.isQuitWait()));
sendmailProtocol.setSslEnable(PropertiesUtils.getProperty(props, "org.opennms.core.utils.smtpssl.enable", sendmailProtocol.isSslEnable()));
}
if (m_config.getUserAuth() != null) {
final UserAuth userAuth = m_config.getUserAuth();
userAuth.setUserName(PropertiesUtils.getProperty(props, "org.opennms.core.utils.authenticateUser", userAuth.getUserName()));
userAuth.setPassword(PropertiesUtils.getProperty(props, "org.opennms.core.utils.authenticatePassword", userAuth.getPassword()));
}
if (m_config.getSendmailMessage() != null) {
final SendmailMessage sendmailMessage = m_config.getSendmailMessage();
sendmailMessage.setFrom(PropertiesUtils.getProperty(props, "org.opennms.core.utils.fromAddress", sendmailMessage.getFrom()));
}
m_config.setUseJmta(PropertiesUtils.getProperty(props, "org.opennms.core.utils.useJMTA", m_config.isUseJmta()));
m_config.setUseAuthentication(PropertiesUtils.getProperty(props, "org.opennms.core.utils.authenticate", m_config.isUseAuthentication()));
}
} catch (IOException e) {
LOG.info("configureProperties: could not load javamail.properties, continuing for is no longer required", e);
}
// this sets any javamail properties that were set in the SendmailConfig object
if (props == null) {
props = new Properties();
}
props.putAll(sendmailConfigDefinedProps);
if (m_config.getSendmailProtocol() != null) {
final SendmailProtocol sendmailProtocol = m_config.getSendmailProtocol();
if (!props.containsKey("mail.smtp.starttls.enable")) {
props.setProperty("mail.smtp.starttls.enable", String.valueOf(sendmailProtocol.isStartTls()));
}
if (!props.containsKey("mail.smtp.quitwait")) {
props.setProperty("mail.smtp.quitwait", String.valueOf(sendmailProtocol.isQuitWait()));
}
if (!props.containsKey("mail.smtp.quitwait")) {
props.setProperty("mail.smtp.quitwait", String.valueOf(sendmailProtocol.isQuitWait()));
}
if (sendmailProtocol.isSslEnable()) {
if (!props.containsKey("mail.smtps.auth")) {
props.setProperty("mail.smtps.auth", String.valueOf(m_config.isUseAuthentication()));
}
if (!props.containsKey("mail.smtps.socketFactory.class")) {
props.setProperty("mail.smtps.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
}
if (!props.containsKey("mail.smtps.socketFactory.port") && m_config.getSendmailHost() != null) {
props.setProperty("mail.smtps.socketFactory.port", String.valueOf(m_config.getSendmailHost().getPort()));
}
}
}
if (!props.containsKey("mail.smtp.auth")) {
props.setProperty("mail.smtp.auth", String.valueOf(m_config.isUseAuthentication()));
}
if (!props.containsKey("mail.smtp.port") && m_config.getSendmailHost() != null) {
props.setProperty("mail.smtp.port", String.valueOf(m_config.getSendmailHost().getPort()));
}
}
use of org.opennms.netmgt.config.javamail.SendmailMessage in project opennms by OpenNMS.
the class JavaSendMailer method buildMimeMessage.
/**
* Builds the mime message.
*
* @param msg the sendmail message
* @return the mime message
*/
public MimeMessage buildMimeMessage(final SendmailMessage msg) {
MimeMessage mimeMsg = new MimeMessage(m_session);
if (m_config.getSendmailMessage() != msg) {
m_config.setSendmailMessage(msg);
}
if (m_config.getSendmailMessage() != null) {
final SendmailMessage configMsg = m_config.getSendmailMessage();
try {
final String charset = m_config.getSendmailProtocol() != null ? m_config.getSendmailProtocol().getCharSet() : Charset.defaultCharset().name();
final MimeMessageHelper helper = new MimeMessageHelper(mimeMsg, false, charset);
helper.setFrom(configMsg.getFrom());
helper.setTo(configMsg.getTo());
helper.setSubject(configMsg.getSubject());
} catch (final MessagingException e) {
LOG.warn("found a problem building message: {}", e.getMessage());
}
} else {
LOG.warn("Missing sendmail message configuration. This MIME message will probably be wrong.");
}
return mimeMsg;
}
Aggregations