SpringBoot integrated mail service

SpringBoot integrated mail service

Sending emails should be one of the necessary functions of the website, such as registration verification, forgotten password or sending marketing information to users. Early on we would

Use JavaMail-related APIs to write related codes for sending emails. Later, Spring launched JavaMailSender to simplify the process of sending emails.

After that, Spring Boot encapsulated it to have the current one spring-boot-starter-mail. The introduction of the article in this chapter mainly comes from

in this package.

1. Easy to use

1.1 pom dependencies

spring-boot-starter-mailAdd package reference in pom package

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath/>
    </parent>

    <groupId>com.example</groupId>
    <artifactId>spring-boot-mail</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-boot-mail</name>
    <description>spring-boot-mail</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>RELEASE</version>
        </dependency>

        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>RELEASE</version>
        </dependency>

        <!-- 模板引擎 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

1.2 Add mailbox configuration in application.properties

spring.application.name=spirng-boot-mail
# 邮箱服务器地址
spring.mail.host=smtp.163.com
# 用户名
[email protected]
# 密码
spring.mail.password=EVNCPVURUPIFNAXG
spring.mail.default-encoding=UTF-8
# 谁来发送邮件
[email protected]

1.3 Write mailService

package com.example.service;

public interface MailService {
    
    

    void sendSimpleMail(String to, String subject, String content);

    void sendHtmlMail(String to, String subject, String content);

    void sendAttachmentsMail(String to, String subject, String content, String filePath);

    void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId);

}
package com.example.service.impl;

import com.example.service.MailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@Component
public class MailServiceImpl implements MailService {
    
    

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

    @Autowired
    private JavaMailSender mailSender;

    @Value("${mail.fromMail.addr}")
    private String from;

    /**
     * 发送文本邮件
     *
     * @param to
     * @param subject
     * @param content
     */
    @Override
    public void sendSimpleMail(String to, String subject, String content) {
    
    
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(content);
        try {
    
    
            mailSender.send(message);
            logger.info("简单邮件已经发送。");
        } catch (Exception e) {
    
    
            logger.error("发送简单邮件时发生异常!", e);
        }
    }

    /**
     * 发送html邮件
     *
     * @param to
     * @param subject
     * @param content
     */
    @Override
    public void sendHtmlMail(String to, String subject, String content) {
    
    
        MimeMessage message = mailSender.createMimeMessage();
        try {
    
    
            // true表示需要创建一个multipart message
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);
            mailSender.send(message);
            logger.info("html邮件发送成功");
        } catch (MessagingException e) {
    
    
            logger.error("发送html邮件时发生异常!", e);
        }
    }

    /**
     * 发送带附件的邮件
     *
     * @param to
     * @param subject
     * @param content
     * @param filePath
     */
    @Override
    public void sendAttachmentsMail(String to, String subject, String content, String filePath) {
    
    
        MimeMessage message = mailSender.createMimeMessage();
        try {
    
    
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);

            FileSystemResource file = new FileSystemResource(new File(filePath));
            String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
            helper.addAttachment(fileName, file);
            //helper.addAttachment("test"+fileName, file);

            mailSender.send(message);
            logger.info("带附件的邮件已经发送。");
        } catch (MessagingException e) {
    
    
            logger.error("发送带附件的邮件时发生异常!", e);
        }
    }


    /**
     * 发送正文中有静态资源(图片)的邮件
     *
     * @param to
     * @param subject
     * @param content
     * @param rscPath
     * @param rscId
     */
    @Override
    public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId) {
    
    
        MimeMessage message = mailSender.createMimeMessage();
        try {
    
    
            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);
            logger.info("嵌入静态资源的邮件已经发送。");
        } catch (MessagingException e) {
    
    
            logger.error("发送嵌入静态资源的邮件时发生异常!", e);
        }
    }
}

1.4 Startup class

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
    
    

    public static void main(String[] args) {
    
    

        SpringApplication.run(Application.class, args);
    }

}

1.5 Write test class for testing

package com.example.service;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

@RunWith(SpringRunner.class)
@SpringBootTest
public class MailServiceTest {
    
    

    @Autowired
    private MailService mailService;

    @Autowired
    private TemplateEngine templateEngine;

    // 邮件发送到[email protected],邮件标题test simple mail,内容文本内容
    @Test
    public void testSimpleMail() throws Exception {
    
    
        mailService.sendSimpleMail("[email protected]","test simple mail"," hello this is simple mail");
    }

