继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Spring boot email(2)

jerry4013
关注TA
已关注
手记 5
粉丝 3
获赞 2

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.

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP