use of org.motechproject.email.exception.EmailSendException in project motech by motech.
the class StatusMessageServiceImpl method sendNotifications.
private void sendNotifications(StatusMessage message) {
List<String> smsRecipients = new ArrayList<>();
for (NotificationRule notificationRule : notificationRulesDataService.retrieveAll()) {
if (notificationRule.matches(message)) {
if (notificationRule.getActionType() == ActionType.SMS) {
smsRecipients.add(notificationRule.getRecipient());
} else if (notificationRule.getActionType() == ActionType.EMAIL) {
try {
emailNotifier.send(message, notificationRule.getRecipient());
} catch (EmailSendException e) {
LOGGER.error("Error while sending notification email to {}", notificationRule.getRecipient(), e);
}
}
}
}
if (!smsRecipients.isEmpty()) {
Map<String, Object> params = new HashMap<>();
params.put("recipients", smsRecipients);
params.put("message", String.format("Motech %s message: [%s] %s", message.getLevel(), message.getModuleName(), message.getText()));
MotechEvent smsEvent = new MotechEvent("send_sms", params);
eventRelay.sendEventMessage(smsEvent);
}
}
use of org.motechproject.email.exception.EmailSendException in project motech by motech.
the class EmailSenderServiceImpl method send.
@Override
@Transactional
public void send(String fromAddress, String toAddress, String subject, String message) throws EmailSendException {
Mail mail = new Mail(fromAddress, toAddress, subject, message);
LOGGER.info(String.format("Sending message [%s] from [%s] to [%s] with subject [%s].", mail.getMessage(), mail.getFromAddress(), mail.getToAddress(), mail.getSubject()));
try {
mailSender.send(getMimeMessagePreparator(mail));
log(new EmailRecord(mail.getFromAddress(), mail.getToAddress(), mail.getSubject(), mail.getMessage(), now(), DeliveryStatus.SENT));
} catch (MailException e) {
log(new EmailRecord(mail.getFromAddress(), mail.getToAddress(), mail.getSubject(), mail.getMessage(), now(), DeliveryStatus.ERROR));
throw new EmailSendException("Unable to send an email to " + mail.getToAddress(), e);
}
}
Aggregations