    // 邮件发送到[email protected],邮件标题test simple mail,内容为html格式的内容
    @Test
    public void testHtmlMail() throws Exception {
    
    
        String content="<html>\n" +
                "<body>\n" +
                "    <h3>hello world ! 这是一封html邮件!</h3>\n" +
                "</body>\n" +
                "</html>";
        mailService.sendHtmlMail("[email protected]","test simple mail",content);
    }

    // 邮件发送到[email protected],邮件标题test simple mail,内容为文本内容,带附件
    @Test
    public void sendAttachmentsMail() {
    
    
        String filePath="C:\\Users\\Administrator\\Pictures\\Camera Roll\\img19.jpg";
        mailService.sendAttachmentsMail("[email protected]", "主题:带附件的邮件", "有附件,请查收!", filePath);
    }

    // 邮件发送到[email protected],邮件标题test simple mail,内容为文本内容,发送正文中有静态资源(图片)的邮件
    @Test
    public void sendInlineResourceMail() {
    
    
        String rscId = "neo006";
        String content="<html><body>这是有图片的邮件:<img src=\'cid:" + rscId + "\' ></body></html>";
        String imgPath = "C:\\Users\\Administrator\\Pictures\\Camera Roll\\img19.jpg";
        mailService.sendInlineResourceMail("[email protected]", "主题:这是有图片的邮件", content, imgPath, rscId);
    }

    // 邮件发送到[email protected],邮件标题test simple mail,内容为模板邮件
    @Test
    public void sendTemplateMail() {
    
    
        //创建邮件正文
        Context context = new Context();
        context.setVariable("id", "006");
        String emailContent = templateEngine.process("emailTemplate", context);
        mailService.sendHtmlMail("[email protected]","主题:这是模板邮件",emailContent);
    }
}

1.5.1 Send plain text

package com.example.service;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.thymeleaf.TemplateEngine;

@RunWith(SpringRunner.class)
@SpringBootTest
public class MailServiceTest {
    
    

    @Autowired
    private MailService mailService;

    @Autowired
    private TemplateEngine templateEngine;

    // 邮件发送到[email protected],邮件标题test simple mail,内容文本内容
    @Test
    public void testSimpleMail() throws Exception {
    
    
        mailService.sendSimpleMail("[email protected]", "test simple mail", " hello this is simple mail");
    }
}

insert image description here

insert image description here

insert image description here

But in the process of normal use, we usually add pictures or attachments to the email to enrich the content of the email. The following describes how to use Spring

Boot to send rich emails.

1.5.2 Send html format mail

package com.example.service;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.thymeleaf.TemplateEngine;

@RunWith(SpringRunner.class)
@SpringBootTest
public class MailServiceTest {
    
    

    @Autowired
    private MailService mailService;

    @Autowired
    private TemplateEngine templateEngine;

    // 邮件发送到[email protected],邮件标题test simple mail,内容为html格式的内容
    @Test
    public void testHtmlMail() throws Exception {
    
    
        String content = "<html>\n" +
                "<body>\n" +
                "    <h3>hello world ! 这是一封html邮件!</h3>\n" +
                "</body>\n" +
                "</html>";
        mailService.sendHtmlMail("[email protected]", "test simple mail", content);
    }
    
}

insert image description here

insert image description here

insert image description here

1.5.3 Sending emails with attachments

package com.example.service;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.thymeleaf.TemplateEngine;

@RunWith(SpringRunner.class)
@SpringBootTest
public class MailServiceTest {
    
    

    @Autowired
    private MailService mailService;

    @Autowired
    private TemplateEngine templateEngine;

    // 邮件发送到[email protected],邮件标题test simple mail,内容为文本内容,带附件
    @Test
    public void sendAttachmentsMail() {
    
    
        String filePath = "C:\\Users\\Administrator\\Pictures\\Camera Roll\\img19.jpg";
        mailService.sendAttachmentsMail("[email protected]", "主题:带附件的邮件", "有附件,请查收!", filePath);
    }
    
}

Add multiple attachments can use multiplehelper.addAttachment(fileName, file)

insert image description here

insert image description here

insert image description here

1.5.4 Sending emails with static resources

Static resources in emails generally refer to pictures.

package com.example.service;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.thymeleaf.TemplateEngine;

