Search in sources :

Example 76 with SimpleMailMessage

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

Example 77 with SimpleMailMessage

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

Example 78 with SimpleMailMessage

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

Example 79 with SimpleMailMessage

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

Example 80 with SimpleMailMessage

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

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