use of org.opennms.netmgt.config.javamail.SendmailProtocol in project opennms by OpenNMS.
the class JavaSendMailer method send.
/**
* Send.
*
* @throws JavaMailerException the java mailer exception
*/
public void send() throws JavaMailerException {
if (m_config.getSendmailProtocol() == null || m_config.getSendmailMessage() == null) {
throw new JavaMailerException("sendmail-protocol or sendmail-message are not configured!");
}
try {
final SendmailProtocol sendmailProtocol = m_config.getSendmailProtocol();
final String body = m_config.getSendmailMessage().getBody();
if ("text/plain".equals(sendmailProtocol.getMessageContentType().toLowerCase())) {
m_message.setText(body);
} else {
m_message.setContent(body, sendmailProtocol.getMessageContentType());
}
} catch (final MessagingException e) {
LOG.error("Java Mailer messaging exception: {}", e, e);
throw new JavaMailerException("Java Mailer messaging exception: " + e, e);
}
send(m_message);
}
use of org.opennms.netmgt.config.javamail.SendmailProtocol in project opennms by OpenNMS.
the class JavaSendMailer method send.
/**
* Send.
*
* @param message the message
* @throws JavaMailerException the java mailer exception
*/
public void send(MimeMessage message) throws JavaMailerException {
Transport t = null;
if (m_config.getSendmailProtocol() == null || m_config.getSendmailHost() == null) {
throw new JavaMailerException("sendmail-protocol or sendmail-host are not configured!");
}
try {
SendmailProtocol protoConfig = m_config.getSendmailProtocol();
t = m_session.getTransport(protoConfig.getTransport());
LOG.debug("for transport name '{}' got: {}@{}", protoConfig.getTransport(), t.getClass().getName(), Integer.toHexString(t.hashCode()));
LoggingTransportListener listener = new LoggingTransportListener();
t.addTransportListener(listener);
if ("mta".equals(t.getURLName().getProtocol())) {
// JMTA throws an AuthenticationFailedException if we call connect()
LOG.debug("transport is 'mta', not trying to connect()");
} else {
final SendmailHost sendmailHost = m_config.getSendmailHost();
if (m_config.isUseAuthentication() && m_config.getUserAuth() != null) {
LOG.debug("authenticating to {}", sendmailHost.getHost());
final UserAuth userAuth = m_config.getUserAuth();
t.connect(sendmailHost.getHost(), sendmailHost.getPort(), userAuth.getUserName(), userAuth.getPassword());
} else {
LOG.debug("not authenticating to {}", sendmailHost.getHost());
t.connect(sendmailHost.getHost(), sendmailHost.getPort(), null, null);
}
}
t.sendMessage(message, message.getAllRecipients());
listener.assertAllMessagesDelivered();
} catch (NoSuchProviderException e) {
LOG.error("Couldn't get a transport: {}", e, e);
throw new JavaMailerException("Couldn't get a transport: " + e, e);
} catch (MessagingException e) {
LOG.error("Java Mailer messaging exception: {}", e, e);
throw new JavaMailerException("Java Mailer messaging exception: " + e, e);
} finally {
try {
if (t != null && t.isConnected()) {
t.close();
}
} catch (MessagingException e) {
throw new JavaMailerException("Java Mailer messaging exception on transport close: " + e, e);
}
}
}
use of org.opennms.netmgt.config.javamail.SendmailProtocol in project opennms by OpenNMS.
the class JavaReadMailerTest 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.netmgt.config.javamail.SendmailProtocol 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.netmgt.config.javamail.SendmailProtocol 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