Search in sources :

Example 1 with EmailSendException

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);
    }
}
Also used : HashMap(java.util.HashMap) NotificationRule(org.motechproject.admin.domain.NotificationRule) ArrayList(java.util.ArrayList) EmailSendException(org.motechproject.email.exception.EmailSendException) MotechEvent(org.motechproject.event.MotechEvent)

Example 2 with EmailSendException

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);
    }
}
Also used : EmailRecord(org.motechproject.email.domain.EmailRecord) Mail(org.motechproject.email.domain.Mail) EmailSendException(org.motechproject.email.exception.EmailSendException) MailException(org.springframework.mail.MailException) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

EmailSendException (org.motechproject.email.exception.EmailSendException)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 NotificationRule (org.motechproject.admin.domain.NotificationRule)1 EmailRecord (org.motechproject.email.domain.EmailRecord)1 Mail (org.motechproject.email.domain.Mail)1 MotechEvent (org.motechproject.event.MotechEvent)1 MailException (org.springframework.mail.MailException)1 Transactional (org.springframework.transaction.annotation.Transactional)1