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:com.netflix.genie.web.services.impl.MailServiceImpl.java

@Override
public void sendEmail(@NotBlank(message = "Cannot send email to blank address.") @Nonnull final String toEmail,
        @NotBlank(message = "Subject cannot be empty") @Nonnull final String subject,
        @Nullable final String body) throws GenieException {
    final SimpleMailMessage simpleMailMessage = new SimpleMailMessage();

    simpleMailMessage.setTo(toEmail);//w w w .j av a 2s .  com
    simpleMailMessage.setFrom(this.fromAddress);
    simpleMailMessage.setSubject(subject);

    // check if body is not empty
    if (StringUtils.isNotBlank(body)) {
        simpleMailMessage.setText(body);
    }

    try {
        this.javaMailSender.send(simpleMailMessage);
    } catch (final MailException me) {
        throw new GenieServerException("Failure to send email", me);
    }
}

From source file:com.springsource.insight.plugin.mail.MessageSendOperationCollectionAspectTest.java

private void testSendMessage(int port) {
    JavaMailSenderImpl sender = new JavaMailSenderImpl();
    sender.setHost(NetworkAddressUtil.LOOPBACK_ADDRESS);
    sender.setProtocol(JavaMailSenderImpl.DEFAULT_PROTOCOL);
    sender.setPort(port);/*from   w  w w .  j a  v  a  2 s.  com*/

    SimpleMailMessage message = new SimpleMailMessage();
    message.setFrom("from@com.springsource.insight.plugin.mail");
    message.setTo("to@com.springsource.insight.plugin.mail");
    message.setCc("cc@com.springsource.insight.plugin.mail");
    message.setBcc("bcc@com.springsource.insight.plugin.mail");

    Date now = new Date(System.currentTimeMillis());
    message.setSentDate(now);
    message.setSubject(now.toString());
    message.setText("Test at " + now.toString());
    sender.send(message);

    Operation op = getLastEntered();
    assertNotNull("No operation extracted", op);
    assertEquals("Mismatched operation type", MailDefinitions.SEND_OPERATION, op.getType());
    assertEquals("Mismatched protocol", sender.getProtocol(),
            op.get(MailDefinitions.SEND_PROTOCOL, String.class));
    assertEquals("Mismatched host", sender.getHost(), op.get(MailDefinitions.SEND_HOST, String.class));
    if (port == -1) {
        assertEquals("Mismatched default port", 25, op.getInt(MailDefinitions.SEND_PORT, (-1)));
    } else {
        assertEquals("Mismatched send port", sender.getPort(), op.getInt(MailDefinitions.SEND_PORT, (-1)));
    }

    if (getAspect().collectExtraInformation()) {
        assertAddresses(op, MailDefinitions.SEND_SENDERS, 1);
        assertAddresses(op, MailDefinitions.SEND_RECIPS, 3);

        OperationMap details = op.get(MailDefinitions.SEND_DETAILS, OperationMap.class);
        assertNotNull("No details extracted", details);
        assertEquals("Mismatched subject", message.getSubject(),
                details.get(MailDefinitions.SEND_SUBJECT, String.class));
    }
}

From source file:com.springstudy.utils.email.SimpleMailService.java

/**
 * ??./*from  ww  w  .  j a  va2 s. com*/
 */
public boolean sendNotificationMail(com.gmk.framework.common.utils.email.Email email) {
    SimpleMailMessage msg = new SimpleMailMessage();
    msg.setFrom(Global.getConfig("mailFrom"));
    msg.setTo(email.getAddress());
    if (StringUtils.isNotEmpty(email.getCc())) {
        String cc[] = email.getCc().split(";");
        msg.setCc(cc);//?
    }
    msg.setSubject(email.getSubject());

    // ????
    //      String content = String.format(textTemplate, userName, new Date());
    String content = email.getContent();
    msg.setText(content);
    try {
        mailSender.send(msg);
        if (logger.isInfoEnabled()) {
            logger.info("??{}", StringUtils.join(msg.getTo(), ","));
        }
        return true;
    } catch (Exception e) {
        logger.error(email.getAddressee() + "-" + email.getSubject() + "-" + "??", e);
    }
    return false;
}

From source file:cz.zcu.kiv.eegdatabase.logic.controller.myaccount.ApplyForWritingPermissionController.java

@Override
protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command,
        BindException bindException) throws Exception {
    log.debug("Processing the form");
    ApplyForWritingPermissionCommand afwpc = (ApplyForWritingPermissionCommand) command;

    log.debug("Loading Person object of actual logged user from database");
    Person user = personDao.getPerson(ControllerUtils.getLoggedUserName());

    log.debug("Composing e-mail message");
    SimpleMailMessage mail = new SimpleMailMessage(mailMessage);
    mail.setFrom(user.getEmail());

    log.debug("Loading list of supervisors");
    List<Person> supervisors = personDao.getSupervisors();
    String[] emails = new String[supervisors.size()];
    int i = 0;//from  w  ww  .  jav  a  2  s  .co m
    for (Person supervisor : supervisors) {
        emails[i++] = supervisor.getEmail();
    }
    mail.setTo(emails);

    log.debug("Setting subject to e-mail message");
    mail.setSubject(mail.getSubject() + " - Write permission request from user " + user.getUsername());

    String messageBody = "User " + user.getUsername()
            + " has requested permission for adding data into EEGbase system.\n" + "Reason is: "
            + afwpc.getReason() + "\n" + "Use the address below to grant the write permission.\n";
    String linkAddress = "http://" + request.getLocalAddr() + ":" + request.getLocalPort()
            + request.getContextPath() + "/system/grant-permission.html?id=" + user.getPersonId();
    log.debug("Address is: " + linkAddress);
    messageBody += linkAddress;
    mail.setText(messageBody);

    String mavName = "";
    try {
        log.debug("Sending message");
        mailSender.send(mail);
        log.debug("Mail was sent");
        mavName = getSuccessView();
    } catch (MailException e) {
        log.debug("Mail was not sent");
        mavName = getFailView();
    }

    log.debug("Returning MAV");
    ModelAndView mav = new ModelAndView(mavName);
    return mav;
}

