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.osiam.addons.administration.mail.EmailSender.java

private MimeMessage getMimeMessage(String fromAddress, String toAddress, String subject, String mailContent) {
    final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
    final MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
    try {/*  w w w  .j  av a  2s  . c  o m*/
        message.setFrom(fromAddress);
        message.setTo(toAddress);
        message.setSubject(subject);
        message.setText(mailContent, true);
        message.setSentDate(new Date());
    } catch (MessagingException e) {
        throw new SendEmailException("Could not create metadata for email", e);
    }
    return mimeMessage;
}

From source file:com.github.dbourdette.otto.service.mail.Mailer.java

public void send(Mail mail) throws MessagingException, UnsupportedEncodingException {
    MailConfiguration configuration = findConfiguration();

    JavaMailSender javaMailSender = mailSender(configuration);

    MimeMessage mimeMessage = javaMailSender.createMimeMessage();

    MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, "UTF-8");
    helper.setSubject(mail.getSubject());
    helper.setFrom(configuration.getSender());

    for (String name : StringUtils.split(mail.getTo(), ",")) {
        helper.addTo(name);//ww w.  ja  v a2  s  . com
    }

    helper.setText(mail.getHtml(), true);

    javaMailSender.send(mimeMessage);
}

From source file:net.cit.tetrad.resource.MailResource.java

public void sendMail(final String from, final String[] to, final String subject, final Map<String, Object> map)
        throws Exception {
    log.debug("   Send Email Start " + to);
    if (globalHostName.isEmpty())
        globalHostName = mainDao.getGlobalHostname();
    MimeMessagePreparator preparator = new MimeMessagePreparator() {
        public void prepare(MimeMessage mimeMessage) throws Exception {

            Locale locale = Locale.getDefault();
            String vmFileName = "net/cit/tetrad/template/mongofail.vm";
            String subjectStr = subject;
            if (locale.toString().equals("ko_KR") || locale.toString().equals("ko")) {
                vmFileName = "net/cit/tetrad/template/mongofail_ko_KR.vm";
                subjectStr = " ? ";
            }/*from   w w  w  .  j ava2s. c o  m*/

            String mailBody = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, vmFileName, "UTF-8",
                    map);
            MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
            message.setTo(to);
            message.setFrom(from);
            message.setSubject("[" + globalHostName + "] " + subjectStr);
            message.setText(mailBody, true);
        }
    };
    mailSender.send(preparator);
    log.debug("   End Email Start " + to);
}

From source file:com.oakhole.core.email.MimeMailService.java

/**
 * ??MIME?./*from  w  w w. j  a va  2  s . c  om*/
 */
public void sendNotificationMail() {

    try {
        MimeMessage msg = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(msg, true, DEFAULT_ENCODING);

        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        //
        String content = generateContent();
        helper.setText(content, true);
        //
        File attachment = generateAttachment();
        helper.addAttachment(attachment.getName(), attachment);

        mailSender.send(msg);
        logger.info("HTML??{}", to);
    } catch (MessagingException e) {
        logger.error("", e);
    } catch (Exception e) {
        logger.error("??", e);
    }
}

From source file:net.maritimecloud.identityregistry.utils.EmailUtil.java

public void sendBugReport(BugReport report) throws MailException, MessagingException {
    MimeMessage message = this.mailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message, true);
    helper.setTo(bugReportEmail);/*from  ww w  .jav a2  s  . c  o m*/
    helper.setFrom(from);
    helper.setSubject(report.getSubject());
    helper.setText(report.getDescription());
    if (report.getAttachments() != null) {
        for (BugReportAttachment attachment : report.getAttachments()) {
            // Decode base64 encoded data
            byte[] data = Base64.getDecoder().decode(attachment.getData());
            ByteArrayDataSource dataSource = new ByteArrayDataSource(data, attachment.getMimetype());
            helper.addAttachment(attachment.getName(), dataSource);
        }
    }
    this.mailSender.send(message);
}

From source file:ar.com.zauber.commons.message.impl.mail.MimeEmailNotificationStrategy.java

/** @see NotificationStrategy#execute(NotificationAddress[], Message) */
public final void execute(final NotificationAddress[] addresses, final Message message) {
    try {/*from  w w  w . j a  v a  2 s. c  o m*/
        final MailSender mailSender = getMailSender();
        //This is ugly....but if it is not a JavaMailSender it will
        //fail (for instance during tests). And the only way to
        //create a Multipartemail is through JavaMailSender
        if (mailSender instanceof JavaMailSender) {
            final JavaMailSender javaMailSender = (JavaMailSender) mailSender;
            final MimeMessage mail = javaMailSender.createMimeMessage();
            final MimeMessageHelper helper = new MimeMessageHelper(mail, true);
            helper.setFrom(getFromAddress().getEmailStr());
            helper.setTo(SimpleEmailNotificationStrategy.getEmailAddresses(addresses));
            helper.setReplyTo(getEmailAddress(message.getReplyToAddress()));
            helper.setSubject(message.getSubject());
            setContent(helper, (MultipartMessage) message);
            addHeaders(message, mail);
            javaMailSender.send(mail);
        } else {
            final SimpleMailMessage mail = new SimpleMailMessage();
            mail.setFrom(getFromAddress().getEmailStr());
            mail.setTo(getEmailAddresses(addresses));
            mail.setReplyTo(getEmailAddress(message.getReplyToAddress()));
            mail.setSubject(message.getSubject());
            mail.setText(message.getContent());
            mailSender.send(mail);
        }
    } catch (final MessagingException e) {
        throw new RuntimeException("Could not send message", e);
    } catch (final UnsupportedEncodingException e) {
        throw new RuntimeException("Could not send message", e);
    }

}

