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");
}
Aggregations