From source file:de.iteratec.iteraplan.businesslogic.service.notifications.NotificationServiceImpl.java

/** {@inheritDoc} */
public void sendEmail(Collection<User> users, String key, Map<User, EmailModel> emailModels) {
    if (sender == null || users == null) {
        return;/*www  .j  a  v  a 2 s  .  c o m*/
    }

    if (StringUtils.isBlank(emailFrom)) {
        LOGGER.error("No outgoing email specified");
        throw new IteraplanTechnicalException(IteraplanErrorMessages.NOTIFICATION_CONFIGURATION_INCOMPLETE);
    }

    for (User user : users) {
        String emailTo = user.getEmail();

        if (StringUtils.isBlank(emailTo)) {
            LOGGER.error("Missing email address for user " + user.getLoginName());
            continue;
        }

        EmailModel model = emailModels.get(user);

        if (key.endsWith(".updated") && model.getChanges().isEmpty()) {
            continue;
        }

        Email email = null;
        try {
            email = emailFactory.createEmail(key, model);
        } catch (MailException e) {
            LOGGER.error("Error generating the email content: ", e);
            continue;
        }

        SimpleMailMessage message = new SimpleMailMessage();
        message.setTo(emailTo);
        message.setFrom(emailFrom);
        message.setSubject(email.getSubject());
        message.setText(email.getText());
        try {
            this.sender.send(message);
        } catch (MailException e) {
            LOGGER.error("Mail cannot be sent: ", e);
            continue;
        }
    }
}

From source file:edu.unc.lib.dl.services.MailNotifier.java

/**
 * Sends a plain text email to the repository administrator.
 *
 * @param subject//from   w  w  w.  j a  v  a  2  s.  co m
 * @param text
 */
public void sendAdministratorMessage(String subject, String text) {
    SimpleMailMessage message = new SimpleMailMessage();
    message.setFrom(this.getRepositoryFromAddress());
    message.setTo(this.administratorAddress);
    message.setSubject("[" + this.irBaseUrl + "]" + subject);
    message.setText(text);
    this.mailSender.send(message);
}

From source file:gov.nih.nci.cabig.caaers.tools.mail.CaaersJavaMailSender.java

@Override
public void send(SimpleMailMessage message) {
    String fromAddress = configuration.get(Configuration.SYSTEM_FROM_EMAIL);
    if (!StringUtils.isBlank(fromAddress))
        message.setFrom(fromAddress);
    super.send(message);
}

From source file:org.encuestame.business.service.MailService.java

@Deprecated
public void send(final String to, final String subject, final String text) throws MailSendException {
    SimpleMailMessage msg = new SimpleMailMessage(this.templateMessage);
    msg.setFrom(getNoEmailResponse());
    msg.setTo(to);/*from w w  w  .  j  a v a  2  s  .  c o m*/
    // msg.setCc();
    msg.setText(text);
    msg.setSubject(buildSubject(subject));
    mailSender.send(msg);
    //log.debug("mail.succesful");
}

From source file:org.encuestame.business.service.MailService.java

/**
 * Send invitation.//  ww w  .j a  v  a2 s  . c o m
 * @param to email to send
 * @param code code of password
 * @throws MailSendException mail exception.
 */
public void sendInvitation(final String to, final String code) throws MailSendException {
    final SimpleMailMessage msg = new SimpleMailMessage(this.templateMessage);
    msg.setFrom(getNoEmailResponse());
    msg.setTo(to);
    msg.setText("<h1>Invitation to Encuestame</h1><p>Please confirm"
            + " this invitation <a>http://www.encuesta.me/cod/" + code + "</a>");
    msg.setSubject(buildSubject("test"));
    try {
        mailSender.send(msg);
    } catch (Exception e) {
        log.error("Error on send email " + e.getMessage());
    }
}

From source file:org.encuestame.business.service.MailService.java

/**
 * Delete notification.// w  w  w. j  a  v  a  2  s  . co  m
 * @param to mail to send
 * @param body body of message
 * @throws MailSendException exception
 */
public void sendDeleteNotification(final String to, final String body) throws MailSendException {
    SimpleMailMessage msg = new SimpleMailMessage(this.templateMessage);
    msg.setFrom(getNoEmailResponse());
    msg.setTo(to);
    msg.setText(body);
    msg.setSubject(
            buildSubject(getMessageProperties("email.message.delete.invitation", buildCurrentLocale(), null)));
    mailSender.send(msg);
}