Search in sources :

Example 51 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project bamboobsc by billchen198318.

the class MailClientUtils method send.

public static void send(String from, String to, String[] cc, String[] bcc, String[] fileNames, File[] files, String subject, String text) throws MailException, Exception {
    if (mailSender == null) {
        throw new Exception("null mailSender!");
    }
    if (StringUtils.isBlank(from) || StringUtils.isBlank(to)) {
        throw new Exception("from and to is required!");
    }
    if (fileNames != null && files != null) {
        if (fileNames.length != files.length) {
            throw new Exception("File parameter error!");
        }
    }
    MimeMessage message = mailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message, true, Constants.BASE_ENCODING);
    helper.setFrom(from);
    helper.setTo(to.endsWith(";") ? to.substring(0, to.length() - 1) : to);
    helper.setSubject(subject);
    helper.setText(text, true);
    if (null != cc && cc.length > 0) {
        helper.setCc(cc);
    }
    if (null != bcc && bcc.length > 0) {
        helper.setBcc(bcc);
    }
    for (int i = 0; fileNames != null && i < fileNames.length; i++) {
        helper.addAttachment(fileNames[i], files[i]);
    }
    mailSender.send(message);
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) MailException(org.springframework.mail.MailException)

Example 52 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project webapp by elimu-ai.

the class Mailer method sendHtmlWithButton.

public static void sendHtmlWithButton(String to, String cc, String from, String subject, String title, String text, String buttonText, String buttonUrl) {
    logger.info("sendHtmlWithButton");
    if (to.contains(",")) {
        to = to.replace(",", "");
    }
    if (to.contains(":")) {
        to = to.replace(":", "");
    }
    JavaMailSenderImpl javaMailSenderImpl = new JavaMailSenderImpl();
    String smtpHost = ConfigHelper.getProperty("smtp.host");
    javaMailSenderImpl.setHost(smtpHost);
    MimeMessage mimeMessage = javaMailSenderImpl.createMimeMessage();
    try {
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
        mimeMessageHelper.setFrom(from);
        mimeMessageHelper.setTo(to);
        if (StringUtils.isNotBlank(cc)) {
            mimeMessageHelper.setCc(cc);
        }
        mimeMessageHelper.setBcc(ADMIN_EMAIL);
        mimeMessageHelper.setSubject(subject);
        String html = "";
        ResourceLoader resourceLoader = new ClassRelativeResourceLoader(Mailer.class);
        logger.info("Loading file email_template_button.html...");
        Resource resource = resourceLoader.getResource("email_template_button.html");
        InputStream inputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader bufferedReader = null;
        try {
            inputStream = resource.getInputStream();
            inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
            bufferedReader = new BufferedReader(inputStreamReader);
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                if (line.contains("${subject}")) {
                    line = line.replace("${subject}", subject);
                }
                if (line.contains("${title}")) {
                    line = line.replace("${title}", title);
                }
                if (line.contains("${text}")) {
                    line = line.replace("${text}", text);
                }
                if (line.contains("${buttonText}")) {
                    line = line.replace("${buttonText}", buttonText);
                }
                if (line.contains("${buttonUrl}")) {
                    line = line.replace("${buttonUrl}", buttonUrl);
                }
                html += line;
            }
        } catch (IOException ex) {
            logger.error(null, ex);
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    logger.error(null, e);
                }
            }
            if (inputStreamReader != null) {
                try {
                    inputStreamReader.close();
                } catch (IOException e) {
                    logger.error(null, e);
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    logger.error(null, e);
                }
            }
        }
        mimeMessageHelper.setText(text, html);
        logger.info("Sending MIME message to " + to + " with subject \"" + subject + "\"...");
        logger.info("title: " + title);
        logger.info("text: " + text);
        logger.info("buttonText: " + buttonText);
        logger.info("buttonUrl: " + buttonUrl);
        if (EnvironmentContextLoaderListener.env != Environment.DEV) {
            javaMailSenderImpl.send(mimeMessage);
        }
    } catch (MessagingException ex) {
        logger.error(null, ex);
    }
}
Also used : JavaMailSenderImpl(org.springframework.mail.javamail.JavaMailSenderImpl) ClassRelativeResourceLoader(org.springframework.core.io.ClassRelativeResourceLoader) ResourceLoader(org.springframework.core.io.ResourceLoader) ClassRelativeResourceLoader(org.springframework.core.io.ClassRelativeResourceLoader) InputStreamReader(java.io.InputStreamReader) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) InputStream(java.io.InputStream) Resource(org.springframework.core.io.Resource) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper)

Example 53 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project webapp by elimu-ai.

the class Mailer method sendHtml.

