Search in sources :

Example 81 with SimpleMailMessage

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";
}
Also used : SimpleMailMessage(org.springframework.mail.SimpleMailMessage) MailAuthenticationException(org.springframework.mail.MailAuthenticationException) MailSendException(org.springframework.mail.MailSendException) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 82 with SimpleMailMessage

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);
}
Also used : User(org.molgenis.data.security.auth.User) SimpleMailMessage(org.springframework.mail.SimpleMailMessage) Test(org.testng.annotations.Test)

Example 83 with SimpleMailMessage

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);
}
Also used : User(org.molgenis.data.security.auth.User) SimpleMailMessage(org.springframework.mail.SimpleMailMessage) Test(org.testng.annotations.Test)

Example 84 with SimpleMailMessage

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, ','));
}
Also used : Group(org.molgenis.data.security.auth.Group) GroupMember(org.molgenis.data.security.auth.GroupMember) MolgenisDataException(org.molgenis.data.MolgenisDataException) SimpleMailMessage(org.springframework.mail.SimpleMailMessage) MolgenisUserException(org.molgenis.security.user.MolgenisUserException) MailException(org.springframework.mail.MailException) URI(java.net.URI) RunAsSystem(org.molgenis.security.core.runas.RunAsSystem) Transactional(org.springframework.transaction.annotation.Transactional)

Example 85 with SimpleMailMessage

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);
}
Also used : SimpleMailMessage(org.springframework.mail.SimpleMailMessage)

Aggregations

SimpleMailMessage (org.springframework.mail.SimpleMailMessage)106 MailException (org.springframework.mail.MailException)17 Test (org.junit.Test)13 Test (org.testng.annotations.Test)7 Test (org.junit.jupiter.api.Test)6 User (org.molgenis.data.security.auth.User)5 JavaMailSender (org.springframework.mail.javamail.JavaMailSender)5 IOException (java.io.IOException)4 MessagingException (javax.mail.MessagingException)4 AbstractMolgenisSpringTest (org.molgenis.data.AbstractMolgenisSpringTest)4 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)4 MailSendException (org.springframework.mail.MailSendException)4 InternetAddress (jakarta.mail.internet.InternetAddress)3 MimeMessage (jakarta.mail.internet.MimeMessage)3 Date (java.util.Date)3 RunAsSystem (org.molgenis.security.core.runas.RunAsSystem)3 MolgenisUserException (org.molgenis.security.user.MolgenisUserException)3 JavaMailSenderImpl (org.springframework.mail.javamail.JavaMailSenderImpl)3 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)2 User (fr.codechill.spring.model.User)2