Example usage for org.springframework.mail.javamail MimeMessageHelper setFrom

List of usage examples for org.springframework.mail.javamail MimeMessageHelper setFrom

Introduction

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

Prototype

public void setFrom(String from) throws MessagingException 

Source Link

Usage

From source file:org.yes.cart.service.mail.impl.MailComposerImpl.java

/** {@inheritDoc} */
@Override//from   w  w  w .j a va2 s .  c o  m
public void convertMessage(final Mail mail, final MimeMessage mimeMessage)
        throws MessagingException, IOException, ClassNotFoundException {

    final MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");

    helper.setTo(mail.getRecipients());

    helper.setSentDate(new Date());

    if (mail.getCc() != null) {
        helper.setCc(mail.getCc());
    }

    if (mail.getBcc() != null) {
        helper.setBcc(mail.getBcc());
    }

    final String textTemplate = mail.getTextVersion();
    final String htmlTemplate = mail.getHtmlVersion();

    helper.setSubject(mail.getSubject());
    helper.setFrom(mail.getFrom());

    if (textTemplate == null || htmlTemplate == null) {
        if (textTemplate != null) {
            helper.setText(textTemplate, false);
        }

        if (htmlTemplate != null) {
            helper.setText(htmlTemplate, true);
            inlineResources(helper, mail);
        }

    } else {
        helper.setText(textTemplate, htmlTemplate);
        inlineResources(helper, mail);
    }

}

From source file:se.vgregion.webbisar.presentation.WebbisarFlowSupportBean.java

public MailMessageResultBean sendWebbis(final Long webbisId, final MailMessageBean mailMessageBean)
        throws WebbisNotFoundException {

    // Validate email adresses first
    MailMessageResultBean result = validateEmailAddresses(mailMessageBean);
    if (Boolean.FALSE.equals(result.getSuccess())) {
        return result;
    }/*from ww  w .  j av a 2 s  . c  o m*/
    // Validate sender name
    if (StringUtils.isBlank(mailMessageBean.getSenderName())) {
        result.setSuccess(Boolean.FALSE);
        result.setMessage("Namn p avsndare mste anges.");
        return result;
    }

    // use this map to store the information that will be merged into the html template
    Map<String, String> emailInformation = new HashMap<String, String>();
    WebbisBean webbisBean = getWebbis(webbisId, null, null, null, null);
    Map<Long, String> webbisarIdNames = webbisBean.getMultipleBirthSiblingIdsAndNames();

    String messageText = mailMessageBean.getMessage();
    if (!StringUtils.isEmpty(messageText)) {
        messageText = messageText.replace("\r", "").replace("\n", "<br/>");
    }

    // add the current webbis to the list of siblings so that
    // we have them all in the same Map
    webbisarIdNames.put(webbisBean.getId(), webbisBean.getName());

    // add the message and the base url for html links
    emailInformation.put("baseUrl", cfg.getExternalBaseUrl());
    emailInformation.put("message", messageText);
    emailInformation.put("senderName", mailMessageBean.getSenderName());
    emailInformation.put("senderAddress", mailMessageBean.getSenderAddress());

    VelocityContext context = new VelocityContext();
    context.put("emailInfo", emailInformation);
    context.put("webbisInfo", webbisarIdNames);

    Template template = null;
    StringWriter msgWriter = null;
    try {
        velocityEngine.init();
        template = velocityEngine.getTemplate(cfg.getMailTemplate());
        msgWriter = new StringWriter();
        template.merge(context, msgWriter);
    } catch (Exception e1) {
        LOGGER.error("Failed to get/merge velocity template.", e1);
        result.setSuccess(Boolean.FALSE);
        result.setMessage("Internt fel, webbis kunde inte skickas.");
        return result;
    }
    String msgText = msgWriter.toString();

    // Seems OK, try to send mail...
    try {
        InternetAddress fromAddress = null;
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, ENCODING_UTF8);
        try {
            fromAddress = new InternetAddress(cfg.getMailFromAddress(), cfg.getMailFromAddressName());
        } catch (UnsupportedEncodingException e) {
            fromAddress = new InternetAddress(cfg.getMailFromAddress());
        }
        helper.setTo(mailMessageBean.getRecipientAddresses().split(","));
        helper.setFrom(fromAddress);
        helper.setSubject(mailMessageBean.getSubject());
        helper.setText(msgText, true);

        // include the vgr logo
        String logoPath = cfg.getMultimediaFileBaseDir() + "/" + cfg.getMailLogo();
        FileSystemResource res = new FileSystemResource(new File(logoPath));
        helper.addInline("imageIdentifier", res);

        mailSender.send(mimeMessage);
    } catch (MailException ex) {
        LOGGER.error("Failed to create/send mail.", ex);
        result.setSuccess(Boolean.FALSE);
        result.setMessage("Internt fel, webbis kunde inte skickas.");
        return result;
    } catch (MessagingException e) {
        LOGGER.error("Failed to create/send mail.", e);
        result.setSuccess(Boolean.FALSE);
        result.setMessage("Internt fel, webbis kunde inte skickas.");
        return result;
    }

    // ...and all was well...
    result.setSuccess(Boolean.TRUE);
    result.setMessage("Webbis skickad!");
    return result;
}