发送邮件(三)


一 发送邮件小例子

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

image-20221003193312865

spring整合发送邮件。

  • 使用的springboot的版本是2.1,因此使用的spring-boot-starter-mail版本要springboot相对应
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-mail -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-mail</artifactId>
			<version>2.1.5.RELEASE</version>
		</dependency>

配置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

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());
        }
    }
}

测试类

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","你好","晚上好");
    }


}

测试成功

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);
    }

模板代码

<!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>

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

  • 此外由于qq邮箱的安全措施比较高,当给使用qq邮箱进行注册的用户发送邮件时,点击链接时会出现安全提示,有时导致无法点击的情况。
  • 因此建议使用其它邮箱进行注册
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

莪假裝堅強

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值