use of org.springframework.mail.SimpleMailMessage in project Gemma by PavlidisLab.
the class SecurityControllerImpl method addUserToGroup.
@Override
public boolean addUserToGroup(String userName, String groupName) {
User userTakingAction = userManager.getCurrentUser();
if (userTakingAction == null) {
throw new IllegalStateException("Cannot add user to group when user is not logged in");
}
User u;
if (userManager.userExists(userName)) {
u = userManager.findByUserName(userName);
if (!u.getEnabled()) {
throw new IllegalArgumentException("Sorry, that user's account is not enabled.");
}
securityService.addUserToGroup(userName, groupName);
} else if (userManager.userWithEmailExists(userName)) {
u = userManager.findByEmail(userName);
if (!u.getEnabled()) {
throw new IllegalArgumentException("Sorry, that user's account is not enabled.");
}
String uname = u.getUserName();
securityService.addUserToGroup(uname, groupName);
} else {
throw new IllegalArgumentException("Sorry, there is no matching user.");
}
/*
* send the user an email.
*/
String emailAddress = u.getEmail();
if (StringUtils.isNotBlank(emailAddress)) {
SecurityControllerImpl.log.debug("Sending email notification to " + emailAddress);
SimpleMailMessage msg = new SimpleMailMessage();
msg.setTo(emailAddress);
msg.setFrom(Settings.getAdminEmailAddress());
msg.setSubject("You have been added to a group on Gemma");
msg.setText(userTakingAction.getUserName() + " has added you to the group '" + groupName + "'.\nTo view groups you belong to, visit " + SecurityControllerImpl.GROUP_MANAGER_URL + "\n\nIf you believe you received this email in error, contact " + Settings.getAdminEmailAddress() + ".");
mailEngine.send(msg);
}
return true;
}
use of org.springframework.mail.SimpleMailMessage in project molgenis by molgenis.
the class FeedbackControllerTest method submit.
@Test
public void submit() throws Exception {
List<String> adminEmails = Collections.singletonList("molgenis@molgenis.org");
when(userService.getSuEmailAddresses()).thenReturn(adminEmails);
mockMvcFeedback.perform(MockMvcRequestBuilders.post(FeedbackController.URI).param("name", "First Last").param("subject", "Feedback form").param("email", "user@domain.com").param("feedback", "Feedback.\nLine two.").param("captcha", "validCaptcha")).andExpect(status().isOk()).andExpect(view().name("view-feedback")).andExpect(model().attribute("feedbackForm", hasProperty("submitted", equalTo(true))));
SimpleMailMessage expected = new SimpleMailMessage();
expected.setTo("molgenis@molgenis.org");
expected.setCc("user@domain.com");
expected.setReplyTo("user@domain.com");
expected.setSubject("[feedback-app123] Feedback form");
expected.setText("Feedback from First Last (user@domain.com):\n\n" + "Feedback.\nLine two.");
verify(mailSender, times(1)).send(expected);
verify(captchaService, times(1)).validateCaptcha("validCaptcha");
}
use of org.springframework.mail.SimpleMailMessage in project molgenis by molgenis.
the class ProgressImplTest method testMailFailed.
@Test
public void testMailFailed() {
jobExecution.setFailureEmail("a@b.c,d@e.f");
progress.start();
progress.status("Working....");
Exception ex = new IllegalArgumentException("blah");
progress.failed(ex);
System.out.println(jobExecution.getLog());
assertTrue(jobExecution.getLog().contains("- Execution started." + System.lineSeparator()));
assertTrue(jobExecution.getLog().contains("- Working...." + System.lineSeparator()));
assertTrue(jobExecution.getLog().contains("- Failed"));
assertTrue(jobExecution.getLog().contains(ex.getMessage()));
SimpleMailMessage mail = new SimpleMailMessage();
mail.setTo(new String[] { "a@b.c", "d@e.f" });
mail.setSubject("Annotator job failed.");
mail.setText(jobExecution.getLog());
Mockito.verify(mailSender).send(mail);
}
use of org.springframework.mail.SimpleMailMessage in project molgenis by molgenis.
the class ProgressImpl method sendEmail.
private void sendEmail(String[] to, String subject, String text) {
if (to.length > 0) {
try {
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setTo(to);
mailMessage.setSubject(subject);
mailMessage.setText(text);
mailSender.send(mailMessage);
} catch (RuntimeException e) {
jobExecution.setProgressMessage(String.format("%s (Mail not sent: %s)", jobExecution.getProgressMessage(), e.getMessage()));
}
}
}
use of org.springframework.mail.SimpleMailMessage in project molgenis by molgenis.
the class ImportRunService method createAndSendStatusMail.
private void createAndSendStatusMail(ImportRun importRun) {
try {
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setTo(userService.getUser(importRun.getUsername()).getEmail());
mailMessage.setSubject(createMailTitle(importRun));
mailMessage.setText(createEnglishMailText(importRun, ZoneId.systemDefault()));
mailSender.send(mailMessage);
} catch (MailException mce) {
LOG.error("Could not send import status mail", mce);
throw new MolgenisDataException("An error occurred. Please contact the administrator.");
}
}
Aggregations