spring boot实现邮件发送功能

首先创建一个spring boot项目,我这里使用的是简单高效的idea编译器。
spring boot实现邮件发送功能
添加依赖

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

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

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

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

    </dependencies>

application.yml
这里用的是163邮箱smtp协议
spring boot实现邮件发送功能
这里的password是授权码,不是真正的邮箱密码。
获取授权码
首先登录进去邮箱,然后点击上方的设置,点击POP3/SMTP/IMAP这里。
spring boot实现邮件发送功能
点击客户端授权密码,然后点击重置授权码,我这里已经授权过了,所以是重置,如果没有授权过,可以自己设置一个。
spring boot实现邮件发送功能
文本邮件
创建一个service。

@Service
public class MailService {
	//获取的application.yml里边的username
    @Value("${spring.mail.username}")
    private String form;

    @Autowired
    private JavaMailSender mailSender;
    public void sendSimpleMail(String to,String subject,
                               String content){
        SimpleMailMessage message = new SimpleMailMessage();
        //发送给谁
        message.setTo(to);
        //主题
        message.setSubject(subject);
        //内容
        message.setText(content);
        //发件人
        message.setFrom(form);
        mailSender.send(message);
    }
}

HTML邮件

public void sendHtmlMail(String to,String subject,
                             String content) throws MessagingException{
        MimeMessage mimeMessage = mailSender.createMimeMessage();

        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);

        helper.setFrom(form);

        helper.setTo(to);

        helper.setText(content,true);

        helper.setSubject(subject);
        mailSender.send(mimeMessage);
    }

附件邮件

public void sendAttenchmentMail(String to,String content,String subject,
                                    String filePath[])throws MessagingException{
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message,true);
        helper.setTo(to);
        helper.setText(content,true);
        helper.setFrom(form);
        helper.setSubject(subject);
        //读取文件
        for (int i = 0;i<filePath.length;i++){
            FileSystemResource file = new FileSystemResource(new File(filePath[i]));
            String fileName = file.getFilename();
            helper.addAttachment(fileName,file);
        }
        mailSender.send(message);
    }

图片邮件

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.setTo(to);
        helper.setText(content,true);
        helper.setFrom(form);
        helper.setSubject(Subject);
        FileSystemResource resource = new FileSystemResource(new File(rscPath));
        helper.addInline(rscId,resource);

        mailSender.send(message);
    }

模板引擎emailTemplate.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>email</title>
</head>
<body>
    你好,感谢你的注册,请点击以下链接进行注册。
    <a href="#" th:href="@{http://www.ityouknow.com/register/{id}(id=${id})}">**账户</a>
</body>
</html>

测试类

public class MailServiceTest extends DemoApplicationTests {

    @Autowired
    private MailService service;
    @Autowired
    private TemplateEngine templateEngine;

    String to = "收件箱地址";

    @Test
    public void sendSimpleMail() {
        service.sendSimpleMail(to,"这是第一封邮件","http://www.baidu.com");
    }

    @Test
    public void sendHtmlTest() throws MessagingException {
        String content = "<html>\n"+
                "<body>\n"+
                "<h3> hello world,第二封邮件</h3>\n"+
                "</body>\n"+
                "</html>";
        service.sendHtmlMail(to,"第二封邮件",content);
    }

    @Test
    public void sendAttenchmentMail() throws MessagingException{
        String[] filePath = {"第一个附件路径","第二个附件路径"};
        service.sendAttenchmentMail(to,"附件邮件","第三封邮件",filePath);
    }


    @Test
    public void sendInlineResourceMail()throws MessagingException {
        String imgPath = "图片路径";
        String rscId="9001";
        String content = "<html>\n"+
                "<body>这是有图片的邮件:<img src=\'cid:"+rscId+
                "\'></img>"+
                "</body>\n"+
                "</html>";
        service.sendInlineResourceMail(to,"这是第四封邮件",content,imgPath,rscId);
    }

    @Test
    public void testTemplate()throws MessagingException{
        Context context = new Context();
        context.setVariable("id","86");
        String emailContent = templateEngine.process("emailTemplate",context);
        service.sendHtmlMail(to,"这是一个模板邮件",emailContent);
    }
}