Example usage for org.springframework.mail SimpleMailMessage setFrom

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

Introduction

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

Prototype

@Override
    public void setFrom(String from) 

Source Link

Usage

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

/**
 * // ww  w. jav  a2  s.  com
 */
@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
@Override
public Boolean sendActivationMail(ActivationUser activationUser) {
    try {
        if (!StringUtils.isBlank(activationUser.getUser().getMail())) {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(getMailFrom());
            message.setTo(activationUser.getUser().getMail());
            message.setSubject(
                    ApplicationPropertyManager.getApplicationProperty("mail.activationUser.subject"));
            message.setText(ApplicationPropertyManager.getApplicationProperty("mail.activationUser.text",
                    new String[] { activationUser.getUser().getFirstName(),
                            activationUser.getUser().getAccount(),
                            URLEncoder.encode(activationUser.getUuid().toString(), "UTF-8"),
                            ApplicationPropertyManager.getApplicationProperty("website.protocol"),
                            ApplicationPropertyManager.getApplicationProperty("website.domain"),
                            ApplicationPropertyManager.getApplicationProperty("website.contextPath") },
                    "{", "}"));
            getJavaMailSender().send(message);

            activationUser.setMailSended(Boolean.TRUE);
            activationUser.setMailSendedDate(new Date());
            getActivationUserDAO().merge(activationUser);
        } else {
            logger.error("Mail activation user not sended for user " + activationUser.getUser().getAccount()
                    + ". Check mail field on tblUser for account " + activationUser.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}/*from  ww w . j  a v a 2s .  c o m*/
 */
@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
@Override
public Boolean sendApprovedMail(ApprovationUser approvationUser) {
    try {
        if (!StringUtils.isBlank(approvationUser.getUser().getMail())) {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(getMailFrom());
            message.setTo(approvationUser.getUser().getMail());
            message.setSubject(ApplicationPropertyManager.getApplicationProperty("mail.approvedUser.subject"));
            message.setText(ApplicationPropertyManager.getApplicationProperty("mail.approvedUser.text",
                    new String[] { approvationUser.getUser().getFirstName(),
                            ApplicationPropertyManager.getApplicationProperty("website.protocol"),
                            ApplicationPropertyManager.getApplicationProperty("website.domain"),
                            ApplicationPropertyManager.getApplicationProperty("website.contextPath"),
                            approvationUser.getUser().getAccount() },
                    "{", "}"));
            getJavaMailSender().send(message);

            approvationUser.setMailSended(Boolean.TRUE);
            approvationUser.setMailSendedDate(new Date());
            getApprovationUserDAO().merge(approvationUser);
        } else {
            logger.error("Mail approved not sended for user " + approvationUser.getUser().getAccount()
                    + ". Check mail field on tblUser for account " + approvationUser.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}/*  w w w .j  ava 2  s  . com*/
 */
@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
@Override
public Boolean sendForumPostReplyNotificationMail(ForumPostNotified forumPostReplied, ForumPost forumPost,
        User currentUser) {
    try {
        if (!StringUtils.isBlank(currentUser.getMail())) {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(getMailFrom());
            message.setTo(currentUser.getMail());
            if (!Forum.SubType.COURSE.equals(forumPost.getForum().getSubType())) {
                // message for a reply post
                message.setSubject(ApplicationPropertyManager
                        .getApplicationProperty("mail.forumPostReplyNotification.subject",
                                new String[] { forumPost.getUser().getFirstName(),
                                        forumPost.getUser().getLastName(),
                                        forumPost.getParentPost().getSubject() },
                                "{", "}"));
                message.setText(ApplicationPropertyManager.getApplicationProperty(
                        "mail.forumPostReplyNotification.text",
                        new String[] { forumPost.getUser().getFirstName(), forumPost.getUser().getLastName(),
                                forumPost.getParentPost().getSubject(),
                                getForumTopicUrl(forumPost.getTopic(),
                                        Forum.SubType.COURSE.equals(forumPost.getForum().getSubType())) },
                        "{", "}"));
            } else {
                // message for a course transcription post or a course question post
                message.setSubject(ApplicationPropertyManager
                        .getApplicationProperty("mail.courseTranscriptionNotification.subject",
                                new String[] { forumPost.getUser().getFirstName(),
                                        forumPost.getUser().getLastName(), forumPost.getTopic().getSubject() },
                                "{", "}"));
                CourseTopicOption courseTopicOption = getCourseTopicOptionDAO()
                        .getOption(forumPost.getTopic().getTopicId());
                message.setText(ApplicationPropertyManager.getApplicationProperty(
                        "mail.courseTranscriptionNotification.text",
                        new String[] { forumPost.getUser().getFirstName(), forumPost.getUser().getLastName(),
                                forumPost.getTopic().getSubject(), getCourseTopicUrl(courseTopicOption) },
                        "{", "}"));
            }
            getJavaMailSender().send(message);

        } else {
            logger.error("Mail for ForumPost reply not sended for user " + currentUser.getAccount()
                    + ". Check mail field on tblUser for account " + currentUser.getAccount());
        }

        forumPostReplied.setMailSended(Boolean.TRUE);
        forumPostReplied.setMailSendedDate(new Date());

        return Boolean.TRUE;
    } catch (Throwable throwable) {
        logger.error(throwable);
        return Boolean.FALSE;
    }
}

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

/**
 * {@inheritDoc}/* w  w w  . j  a  v  a2s  .c o  m*/
 */
@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
@Override
public Boolean sendForumPostReplyNotificationMessage(ForumPostNotified forumPostReplied, ForumPost forumPost,
        User currentUser) {
    try {
        if (!StringUtils.isBlank(currentUser.getMail())) {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(getMailFrom());
            message.setTo(currentUser.getMail());
            if (!Forum.SubType.COURSE.equals(forumPost.getForum().getSubType())) {
                // message for a reply post
                message.setSubject(ApplicationPropertyManager
                        .getApplicationProperty("mail.forumPostReplyNotification.subject",
                                new String[] { forumPost.getUser().getFirstName(),
                                        forumPost.getUser().getLastName(),
                                        forumPost.getParentPost().getSubject() },
                                "{", "}"));
                message.setText(ApplicationPropertyManager.getApplicationProperty(
                        "mail.forumPostReplyNotification.text",
                        new String[] { forumPost.getUser().getFirstName(), forumPost.getUser().getLastName(),
                                forumPost.getParentPost().getSubject(),
                                getForumTopicUrl(forumPost.getTopic(),
                                        Forum.SubType.COURSE.equals(forumPost.getForum().getSubType())) },
                        "{", "}"));
            } else {
                // message for a course transcription post or a course question post
                message.setSubject(ApplicationPropertyManager
                        .getApplicationProperty("mail.courseTranscriptionNotification.subject",
                                new String[] { forumPost.getUser().getFirstName(),
                                        forumPost.getUser().getLastName(), forumPost.getTopic().getSubject() },
                                "{", "}"));

                CourseTopicOption courseTopicOption = getCourseTopicOptionDAO()
                        .getOption(forumPost.getTopic().getTopicId());
                message.setText(ApplicationPropertyManager.getApplicationProperty(
                        "mail.courseTranscriptionNotification.text",
                        new String[] { forumPost.getUser().getFirstName(), forumPost.getUser().getLastName(),
                                forumPost.getTopic().getSubject(), getCourseTopicUrl(courseTopicOption) },
                        "{", "}"));
            }

            //getJavaMailSender().send(message);
            //instead of sending the message by email sending it by internal message

            // Composing Message
            UserMessage userMessage = new UserMessage();
            userMessage.setSender("Staff");
            User tempUser = currentUser;
            userMessage.setRecipient(tempUser.getAccount());
            userMessage.setSubject(message.getSubject());
            userMessage.setBody(message.getText());
            userMessage.setMessageId(null);
            userMessage.setRecipientStatus(RecipientStatus.NOT_READ);
            userMessage.setSendedDate(new Date());

            //Sending Message
            getCommunityService().createNewMessage(userMessage);

        } else {
            logger.error("Mail for ForumPost reply not sended for user " + currentUser.getAccount()
                    + ". Check mail field on tblUser for account " + currentUser.getAccount());
        }

        forumPostReplied.setMailSended(Boolean.TRUE);
        forumPostReplied.setMailSendedDate(new Date());

        return Boolean.TRUE;
    } catch (Throwable throwable) {
        logger.error(throwable);
        return Boolean.FALSE;
    }
}

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

/**
 * {@inheritDoc}//from   w  ww  .j a v a 2s .  co  m
 */
@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
@Override
public Boolean sendMailLockedUser(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.lockedUser.subject"));
            message.setText(
                    ApplicationPropertyManager.getApplicationProperty("mail.lockedUser.text",
                            new String[] { lockedUser.getUser().getAccount(),
                                    ApplicationPropertyManager.getApplicationProperty("mail.admin.to"), },
                            "{", "}"));
            getJavaMailSender().send(message);
            SimpleMailMessage messageToAdmin = new SimpleMailMessage();
            messageToAdmin.setFrom(getMailFrom());
            messageToAdmin.setTo(ApplicationPropertyManager.getApplicationProperty("mail.admin.to"));
            messageToAdmin.setSubject(
                    ApplicationPropertyManager.getApplicationProperty("mail.lockedUserToAdmin.subject"));
            messageToAdmin
                    .setText(ApplicationPropertyManager.getApplicationProperty("mail.lockedUserToAdmin.text",
                            new String[] { lockedUser.getUser().getAccount() }, "{", "}"));
            getJavaMailSender().send(messageToAdmin);
            lockedUser.setMailSended(Boolean.TRUE);
            lockedUser.setMailSendedDate(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}/*from  ww  w.  j  av  a2  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}//from  w w  w.  j av  a 2s  .c  om
 */
@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);
    message.setSubject("[Opal] Report: " + reportTemplateName);
    message.setText(getEmailNotificationText(reportTemplate, reportOutput));

    for (String emailAddress : reportTemplate.getEmailNotificationAddresses()) {
        message.setTo(emailAddress);/*w w w  . j  av  a  2s . c o m*/
        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.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   ww w  .  ja  v  a  2s  .  co  m*/
        }
    }
}

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

@Test
@Rollback//  ww w. j  ava  2 s . 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);
}