• 发送邮件(三)



    一 发送邮件小例子

    使用是新浪邮箱给注册该网站的用户发送邮件用于激活账号。

    image-20221003193312865

    spring整合发送邮件。

    • 使用的springboot的版本是2.1,因此使用的spring-boot-starter-mail版本要springboot相对应
    
    		<dependency>
    			<groupId>org.springframework.bootgroupId>
    			<artifactId>spring-boot-starter-mailartifactId>
    			<version>2.1.5.RELEASEversion>
    		dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    配置yml文件。

    • 其中 host为发送邮件的主机,可以在邮箱的设置中找到。
    • post 为端口。邮箱的默认端口为465
    • username 为发送邮件的用户的邮箱名(或账号)
    • password 为授权码
      #邮件
    spring:  
      mail:
        host: smtp.sina.com     # 发送邮件的主机
        port: 465 
        username: xxx@sina.com
        password: 678152xxxxxxxx  # 这个是授权码
        protocol: smtps
        properties:
          mail:
            smtp:
              ssl:
                enable: true
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    image-20221003221348100

    编写发送邮件的统一的配置类

    package com.wjiangquan.community.util;
    
    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.mail.javamail.JavaMailSender;
    import org.springframework.mail.javamail.MimeMessageHelper;
    import org.springframework.stereotype.Component;
    
    import javax.mail.MessagingException;
    import javax.mail.internet.MimeMessage;
    
    /**
     * @author weijiangquan
     * @date 2022/10/3 -19:34
     * @Description
     */
    
    @Component
    public class MailClient {
    
    
        private static final Logger logger = LoggerFactory.getLogger(MailClient.class);
    
        @Autowired
        private JavaMailSender mailSender;
    
        @Value("${spring.mail.username}")
        private String from;  //发送的主机
    
        /**
         * 
         * @param toWho 收邮件人的邮件名
         * @param title 邮件的主题(即标题)
         * @param content (邮件的内容)
         */
        public void sendMail(String toWho,String title,String content){
            try {
                MimeMessage message = mailSender.createMimeMessage();
                MimeMessageHelper helper = new MimeMessageHelper(message);
                helper.setFrom(from);
                helper.setTo(toWho);
                helper.setSubject(title);
                helper.setText(content,true);
                mailSender.send(helper.getMimeMessage());
            } catch (MessagingException e) {
                logger.error("发送邮件失败"+e.getMessage());
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    测试类

    package com.wjiangquan.community;
    
    import com.wjiangquan.community.util.MailClient;
    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.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringRunner;
    
    
    
    /**
     * @author weijiangquan
     * @date 2022/10/3 -19:45
     * @Description
     */
    @RunWith(SpringRunner.class)
    @SpringBootTest
    @ContextConfiguration(classes = CommunityApplication.class)
    public class TestSendMail {
    
        @Autowired
        private MailClient mailClient;
    
        @Test
        public void test(){
            mailClient.sendMail("xxxxxxxxxxxx@qq.com","你好","晚上好");
        }
    
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    测试成功

    image-20221003221448865

    二 发送模板邮件

    在用户注册成功之后,需要向用户发送一封激活邮件,以确定邮箱的可靠性。在这里使用的是thymeleaf作为模板引擎

    测试代码

    //在之前的基础上需要注入如下对象
        @Autowired
        private TemplateEngine templateEngine;  //该对象是由bean容器管理的,可以直接注入
    
    @Test
        public void testTemplate(){
            Context context = new Context();
            context.setVariable("likePeople","托尔斯泰");
            String content = templateEngine.process("/mail/testDemo", context);
            System.out.println(content);
            mailClient.sendMail("xxxxxx@qq.com","hello world",content);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    模板代码

    DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>发送邮件测试title>
    head>
    <body>
        <h1>
            <span th:text="${likePeople}">span>
        h1>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    通过测试发现,这种方式邮箱没有提示,需要自己进入邮箱才可以看得到。

    • 此外由于qq邮箱的安全措施比较高,当给使用qq邮箱进行注册的用户发送邮件时,点击链接时会出现安全提示,有时导致无法点击的情况。
    • 因此建议使用其它邮箱进行注册
  • 相关阅读:
    SpringBoot-属性绑定和bean属性校验
    成都易佰特的坑——E103-W06
    146.LRU缓存
    怎么在树莓派上搭建web网站,并发布到外网可访问?
    常用的设计模式
    维格云短信模板入门教程
    啥?13行python代码实现微信推送消息?
    如何使用ARM协处理器CP15在32位ARRCH模式下操作64位寄存器)
    每日练习-7
    WebGL前言——WebGL相关介绍
  • 原文地址:https://blog.csdn.net/weixin_47994845/article/details/127711251