@RunWith(SpringRunner.class)
@SpringBootTest
public class MailServiceTest {
    
    

    @Autowired
    private MailService mailService;

    @Autowired
    private TemplateEngine templateEngine;

    // 邮件发送到[email protected],邮件标题test simple mail,内容为文本内容,发送正文中有静态资源(图片)的邮件
    @Test
    public void sendInlineResourceMail() {
    
    
        String rscId = "neo006";
        String content = "<html><body>这是有图片的邮件:<img src=\'cid:" + rscId + "\' ></body></html>";
        String imgPath = "C:\\Users\\Administrator\\Pictures\\Camera Roll\\img19.jpg";
        mailService.sendInlineResourceMail("[email protected]", "主题:这是有图片的邮件", content, imgPath, rscId);
    }
    
}

Adding multiple pictures can be achieved by using multiple <img src='cid:" + rscId + "' >andhelper.addInline(rscId, res)

insert image description here

insert image description here

insert image description here

2. Send according to the template

email templateemailTemplate.html

<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8"/>
        <title>Title</title>
    </head>
    <body>
        您好,这是验证邮件,请点击下面的链接完成验证,<br/>
        <a href="#" th:href="@{ http://www.ityouknow.com/neo/{id}(id=${id}) }">激活账号</a>
    </body>
</html>
package com.example.service;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

@RunWith(SpringRunner.class)
@SpringBootTest
public class MailServiceTest {
    
    

    @Autowired
    private MailService mailService;

    @Autowired
    private TemplateEngine templateEngine;

    // 邮件发送到[email protected],邮件标题test simple mail,内容为模板邮件
    @Test
    public void sendTemplateMail() {
    
    
        //创建邮件正文
        Context context = new Context();
        context.setVariable("id", "006");
        String emailContent = templateEngine.process("emailTemplate", context);
        mailService.sendHtmlMail("[email protected]", "主题:这是模板邮件", emailContent);
    }
}

insert image description here

insert image description here

insert image description here

So far, all mail sending services have been completed.

3. Mail system

These are the basic services for sending emails above, but if we want to build an email system, we need to consider the following issues:

3.1 Email template

We often receive emails like this:

尊敬的neo用户:
	恭喜您注册成为xxx网的用户,,同时感谢您对xxx的关注与支持并欢迎您使用xx的产品与服务。

Among them, only the user name of neo is changing, and the content of other emails remains unchanged. If you need to manually splice each email, it will not be elegant, and

And it is inconvenient if the code needs to be changed every time the template is modified. Therefore, it is recommended to make an email template for this kind of email requirements. template

The essence is very simple, just replace the changed parameters in the template and convert them into html strings. Here is thymeleafan example to demonstrate.

1. Import thymeleaf package in pom

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

2. Create emailTemplate.html under resorces/templates

<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8"/>
        <title>Title</title>
    </head>
    <body>
        您好,这是验证邮件,请点击下面的链接完成验证,<br/>
        <a href="#" th:href="@{ http://www.ityouknow.com/neo/{id}(id=${id}) }">激活账号</a>
    </body>
</html>

3. Parse the template and send it

@Test
public void sendTemplateMail() {
    
    
    //创建邮件正文
    Context context = new Context();
    context.setVariable("id", "006");
    String emailContent = templateEngine.process("emailTemplate", context);
    mailService.sendHtmlMail("[email protected]", "主题:这是模板邮件", emailContent);
}

3.2 Failed to send

Due to various reasons, there will always be email sending failures, such as: emails are sent too frequently, network abnormalities, etc. When this happens, I

Generally, we will consider retrying to send emails, which will be implemented in the following steps:

  • 1. After receiving a request to send an email, first record the request and store it in the warehouse.

  • 2. Call the mail sending interface to send mail, and record the sending result into the database.

  • 3. During the time period when the timing system is started to scan, emails that are not successfully sent and the number of retries are less than 3 times will be sent again

3.3 Asynchronous sending

In many cases, sending emails is not the result that our main business must pay attention to. For example, notification and reminder businesses can allow delays or failures. this time

At this time, you can use asynchronous method to send emails to speed up the execution speed of the main transaction. In actual projects, you can use MQ to send emails related parameters, and listen to

Message Queue starts sending mail afterwards.

おすすめ

転載: blog.csdn.net/qq_30614345/article/details/132091785