Search in sources :

Example 1 with MailMessageSender

use of org.openmrs.notification.mail.MailMessageSender in project openmrs-module-pihcore by PIH.

the class EmailTestPageController method post.

public String post(@RequestParam(value = "recipients", required = true) String recipients, @RequestParam(value = "sender", required = true) String sender, @RequestParam(value = "subject", required = true) String subject, @RequestParam(value = "message", required = true) String message, @SpringBean("messageService") MessageService messageService, UiUtils ui, UiSessionContext context, PageRequest request) {
    if (Context.hasPrivilege(REQUIRED_PRIVILEGE)) {
        try {
            Message msg = messageService.createMessage(recipients, sender, subject, message);
            // If a custom mail.properties file is found, then create a custom message sender for testing this
            File propertiesFile = new File(OpenmrsUtil.getApplicationDataDirectory(), "mail.properties");
            if (propertiesFile.exists()) {
                Properties p = new Properties();
                OpenmrsUtil.loadProperties(p, propertiesFile);
                Session mailSession = Session.getInstance(p, new Authenticator() {

                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(p.getProperty("mail.user"), p.getProperty("mail.password"));
                    }
                });
                MailMessageSender messageSender = new MailMessageSender(mailSession);
                messageSender.send(msg);
            } else // Otherwise, use the core message sender and configuration from the runtime properties
            {
                messageService.sendMessage(msg);
            }
            request.getSession().setAttribute(EmrConstants.SESSION_ATTRIBUTE_INFO_MESSAGE, "Email Sent Successfully");
        } catch (Exception e) {
            log.error("Unable to send email", e);
            request.getSession().setAttribute(EmrConstants.SESSION_ATTRIBUTE_ERROR_MESSAGE, e.getMessage());
        }
    } else {
        request.getSession().setAttribute(EmrConstants.SESSION_ATTRIBUTE_ERROR_MESSAGE, "You are not authorized to send test emails");
    }
    return "redirect:" + ui.pageLink("pihcore", "admin/emailTest");
}
Also used : MailMessageSender(org.openmrs.notification.mail.MailMessageSender) Message(org.openmrs.notification.Message) Properties(java.util.Properties) File(java.io.File) Authenticator(javax.mail.Authenticator) IOException(java.io.IOException) Session(javax.mail.Session) PasswordAuthentication(javax.mail.PasswordAuthentication)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 Properties (java.util.Properties)1 Authenticator (javax.mail.Authenticator)1 PasswordAuthentication (javax.mail.PasswordAuthentication)1 Session (javax.mail.Session)1 Message (org.openmrs.notification.Message)1 MailMessageSender (org.openmrs.notification.mail.MailMessageSender)1