From source file:cs544.wamp_blog_engine.service.impl.NotificationService.java

public void sendMail(String fromEmail, String toEmail, String emailSubject, String emailBody) {
    //      String fromEmail = emailTemplate.getFrom();
    //      String[] toEmail = emailTemplate.getTo();
    //        String[] toEmails = new String[]{toEmail};
    //      String emailSubject = emailTemplate.getSubject();
    //      String emailBody = String.format(emailTemplate.getText(), dear, content);

    MimeMessage mimeMessage = javaMailSender.createMimeMessage();
    try {/*from  w  ww  .  ja  v  a  2s  . c  o m*/
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);

        helper.setFrom(fromEmail);
        helper.setTo(toEmail);
        helper.setSubject(emailSubject);
        helper.setText(emailBody);

        /*
         uncomment the following lines for attachment FileSystemResource
         file = new FileSystemResource("attachment.jpg");
         helper.addAttachment(file.getFilename(), file);
         */
        javaMailSender.send(mimeMessage);
        System.out.println("Mail sent successfully.");
    } catch (MessagingException e) {
        e.printStackTrace();
    }

}

From source file:net.bafeimao.umbrella.web.service.UserService.java

private void sendRegistrationConfirmEmail(final User user) {
    final MimeMessagePreparator preparator = new MimeMessagePreparator() {
        public void prepare(MimeMessage mimeMessage) throws Exception {
            MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
            message.setTo(user.getEmail());
            message.setFrom("29283212@qq.com");
            message.setSubject("coconut?");

            Map model = new HashMap();
            model.put("user", user);
            String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine,
                    "/templates/registration-confirm-mail.html", "gb2312", model);
            message.setText(text, true);
        }/*from  ww w.j  a  va2  s . com*/
    };
    this.mailSender.send(preparator);
}

From source file:br.eti.danielcamargo.backend.common.core.business.MailService.java

public void enviarEmail(String baseTemplatePath, String template, Map<String, Object> params,
        final String subject, final String... to) {
    try {//from w  ww . ja  v a2  s .  c o m
        StringBuilder textBuilder = new StringBuilder();
        Template temp = freemarkerMailConfiguration.getTemplate(baseTemplatePath + "/cabecalho-email.html");
        StringWriter str = new StringWriter();
        temp.process(params, str);
        final String cabecalho = str.toString();
        textBuilder.append(cabecalho);

        //para montar o email
        template = baseTemplatePath + "/" + template;
        temp = freemarkerMailConfiguration.getTemplate(template);
        str = new StringWriter();
        temp.process(params, str);
        String text = str.toString();
        textBuilder.append(text);

        temp = freemarkerMailConfiguration.getTemplate(baseTemplatePath + "/rodape-email.html");
        str = new StringWriter();
        // parametros rodapeh

        Map<String, Object> paramsRodape = new HashMap<>();
        temp.process(paramsRodape, str);
        text = str.toString();
        textBuilder.append(text);

        // enviar msg
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setSubject(subject);
        helper.setFrom("danielsudpr@gmail.com");
        helper.setText(textBuilder.toString(), true);
        //helper.addInline("logo", new ClassPathResource("/hsnpts/logo.png"));
        helper.setTo(to);

        boolean disabled = true;

        if (disabled) {
            _log.log(Level.INFO, textBuilder.toString());
            return;
        }

        mailSender.send(mimeMessage);

    } catch (TemplateException | IOException | MessagingException | MailException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.gisgraphy.service.MailEngine.java

/**
 * Convenience method for sending messages with attachments.
 * /*from w w  w. java2s. com*/
 * @param recipients
 *                array of e-mail addresses
 * @param sender
 *                e-mail address of sender
 * @param resource
 *                attachment from classpath
 * @param bodyText
 *                text in e-mail
 * @param subject
 *                subject of e-mail
 * @param attachmentName
 *                name for attachment
 * @throws MessagingException
 *                 thrown when can't communicate with SMTP server
 */
public void sendMessage(String[] recipients, String sender, ClassPathResource resource, String bodyText,
        String subject, String attachmentName) throws MessagingException {
    MimeMessage message = ((JavaMailSenderImpl) mailSender).createMimeMessage();

    // use the true flag to indicate you need a multipart message
    MimeMessageHelper helper = new MimeMessageHelper(message, true);

    helper.setTo(recipients);
    helper.setFrom(sender);
    helper.setText(bodyText);
    helper.setSubject(subject);

    helper.addAttachment(attachmentName, resource);

    ((JavaMailSenderImpl) mailSender).send(message);
}