Example usage for org.springframework.mail SimpleMailMessage SimpleMailMessage

List of usage examples for org.springframework.mail SimpleMailMessage SimpleMailMessage

Introduction

In this page you can find the example usage for org.springframework.mail SimpleMailMessage SimpleMailMessage.

Prototype

public SimpleMailMessage() 

Source Link

Document

Create a new SimpleMailMessage .

Usage

From source file:org.medici.bia.service.mail.MailServiceImpl.java

/**
 * {@inheritDoc}/*from  www.j  av a  2  s.co m*/
 */
@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
@Override
public Boolean sendMailUnlockedUser(LockedUser lockedUser) {
    try {
        if (!StringUtils.isBlank(lockedUser.getUser().getMail())) {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(getMailFrom());
            message.setTo(lockedUser.getUser().getMail());
            message.setSubject(ApplicationPropertyManager.getApplicationProperty("mail.unlockedUser.subject"));
            message.setText(ApplicationPropertyManager.getApplicationProperty("mail.unlockedUser.text"));
            getJavaMailSender().send(message);

            lockedUser.setMailUnlockSended(Boolean.TRUE);
            lockedUser.setMailUnlockSendedDate(new Date());
            getLockedUserDAO().merge(lockedUser);
        } else {
            logger.error("Mail locked not sended for user " + lockedUser.getUser().getAccount()
                    + ". Check mail field on tblUser for account " + lockedUser.getUser().getAccount());
        }
        return Boolean.TRUE;
    } catch (Throwable throwable) {
        logger.error(throwable);
        return Boolean.FALSE;
    }
}

From source file:org.medici.bia.service.mail.MailServiceImpl.java

/**
 * {@inheritDoc}/*ww w.j  a va  2 s.c  o  m*/
 */
@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
@Override
public Boolean sendUserPasswordResetMail(PasswordChangeRequest passwordChangeRequest) {
    try {
        if (!StringUtils.isBlank(passwordChangeRequest.getUser().getMail())) {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(getMailFrom());
            message.setTo(passwordChangeRequest.getUser().getMail());
            message.setSubject(
                    ApplicationPropertyManager.getApplicationProperty("mail.resetUserPassword.subject"));
            message.setText(ApplicationPropertyManager.getApplicationProperty("mail.resetUserPassword.text",
                    new String[] { passwordChangeRequest.getUser().getFirstName(),
                            passwordChangeRequest.getUser().getAccount(),
                            URLEncoder.encode(passwordChangeRequest.getUuid().toString(), "UTF-8"),
                            ApplicationPropertyManager.getApplicationProperty("website.protocol"),
                            ApplicationPropertyManager.getApplicationProperty("website.domain"),
                            ApplicationPropertyManager.getApplicationProperty("website.contextPath") },
                    "{", "}"));
            getJavaMailSender().send(message);

            passwordChangeRequest.setMailSended(Boolean.TRUE);
            passwordChangeRequest.setMailSendedDate(new Date());
            getPasswordChangeRequestDAO().merge(passwordChangeRequest);
        } else {
            logger.error(
                    "Mail password reset not sended for user " + passwordChangeRequest.getUser().getAccount()
                            + ". Check mail field on tblUser for account "
                            + passwordChangeRequest.getUser().getAccount());
        }
        return Boolean.TRUE;
    } catch (Throwable throwable) {
        logger.error(throwable);
        return Boolean.FALSE;
    }
}

From source file:org.obiba.opal.shell.commands.ReportCommand.java

private void sendEmailNotification(ReportTemplate reportTemplate, FileObject reportOutput) {
    String reportTemplateName = reportTemplate.getName();

    SimpleMailMessage message = new SimpleMailMessage();
    message.setFrom(fromAddress);//from w w w .ja va 2  s .c  om
    message.setSubject("[Opal] Report: " + reportTemplateName);
    message.setText(getEmailNotificationText(reportTemplate, reportOutput));

    for (String emailAddress : reportTemplate.getEmailNotificationAddresses()) {
        message.setTo(emailAddress);
        try {
            mailSender.send(message);
        } catch (MailException ex) {
            getShell().printf("Email notification not sent: %s", ex.getMessage());
            log.error("Email notification not sent: {}", ex.getMessage());
        }
    }
}