public static void sendHtml(String to, String cc, String from, String subject, String title, String text) {
    logger.info("sendPlainText");
    if (to.contains(",")) {
        to = to.replace(",", "");
    }
    if (to.contains(":")) {
        to = to.replace(":", "");
    }
    JavaMailSenderImpl javaMailSenderImpl = new JavaMailSenderImpl();
    String smtpHost = ConfigHelper.getProperty("smtp.host");
    javaMailSenderImpl.setHost(smtpHost);
    MimeMessage mimeMessage = javaMailSenderImpl.createMimeMessage();
    try {
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
        mimeMessageHelper.setFrom(from);
        mimeMessageHelper.setTo(to);
        if (StringUtils.isNotBlank(cc)) {
            mimeMessageHelper.setCc(cc);
        }
        mimeMessageHelper.setBcc(ADMIN_EMAIL);
        mimeMessageHelper.setSubject(subject);
        String html = "";
        ResourceLoader resourceLoader = new ClassRelativeResourceLoader(Mailer.class);
        logger.info("Loading file email_template.html...");
        Resource resource = resourceLoader.getResource("email_template.html");
        InputStream inputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader bufferedReader = null;
        try {
            inputStream = resource.getInputStream();
            inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
            bufferedReader = new BufferedReader(inputStreamReader);
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                if (line.contains("${subject}")) {
                    line = line.replace("${subject}", subject);
                } else if (line.contains("${title}")) {
                    line = line.replace("${title}", title);
                } else if (line.contains("${text}")) {
                    line = line.replace("${text}", text);
                }
                html += line;
            }
        } catch (IOException ex) {
            logger.error(null, ex);
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    logger.error(null, e);
                }
            }
            if (inputStreamReader != null) {
                try {
                    inputStreamReader.close();
                } catch (IOException e) {
                    logger.error(null, e);
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    logger.error(null, e);
                }
            }
        }
        mimeMessageHelper.setText(text, html);
        logger.info("Sending MIME message to " + to + " with subject \"" + subject + "\"...");
        logger.info("title: " + title);
        logger.info("text: " + text);
        if (EnvironmentContextLoaderListener.env != Environment.DEV) {
            javaMailSenderImpl.send(mimeMessage);
        }
    } catch (MessagingException ex) {
        logger.error(null, ex);
    }
}
Also used : JavaMailSenderImpl(org.springframework.mail.javamail.JavaMailSenderImpl) ClassRelativeResourceLoader(org.springframework.core.io.ClassRelativeResourceLoader) ResourceLoader(org.springframework.core.io.ResourceLoader) ClassRelativeResourceLoader(org.springframework.core.io.ClassRelativeResourceLoader) InputStreamReader(java.io.InputStreamReader) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) InputStream(java.io.InputStream) Resource(org.springframework.core.io.Resource) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper)

Example 54 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project ArachneCentralAPI by OHDSI.

the class ArachneMailSender method send.

public void send(ArachneMailMessage mailMessage) {
    MimeMessage message = mailSender.createMimeMessage();
    MimeMessageHelper helper;
    try {
        helper = new MimeMessageHelper(message, true);
        helper.setSubject(mailMessage.getSubject());
        helper.setFrom(from, mailMessage.getFromPersonal());
        helper.setTo(mailMessage.getUser().getEmail());
        helper.setText(buildContent(mailMessage.getTemplate(), mailMessage.getParameters()), true);
        mailSender.send(message);
    } catch (MailConnectException e) {
        LOG.error(e.getMessage(), e);
        throw new MailSendException(e.getMessage());
    } catch (MessagingException | UnsupportedEncodingException ex) {
        LOG.error(ex.getMessage(), ex);
    } catch (MailSendException e) {
        LOG.error(e.getMessage(), e);
        throw new MailSendException("Failed to send e-mail. Please, contact to the administrator.");
    }
}
Also used : MailSendException(org.springframework.mail.MailSendException) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MailConnectException(com.sun.mail.util.MailConnectException) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper)

Example 55 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project uplace.es by Uplace.

the class MailService method sendEmail.

@Async
public void sendEmail(String to, String subject, String content, boolean isMultipart, boolean isHtml) throws MessagingException {
    log.debug("Send email[multipart '{}' and html '{}'] to '{}' with subject '{}' and content={}", isMultipart, isHtml, to, subject, content);
    // Prepare message using a Spring helper
    MimeMessage mimeMessage = javaMailSender.createMimeMessage();
    MimeMessageHelper message = new MimeMessageHelper(mimeMessage, isMultipart, CharEncoding.UTF_8);
    message.setTo(to);
    message.setFrom(jHipsterProperties.getMail().getFrom());
    message.setSubject(subject);
    message.setText(content, isHtml);
    javaMailSender.send(mimeMessage);
    log.debug("Sent email to User '{}'", to);
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) Async(org.springframework.scheduling.annotation.Async)

Aggregations

MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)111 MimeMessage (javax.mail.internet.MimeMessage)103 MessagingException (javax.mail.MessagingException)47 Async (org.springframework.scheduling.annotation.Async)20 File (java.io.File)16 MailException (org.springframework.mail.MailException)16 FileSystemResource (org.springframework.core.io.FileSystemResource)12 Date (java.util.Date)11 JavaMailSenderImpl (org.springframework.mail.javamail.JavaMailSenderImpl)11 InternetAddress (javax.mail.internet.InternetAddress)10 JavaMailSender (org.springframework.mail.javamail.JavaMailSender)9 MimeMessagePreparator (org.springframework.mail.javamail.MimeMessagePreparator)9 MailSendException (org.springframework.mail.MailSendException)7 IOException (java.io.IOException)6 Template (freemarker.template.Template)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 Properties (java.util.Properties)5 DataSource (javax.activation.DataSource)5 Test (org.junit.Test)5