Search in sources :

Example 86 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project tutorials by eugenp.

the class EmailServiceImpl method sendMessageWithAttachment.

@Override
public void sendMessageWithAttachment(String to, String subject, String text, String pathToAttachment) {
    try {
        MimeMessage message = emailSender.createMimeMessage();
        // pass 'true' to the constructor to create a multipart message
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(text);
        FileSystemResource file = new FileSystemResource(new File(pathToAttachment));
        helper.addAttachment("Invoice", file);
        emailSender.send(message);
    } catch (MessagingException e) {
        e.printStackTrace();
    }
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) FileSystemResource(org.springframework.core.io.FileSystemResource) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) File(java.io.File)

Example 87 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project spring-boot by Linda-Tan.

the class TestController method sendEmail.

@RequestMapping("sendemail")
public BaseResponse sendEmail() {
    try {
        final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
        final MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
        message.setFrom("li926893@gmail.com");
        message.setTo("tyf201314@qq.com");
        message.setSubject("测试邮件主题");
        message.setText("测试邮件内容");
        this.mailSender.send(mimeMessage);
        return new ObjectRestResponse("hellowzho直接后果orld");
    } catch (Exception ex) {
        log.error("发送失败", ex);
        return new ObjectRestResponse("发送失败");
    }
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) ObjectRestResponse(com.junliang.spring.pojo.vo.ObjectRestResponse) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 88 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project spring-boot by Linda-Tan.

the class EmailService method sendBytemplate.

/**
 * 发送邮件
 * @param mail
 * @throws Exception
 */
public void sendBytemplate(Email mail) throws Exception {
    Assert.notNull(mail, "email must not be null");
    log.info("发送邮件:{}", mail.getContent());
    MimeMessage message = mailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message, true);
    helper.setFrom(fromUser);
    helper.setTo(mail.getEmail());
    helper.setSubject(mail.getSubject());
    if (StringUtils.isNotBlank(mail.getContent()))
        helper.setText(mail.getContent());
    // 如果有抄送、密送
    if (null != mail.getBcc() && mail.getBcc().length > 0)
        helper.setBcc(mail.getBcc());
    if (null != mail.getCc() && mail.getCc().length > 0)
        helper.setCc(mail.getCc());
    // 如果有指定模板
    if (StringUtils.isNotBlank(mail.getTemplate())) {
        Context context = new Context();
        context.setVariable("email", mail.getKvMap());
        String text = templateEngine.process(mail.getTemplate(), context);
        helper.setText(text, true);
    }
    // 如果有附件
    if (null != mail.getAddAttachments() && mail.getAddAttachments().length > 0) {
        for (File file : mail.getAddAttachments()) {
            helper.addAttachment(file.getName(), file);
        }
    }
    mailSender.send(message);
}
Also used : Context(org.thymeleaf.context.Context) MimeMessage(javax.mail.internet.MimeMessage) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) File(java.io.File)

Example 89 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project ngtesting-platform by aaronchen2k.

the class MailServiceImpl method send.

public void send(String subject, String text, String toEmail) {
    MimeMessage mimeMessage = mailSender.createMimeMessage();
    MimeMessageHelper messageHelper = null;
    messageHelper = new MimeMessageHelper(mimeMessage, ENCODE);
    if (StringUtils.isEmpty(subject)) {
        subject = mailMessage.getSubject();
    }
    try {
        messageHelper.setSubject(subject);
        messageHelper.setText(text, true);
        messageHelper.setTo(toEmail);
        messageHelper.setFrom(mailMessage.getFrom());
    } catch (MessagingException e) {
        e.printStackTrace();
    }
    mailSender.send(mimeMessage);
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper)

Example 90 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project FuryViewer by TheDoctor-95.

the class MailService method sendEmail.

@Async
public void sendEmail(String to, String subject, String content, boolean isMultipart, boolean isHtml) {
    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();
    try {
        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);
    } catch (Exception e) {
        if (log.isDebugEnabled()) {
            log.warn("Email could not be sent to user '{}'", to, e);
        } else {
            log.warn("Email could not be sent to user '{}': {}", to, e.getMessage());
        }
    }
}
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