From source file:org.openlmis.core.service.UserService.java

private SimpleMailMessage createEmailMessage(User user, String resetPasswordLink, String subject) {
    String passwordResetToken = generateUUID();
    String[] passwordResetLink = new String[] { user.getFirstName(), user.getLastName(), user.getUserName(),
            resetPasswordLink + passwordResetToken };
    String mailBody = messageService.message("password.reset.email.body", (Object[]) passwordResetLink);

    userRepository.insertPasswordResetToken(user, passwordResetToken);

    SimpleMailMessage emailMessage = new SimpleMailMessage();
    emailMessage.setSubject(subject);//  w w  w  . j a  v  a  2  s.  co  m
    emailMessage.setText(mailBody);
    emailMessage.setTo(user.getEmail());

    return emailMessage;
}

From source file:org.opentestsystem.shared.monitoringalerting.service.AbstractNotificationService.java

private void sendNotifications(final List<Notification> notifications) {
    for (Notification notification : notifications) {
        notificationRepository.save(notification);

        if (this.emailActive) {
            SimpleMailMessage message = new SimpleMailMessage(); // NOPMD
            message.setFrom(this.fromAddress);
            message.setBcc(notification.getAddresses().toArray(new String[] {}));
            message.setSubject(subjectPrefix + " " + notification.getSubject());
            message.setText(notification.getContent());
            mailSender.send(message);//from   w w  w  .  j  a v a 2  s  . c  o  m
        }
    }
}

From source file:org.orcid.core.manager.NotificationManagerTest.java

@Test
@Rollback//from w  w  w. j av  a  2s .  c o m
public void testSendLegacyVerificationEmail() throws JAXBException, IOException, URISyntaxException {
    URI baseUri = new URI("http://testserver.orcid.org");
    SecurityQuestionEntity securityQuestion = new SecurityQuestionEntity();
    securityQuestion.setId(1);
    securityQuestion.setQuestion("What is the name of your favorite teacher?");
    when(securityQuestionDao.find(1)).thenReturn(securityQuestion);

    OrcidMessage orcidMessage = (OrcidMessage) unmarshaller
            .unmarshal(getClass().getResourceAsStream(ORCID_INTERNAL_FULL_XML));
    OrcidProfile orcidProfile = orcidMessage.getOrcidProfile();
    notificationManager.sendLegacyVerificationEmail(orcidProfile, baseUri);

    SimpleMailMessage expected = new SimpleMailMessage();
    expected.setFrom("no_reply@orcid.org");
    expected.setTo("josiah_carberry@brown.edu");
    expected.setSubject("Open Researcher & Contributor ID - Registration Complete");
    expected.setText(
            IOUtils.toString(getClass().getResourceAsStream("example_legacy_verification_email_body.txt")));

    verify(mailSender, times(1)).send(expected);
}

From source file:org.orcid.core.manager.NotificationManagerTest.java

@Test
@Rollback/*from ww  w.  jav a 2s .c  o  m*/
public void testSendVerificationEmail() throws JAXBException, IOException, URISyntaxException {
    URI baseUri = new URI("http://testserver.orcid.org");

    OrcidMessage orcidMessage = (OrcidMessage) unmarshaller
            .unmarshal(getClass().getResourceAsStream(ORCID_INTERNAL_FULL_XML));
    OrcidProfile orcidProfile = orcidMessage.getOrcidProfile();
    notificationManager.sendVerificationEmail(orcidProfile, baseUri);

    SimpleMailMessage expected = new SimpleMailMessage();
    expected.setFrom("no_reply@orcid.org");
    expected.setTo("josiah_carberry@brown.edu");
    expected.setSubject("Open Researcher & Contributor ID - Email Verification Required");
    expected.setText(IOUtils.toString(getClass().getResourceAsStream("example_verification_email_body.txt")));

    verify(mailSender, times(1)).send(expected);
}

From source file:org.orcid.core.manager.NotificationManagerTest.java

