Search in sources :

Example 41 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project scoold by Erudika.

the class ScooldEmailer method sendEmail.

@Override
public boolean sendEmail(final List<String> emails, final String subject, final String body) {
    if (emails == null || emails.isEmpty()) {
        return false;
    }
    asyncExecute(() -> {
        emails.forEach(email -> {
            try {
                mailSender.send((MimeMessage mimeMessage) -> {
                    MimeMessageHelper msg = new MimeMessageHelper(mimeMessage);
                    msg.setTo(email);
                    msg.setSubject(subject);
                    msg.setFrom(Config.SUPPORT_EMAIL, Config.APP_NAME);
                    // body is assumed to be HTML
                    msg.setText(body, true);
                });
                logger.debug("Email sent to {}, {}", email, subject);
            } catch (MailException ex) {
                logger.error("Failed to send email to {} with body [{}]. {}", email, body, ex.getMessage());
            }
        });
    });
    return true;
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MailException(org.springframework.mail.MailException) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper)

Example 42 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project OpenClinica by OpenClinica.

the class NotificationActionProcessor method createMimeMessagePreparator.

private void createMimeMessagePreparator(final ParticipantDTO pDTO, final String email) {
    MimeMessagePreparator preparator = new MimeMessagePreparator() {

        public void prepare(MimeMessage mimeMessage) throws Exception {
            MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
            message.setFrom(EmailEngine.getAdminEmail());
            message.setTo(email);
            message.setSubject(pDTO.getEmailSubject());
            message.setText(pDTO.getMessage());
        }
    };
    BulkEmailSenderService.addMimeMessage(preparator);
}
Also used : MimeMessagePreparator(org.springframework.mail.javamail.MimeMessagePreparator) MimeMessage(javax.mail.internet.MimeMessage) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper)

Example 43 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project OpenClinica by OpenClinica.

the class CoreSecureController method sendEmail.

public Boolean sendEmail(String to, String from, String subject, String body, Boolean htmlEmail, String successMessage, String failMessage, Boolean sendMessage, HttpServletRequest request) throws Exception {
    Boolean messageSent = true;
    try {
        JavaMailSenderImpl mailSender = (JavaMailSenderImpl) SpringServletAccess.getApplicationContext(getServletContext()).getBean("mailSender");
        // @pgawade 09-Feb-2012 #issue 13201 - setting the "mail.smtp.localhost" property to localhost when java API is not able to
        // retrieve the host name
        Properties javaMailProperties = mailSender.getJavaMailProperties();
        if (null != javaMailProperties) {
            if (javaMailProperties.get("mail.smtp.localhost") == null || ((String) javaMailProperties.get("mail.smtp.localhost")).equalsIgnoreCase("")) {
                javaMailProperties.put("mail.smtp.localhost", "localhost");
            }
        }
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, htmlEmail);
        helper.setFrom(from);
        helper.setTo(processMultipleImailAddresses(to.trim()));
        helper.setSubject(subject);
        helper.setText(body, true);
        mailSender.send(mimeMessage);
        if (successMessage != null && sendMessage) {
            addPageMessage(successMessage, request);
        }
        LOGGER.debug("Email sent successfully on {}", new Date());
    } catch (MailException me) {
        me.printStackTrace();
        if (failMessage != null && sendMessage) {
            addPageMessage(failMessage, request);
        }
        LOGGER.debug("Email could not be sent on {} due to: {}", new Date(), me.toString());
        messageSent = false;
    }
    return messageSent;
}
Also used : JavaMailSenderImpl(org.springframework.mail.javamail.JavaMailSenderImpl) MimeMessage(javax.mail.internet.MimeMessage) MailException(org.springframework.mail.MailException) Properties(java.util.Properties) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) Date(java.util.Date)

Example 44 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project zhcet-web by zhcet-amu.

the class EmailService method constructHtmlEmail.

private MimeMessage constructHtmlEmail(String emailAddress, String subject, String body, String[] bcc) throws MessagingException {
    MimeMessage mimeMessage = sender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
    helper.setText(body, true);
    setBasicInfo(helper, senderEmail, emailAddress, subject, bcc);
    return mimeMessage;
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper)

Example 45 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project xmall by Exrick.

the class EmailUtil method sendEmailPayResult.

@Async
public void sendEmailPayResult(String sendTo, String title, String content) {
    JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
    // 设定邮箱服务器配置
    senderImpl.setHost(EMAIL_HOST);
    senderImpl.setUsername(EMAIL_USERNAME);
    senderImpl.setPassword(EMAIL_PASSWORD);
    Properties prop = new Properties();
    // 服务器进行认证
    prop.put("mail.smtp.auth", "true");
    prop.put("mail.smtp.timeout", "20000");
    // qq邮箱需开启
    prop.put("mail.smtp.ssl.enable", "true");
    // 邮箱发送服务器端口,这里设置为465端口 避免服务器解封25端口
    prop.setProperty("mail.smtp.port", "465");
    prop.setProperty("mail.smtp.socketFactory.port", "465");
    prop.put("mail.smtp.auth", "true");
    senderImpl.setJavaMailProperties(prop);
    // 发送html邮件
    MimeMessage mailMessage = senderImpl.createMimeMessage();
    MimeMessageHelper messageHelper = null;
    // 设置邮件内容
    try {
        messageHelper = new MimeMessageHelper(mailMessage, true, "utf-8");
        messageHelper.setTo(sendTo);
        messageHelper.setFrom(EMAIL_SENDER);
        messageHelper.setSubject(title);
        // true表示HTML格式的邮件
        messageHelper.setText(content, true);
    } catch (MessagingException e) {
        e.printStackTrace();
    }
    // 发送邮件
    senderImpl.send(mailMessage);
    log.info("给 " + sendTo + " 的审核结果邮件发送成功");
}
Also used : JavaMailSenderImpl(org.springframework.mail.javamail.JavaMailSenderImpl) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) Properties(java.util.Properties) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) Async(org.springframework.scheduling.annotation.Async)

Aggregations

MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)125 MimeMessage (javax.mail.internet.MimeMessage)112 MessagingException (javax.mail.MessagingException)51 Async (org.springframework.scheduling.annotation.Async)20 MailException (org.springframework.mail.MailException)19 File (java.io.File)17 InternetAddress (javax.mail.internet.InternetAddress)12 FileSystemResource (org.springframework.core.io.FileSystemResource)12 JavaMailSenderImpl (org.springframework.mail.javamail.JavaMailSenderImpl)12 MimeMessagePreparator (org.springframework.mail.javamail.MimeMessagePreparator)12 Date (java.util.Date)11 JavaMailSender (org.springframework.mail.javamail.JavaMailSender)11 HashMap (java.util.HashMap)8 IOException (java.io.IOException)7 Map (java.util.Map)7 MailSendException (org.springframework.mail.MailSendException)7 Template (freemarker.template.Template)6 List (java.util.List)6 Locale (java.util.Locale)6 Properties (java.util.Properties)6