/** * 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; }
/** * 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; }
/** * 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, null); 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; }
/** * 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, null); 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; }
/** * Instantiates a new SNMP Trap northbounder. * * @param configDao the SNMP Trap configuration DAO * @param javaMailDao the JavaMail configuration DAO * @param destinationName the destination name */ public EmailNorthbounder(EmailNorthbounderConfigDao configDao, JavaMailConfigurationDao javaMailDao, String destinationName) { super(NBI_NAME + ":" + destinationName); m_configDao = configDao; m_destination = configDao.getConfig().getEmailDestination(destinationName); // Creating a local copy of the SendmailConfig object, to avoid potential thread contention issues. try { final SendmailConfig sendmail = javaMailDao.getSendMailConfig(destinationName); if (sendmail != null) { final String sendmailText = JaxbUtils.marshal(sendmail); m_sendmail = JaxbUtils.unmarshal(SendmailConfig.class, sendmailText); } } catch (Exception e) { LOG.error("Can't create a copy of the SendmailConfig object named {}.", destinationName, e); } // Saving a local copy of the templates, as they will be overridden every time a new email has to be sent. if (m_sendmail != null && m_sendmail.getSendmailMessage() != null) { final SendmailMessage sendmailMessage = m_sendmail.getSendmailMessage(); m_emailSubjectFormat = sendmailMessage.getSubject(); m_emailBodyFormat = sendmailMessage.getBody(); m_emailFrom = sendmailMessage.getFrom(); m_emailTo = sendmailMessage.getTo(); } }
/** * Instantiates a new SNMP Trap northbounder. * * @param configDao the SNMP Trap configuration DAO * @param javaMailDao the JavaMail configuration DAO * @param destinationName the destination name */ public EmailNorthbounder(EmailNorthbounderConfigDao configDao, JavaMailConfigurationDao javaMailDao, String destinationName) { super(NBI_NAME + ":" + destinationName); m_configDao = configDao; m_destination = configDao.getConfig().getEmailDestination(destinationName); // Creating a local copy of the SendmailConfig object, to avoid potential thread contention issues. try { final SendmailConfig sendmail = javaMailDao.getSendMailConfig(destinationName); if (sendmail != null) { final String sendmailText = JaxbUtils.marshal(sendmail); m_sendmail = JaxbUtils.unmarshal(SendmailConfig.class, sendmailText); } } catch (Exception e) { LOG.error("Can't create a copy of the SendmailConfig object named {}.", destinationName, e); } // Saving a local copy of the templates, as they will be overridden every time a new email has to be sent. if (m_sendmail != null && m_sendmail.getSendmailMessage() != null) { final SendmailMessage sendmailMessage = m_sendmail.getSendmailMessage(); m_emailSubjectFormat = sendmailMessage.getSubject(); m_emailBodyFormat = sendmailMessage.getBody(); m_emailFrom = sendmailMessage.getFrom(); m_emailTo = sendmailMessage.getTo(); } }