@Test
@Rollback/* w  w w.j ava2  s  . co m*/
public void testResetEmail() throws Exception {
    URI baseUri = new URI("http://testserver.orcid.org");

    OrcidMessage orcidMessage = (OrcidMessage) unmarshaller
            .unmarshal(getClass().getResourceAsStream(ORCID_INTERNAL_FULL_XML));
    OrcidProfile orcidProfile = orcidMessage.getOrcidProfile();
    orcidProfile.setPassword("r$nd0m");
    EncryptionManager mockEncypter = mock(EncryptionManager.class);
    getTargetObject(notificationManager, NotificationManagerImpl.class).setEncryptionManager(mockEncypter);
    when(mockEncypter.encryptForExternalUse(any(String.class))).thenReturn(
            "Ey+qsh7G2BFGEuqqkzlYRidL4NokGkIgDE+1KOv6aLTmIyrppdVA6WXFIaQ3KsQpKEb9FGUFRqiWorOfhbB2ww==");
    notificationManager.sendPasswordResetEmail(orcidProfile, baseUri);
    SimpleMailMessage expected = new SimpleMailMessage();
    expected.setFrom("no_reply@orcid.org");
    expected.setTo("josiah_carberry@brown.edu");
    expected.setSubject("Open Researcher & Contributor ID - Password Reset");
    String expectedText = IOUtils.toString(getClass().getResourceAsStream("example_reset_email_body.txt"));
    expected.setText(expectedText);

    verify(mailSender, times(1)).send(expected);
}

From source file:org.orcid.core.manager.NotificationManagerTest.java

@Test
@Rollback/*www  .  j  av  a  2 s  .  co m*/
public void testAmendEmail() throws JAXBException, IOException, URISyntaxException {

    OrcidMessage orcidMessage = (OrcidMessage) unmarshaller
            .unmarshal(getClass().getResourceAsStream(ORCID_INTERNAL_FULL_XML));
    OrcidProfile orcidProfile = orcidMessage.getOrcidProfile();
    notificationManager.sendAmendEmail(orcidProfile, "8888-8888-8888-8880");

    SimpleMailMessage expected = new SimpleMailMessage();
    expected.setFrom("no_reply@orcid.org");
    expected.setTo("josiah_carberry@brown.edu");
    expected.setSubject("Open Researcher & Contributor ID - Record Amended");
    String expectedText = IOUtils.toString(getClass().getResourceAsStream("example_amend_email_body.txt"));
    expected.setText(expectedText);

    verify(mailSender, times(1)).send(expected);
}

From source file:org.orcid.core.manager.NotificationManagerTest.java

@Test
@Rollback/* w w  w.j a  v a 2s.  c  om*/
public void testAddedDelegatesSentCorrectEmail() throws JAXBException, IOException, URISyntaxException {

    OrcidMessage orcidMessage = (OrcidMessage) unmarshaller
            .unmarshal(getClass().getResourceAsStream(ORCID_INTERNAL_FULL_XML));
    OrcidProfile orcidProfile = orcidMessage.getOrcidProfile();
    DelegationDetails firstNewDelegate = new DelegationDetails();
    DelegateSummary firstNewDelegateSummary = new DelegateSummary();
    firstNewDelegateSummary.setCreditName(new CreditName("Jimmy Dove"));
    firstNewDelegate.setDelegateSummary(firstNewDelegateSummary);

    DelegationDetails secondNewDelegate = new DelegationDetails();
    DelegateSummary secondNewDelegateSummary = new DelegateSummary();
    secondNewDelegate.setDelegateSummary(secondNewDelegateSummary);

    notificationManager.sendNotificationToAddedDelegate(orcidProfile,
            Arrays.asList(new DelegationDetails[] { firstNewDelegate }));

    SimpleMailMessage expected = new SimpleMailMessage();
    expected.setFrom("no_reply@orcid.org");
    expected.setTo("josiah_carberry@brown.edu");
    expected.setSubject("Open Researcher & Contributor ID - You've been Made a Proxy!");
    String expectedText = IOUtils
            .toString(getClass().getResourceAsStream("example_added_as_delegate_email.txt"));
    expected.setText(expectedText);

    verify(mailSender, times(1)).send(expected);

    notificationManager.sendNotificationToAddedDelegate(orcidProfile,
            Arrays.asList(new DelegationDetails[] { firstNewDelegate, secondNewDelegate }));
    // check that the mail sender has been called an additional two times
    // because we've added a second delegate
    verify(mailSender, times(3)).send(any(SimpleMailMessage.class));

}