zoukankan      html  css  js  c++  java
  • SpringBoot系列——mail

      前言

      邮件是许多项目里都需要用到的功能,之前一直都是用JavaMail来发,现在Spring框架为使用JavaMailSender接口发送电子邮件提供了一个简单的抽象,Spring Boot为它提供了自动配置以及启动模块。springboot参考手册介绍:https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/reference/htmlsingle/#boot-features-email

      作为发送方,首先需要开启POP3/SMTP服务,登录邮箱后前往设置进行开启,开启后取得授权码。

      POP3 :
      POP3是Post Office Protocol 3的简称,即邮局协议的第3个版本,规定怎样将个人计算机连接到Internet的邮件服务器和下载电子邮件的电子协议。是因特网电子邮件的第一个离线协议标准,POP3允许用户从服务器上把邮件存储到本地主机(即自己的计算机)上,同时删除保存在邮件服务器上的邮件,而POP3服务器则是遵循POP3协议的接收邮件服务器,用来接收电子邮件的。
      

      SMTP:
      SMTP 的全称是“Simple Mail Transfer Protocol”,即简单邮件传输协议。是一组用于从源地址到目的地址传输邮件的规范,通过来控制邮件的中转方式。SMTP 协议属于 TCP/IP 协议簇,帮助每台计算机在发送或中转信件时找到下一个目的地。SMTP 服务器就是遵循 SMTP 协议的发送邮件服务器。

      代码编写

      maven引包,其中,邮件模板需要用到thymeleaf

            <!-- springboot mail -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-mail</artifactId>
            </dependency>
            <!-- thymeleaf模板 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
            </dependency>
            <!-- springboot web(MVC)-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!-- springboot -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>

      appliaction.propertise配置文件

    #设置服务端口
    server.port=10010
    
    # Email (MailProperties)
    spring.mail.default-encoding=UTF-8
    spring.mail.host=smtp.qq.com
    spring.mail.username=huanzi.qch@qq.com #发送方邮件名 
    spring.mail.password= #授权码
    spring.mail.properties.mail.smtp.auth=true
    spring
    .mail.properties.mail.smtp.starttls.enable=true
    spring
    .mail.properties.mail.smtp.starttls.required=true

      SpringBootMailServiceImpl.java

    @Service
    class SpringBootMailServiceImpl implements SpringBootMailService {
    
        @Autowired
        private JavaMailSender mailSender;
    
        /**
         * 发送方
         */
        @Value("${spring.mail.username}")
        private String from;
    
        /**
         * 发送简单邮件
         *
         * @param to      接收方
         * @param subject 邮件主题
         * @param text    邮件内容
         */
        @Override
        public void sendSimpleMail(String to, String subject, String text) {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(from);
            message.setTo(to);
            message.setSubject(subject);
            message.setText(text);
    
            mailSender.send(message);
        }
    
        /**
         * 发送HTML格式的邮件
         *
         * @param to      接收方
         * @param subject 邮件主题
         * @param content HTML格式的邮件内容
         * @throws MessagingException
         */
        @Override
        public void sendHtmlMail(String to, String subject, String content) throws MessagingException {
            MimeMessage message = mailSender.createMimeMessage();
            //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);
        }
    
        /**
         * 发送HTML格式的邮件,并可以添加附件
         * @param to      接收方
         * @param subject 邮件主题
         * @param content HTML格式的邮件内容
         * @param files   附件
         * @throws MessagingException
         */
        @Override
        public void sendAttachmentsMail(String to, String subject, String content, List<File> files) 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);
            //添加附件
            for(File file : files){
                helper.addAttachment(file.getName(), new FileSystemResource(file));
            }
    
            mailSender.send(message);
        }
    }

      测试controller

        @Autowired
        private SpringBootMailService springBootMailService;
    
        @Autowired
        private TemplateEngine templateEngine;
    
        @GetMapping("/index")
        public String index() throws MessagingException {
            //简单邮件
            springBootMailService.sendSimpleMail("1726639183@qq.com","Simple Mail","第一封简单邮件");
    
            //HTML格式邮件
            Context context = new Context();
            context.setVariable("username","我的小号");
            springBootMailService.sendHtmlMail("1726639183@qq.com","HTML Mail",templateEngine.process("mail/mail",context));
    
            //HTML格式邮件,带附件
            Context context2 = new Context();
            context2.setVariable("username","我的小号(带附件)");
            ArrayList<File> files = new ArrayList<>();
            files.add(new File("C:\Users\Administrator\Desktop\上传测试.txt"));
            files.add(new File("C:\Users\Administrator\Desktop\上传测试2.txt"));
            springBootMailService.sendAttachmentsMail("1726639183@qq.com","Attachments Mail",templateEngine.process("mail/attachment",context2),files);
    
            return "hello springboot!";
        }

      两个html模板,路径:myspringbootsrcmain esources emplatesmail

      mail.html

    <!DOCTYPE html>
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>Mail Templates</title>
    </head>
    <body>
        <h3><span th:text="${username}"></span>,你好!</h3>
        <p style="color: red;">这是一封HTML格式的邮件。</p>
    </body>
    </html>

      attachment.html

    <!DOCTYPE html>
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>Mail Templates Accessory</title>
    </head>
    <body>
        <h3><span th:text="${username}"></span>,你好!</h3>
        <p>这是一封HTML格式的邮件。请收下附件!</p>
    </body>
    </html>

      效果

      Simple Mail

      

      HTML Mail

      Attachments Mail 

      后记

      本文章部分参考:https://www.cnblogs.com/yangtianle/p/8811732.html

      代码开源

      代码已经开源、托管到我的GitHub、码云:

      GitHub:https://github.com/huanzi-qch/springBoot

      码云:https://gitee.com/huanzi-qch/springBoot

  • 相关阅读:
    java实现验证码功能
    C# 自动注册OCX方法
    wamp出现You don’t have permission to access/on this server提示
    C# 图像旋转代码
    C# 实现图像快速 水平 垂直 翻转
    C#创建Graphics对象的方法
    winform控件大小改变是防止背景重绘导致的闪烁(转载)
    C#中DataTable中Rows.Add 和 ImportRow 对比
    MongoDb C# 驱动操作示例
    解决c#所有单线程单元(STA)线程都应使用泵式等待基元(如 CoWaitForMultipleHandles),并在运行时间很长的操作过程中定期发送消息。 转载
  • 原文地址:https://www.cnblogs.com/huanzi-qch/p/9957987.html
Copyright © 2011-2022 走看看