为了账号安全,请及时绑定邮箱和手机立即绑定

Spring boot email(2)

标签:
SpringBoot

Sending an email with images

public void sendInlineResourceMail(String to, String subject, String content,
                                       String rscPath, String rscId) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content, true);

        FileSystemResource res = new FileSystemResource(new File(rscPath));
        helper.addInline(rscId, res);
        mailSender.send(message);
    }
@Test
public void senInlineResourceMail() throws MessagingException {
    String imgPath = "";
    String rscId = "***";
    String content = "<html><body><img src=\'cid:" + rscId
            + "\'></img></body></html>";
    mailService.sendInlineResourceMail("***", "test", content, imgPath, rscId);
}

Sending a template email with thymeleaf

Step 1: Import dependency.

  • Notice: Do not import thymeleaf until you really start using it.
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

Step 2: Create an html template

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>Email</title>
</head>
<body>
    Hello! Activate account!
    <a href="#" th:href="@{https://www.google.ca}">Activate account</a>
</body>
</html>

Step 3: Send this email

@Test
public void templateMail() throws MessagingException {
    Context context = new Context();
//        context.setVariable("id", "006");
    String emailTemplate = templateEngine.process("emailTemplate", context);
    mailService.sendHTMLMail("send to", "test", emailTemplate);
}

Exception handler

Sending an email is usually an asynchronous request. We need to handle the exceptions in the try catch.
So we add a logger in the service.

Step 1: Add Logger as an instance variable.

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

private final Logger logger = LoggerFactory.getLogger(this.getClass());

Step 2: try catch and print log

public void sendInlineResourceMail(String to, String subject, String content,
                                   String rscPath, String rscId) {
    logger.info("Sending email start: {},{},{},{},{}", to, subject, content, rscPath, rscId);
    MimeMessage message = mailSender.createMimeMessage();
    MimeMessageHelper helper = null;
    try {
        helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content, true);

        FileSystemResource res = new FileSystemResource(new File(rscPath));
        helper.addInline(rscId, res);
        mailSender.send(message);
        logger.info("Sending email success!");
    } catch (MessagingException e) {
        logger.error("Sending email failed:",e);
    }
}

Step 3: test

Run the same test method, and we can see the log in the terminal.

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消