use of org.springframework.mail.SimpleMailMessage in project molgenis by molgenis.
the class FeedbackController method submitFeedback.
/**
* Handles feedback form submission.
*
* @throws CaptchaException if no valid captcha is supplied
*/
@PostMapping
public String submitFeedback(@Valid FeedbackForm form, @Valid @ModelAttribute CaptchaRequest captchaRequest) throws CaptchaException {
if (!captchaService.validateCaptcha(captchaRequest.getCaptcha())) {
form.setErrorMessage("Invalid captcha.");
return "view-feedback";
}
try {
LOG.info("Sending feedback:" + form);
SimpleMailMessage message = createFeedbackMessage(form);
mailSender.send(message);
form.setSubmitted(true);
captchaService.removeCaptcha();
} catch (MailAuthenticationException e) {
LOG.error("Error authenticating with email server.", e);
form.setErrorMessage(MAIL_AUTHENTICATION_EXCEPTION_MESSAGE);
} catch (MailSendException e) {
LOG.error("Error sending mail", e);
form.setErrorMessage(MAIL_SEND_EXCEPTION_MESSAGE);
}
return "view-feedback";
}
use of org.springframework.mail.SimpleMailMessage in project molgenis by molgenis.
the class AccountServiceImplTest method resetPassword.
@SuppressWarnings("unchecked")
@Test
public void resetPassword() {
when(idGenerator.generateId(SHORT_SECURE_RANDOM)).thenReturn("newPassword");
Query<User> q = mock(Query.class);
when(q.eq(EMAIL, "user@molgenis.org")).thenReturn(q);
when(q.findOne()).thenReturn(user);
when(dataService.query(USER, User.class)).thenReturn(q);
accountService.resetPassword("user@molgenis.org");
verify(dataService).update(USER, user);
verify(user).setPassword("newPassword");
when(user.getPassword()).thenReturn("newPassword");
SimpleMailMessage expected = new SimpleMailMessage();
expected.setTo("jan.jansen@activation.nl");
expected.setSubject("Your new password request");
expected.setText("Somebody, probably you, requested a new password for Molgenis title.\n" + "The new password is: newPassword\n" + "Note: we strongly recommend you reset your password after log-in!");
verify(mailSender).send(expected);
}
use of org.springframework.mail.SimpleMailMessage in project molgenis by molgenis.
the class AccountServiceImplTest method activateUser.
@Test
public void activateUser() {
@SuppressWarnings("unchecked") Query<User> q = mock(Query.class);
when(q.eq(ACTIVE, false)).thenReturn(q);
when(q.and()).thenReturn(q);
when(q.eq(ACTIVATIONCODE, "123")).thenReturn(q);
when(q.findOne()).thenReturn(user);
when(dataService.query(USER, User.class)).thenReturn(q);
accountService.activateUser("123");
ArgumentCaptor<User> argument = ArgumentCaptor.forClass(User.class);
verify(dataService).update(eq(USER), argument.capture());
verify(user).setActive(true);
SimpleMailMessage expected = new SimpleMailMessage();
expected.setTo("jan.jansen@activation.nl");
expected.setText("Dear Jan Jansen,\n\nyour registration request for Molgenis title was approved.\n" + "Your account is now active.\n");
expected.setSubject("Your registration request for Molgenis title");
verify(mailSender).send(expected);
}
use of org.springframework.mail.SimpleMailMessage in project molgenis by molgenis.
the class AccountServiceImpl method createUser.
@Override
@RunAsSystem
@Transactional
public void createUser(User user, String baseActivationUri) throws UsernameAlreadyExistsException, EmailAlreadyExistsException {
// Check if username already exists
if (userService.getUser(user.getUsername()) != null) {
throw new UsernameAlreadyExistsException("Username '" + user.getUsername() + "' already exists.");
}
// Check if email already exists
if (userService.getUserByEmail(user.getEmail()) != null) {
throw new EmailAlreadyExistsException("Email '" + user.getEmail() + "' is already registered.");
}
// collect activation info
String activationCode = idGenerator.generateId(SECURE_RANDOM);
List<String> activationEmailAddresses;
if (authenticationSettings.getSignUpModeration()) {
activationEmailAddresses = userService.getSuEmailAddresses();
if (activationEmailAddresses == null || activationEmailAddresses.isEmpty())
throw new MolgenisDataException("Administrator account is missing required email address");
} else {
String activationEmailAddress = user.getEmail();
if (activationEmailAddress == null || activationEmailAddress.isEmpty())
throw new MolgenisDataException("User '" + user.getUsername() + "' is missing required email address");
activationEmailAddresses = asList(activationEmailAddress);
}
// create user
user.setActivationCode(activationCode);
user.setActive(false);
dataService.add(USER, user);
LOG.debug("created user " + user.getUsername());
// add user to group
Group group = dataService.query(GROUP, Group.class).eq(NAME, ALL_USER_GROUP).findOne();
GroupMember groupMember = null;
if (group != null) {
groupMember = groupMemberFactory.create();
groupMember.setGroup(group);
groupMember.setUser(user);
dataService.add(GROUP_MEMBER, groupMember);
}
// send activation email
URI activationUri = URI.create(baseActivationUri + '/' + activationCode);
try {
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setTo(activationEmailAddresses.toArray(new String[] {}));
mailMessage.setSubject("User registration for " + appSettings.getTitle());
mailMessage.setText(createActivationEmailText(user, activationUri));
mailSender.send(mailMessage);
} catch (MailException mce) {
LOG.error("Could not send signup mail", mce);
if (groupMember != null) {
dataService.delete(GROUP_MEMBER, groupMember);
}
dataService.delete(USER, user);
throw new MolgenisUserException("An error occurred. Please contact the administrator. You are not signed up!");
}
LOG.debug("send activation email for user " + user.getUsername() + " to " + StringUtils.join(activationEmailAddresses, ','));
}
use of org.springframework.mail.SimpleMailMessage in project tuerauf-backend-java by dsteinkopf.
the class LogAndMailService method sendMail.
/**
* This method will send compose and send the message
*/
public void sendMail(String to, String subject, String body) {
SimpleMailMessage message = new SimpleMailMessage();
message.setTo(to);
message.setSubject(subject);
message.setText(body);
if (StringUtils.isNotEmpty(fromMailAddress)) {
message.setFrom(fromMailAddress);
}
javaMailSender.send(message);
}
Aggregations