Spring boot发送邮件过程及问题整理

最开始参考的是网上的普遍教程,用的qq邮箱,但是qq邮箱及其不稳定。
有时候发送成功,有时候不能发送成功。
而且今天在运行的时候,突然崩了,我去qq邮箱查看了一下,显示不能找到qq邮箱服务器ip。
果断放弃了qq邮箱,转用了163邮箱,非常nice!!

贴一个今天的报错信息

Error creating bean with name 'org.springframework.boot.autoconfigure.mail.MailSenderValidatorAutoConfiguration': 
Bean instantiation via constructor failed; 
nested exception is org.springframework.beans.BeanInstantiationException: 
Failed to instantiate [org.springframework.boot.autoconfigure.mail.MailSenderValidatorAutoConfiguration]: Constructor threw exception; nested exception is java.lang.IllegalStateException: Mail server is not available

使用springboot发送邮件

更改邮箱配置

首先更改邮箱配置
在这里插入图片描述
在这里插入图片描述
开启下面箭头所指地方,它会让你发送一个短信,然后给你一串字符,保存一下这串字符,它是你springboot连接邮箱的登录密码。
在这里插入图片描述

Spingboot pom.xml文件添加依赖

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

Springboot .properties文件配置

#邮箱配置
###
#smtp服务主机  qq邮箱则为smtp.qq.com
spring.mail.host=smtp.163.com
#端口
spring.mail.port=465
#服务协议
spring.mail.protocol=smtp
#编码集
spring.mail.default-encoding=UTF-8
#发送邮件的账户
spring.mail.username=x1692760724@163.com
#授权码
spring.mail.password=TXVMCBZOMAYCXADM
spring.mail.test-connection=true
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.ssl.enable=true
spring.mail.properties.mail.debug=true

Controller层

package com.example.homework.controller;

import com.example.homework.entity.*;
import com.example.homework.service.AdminService;
import com.example.homework.service.StudentService;
import com.example.homework.service.TeacherService;
import com.example.homework.util.VerCodeGenerateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/common")
public class CommonAjaxController {
    @Value("${spring.mail.username}")
    private String from;
    @Resource
    private JavaMailSender mailSender;
   
    @RequestMapping(value = "/sendEmail",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> commonEmail(String id, String email,Integer role, Model model) {
        String userName="小明";
        String userEmail="123@qq.com";
        // 创建邮件消息
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(userEmail);
        message.setSubject("验证码");
        String verCode = VerCodeGenerateUtil.generateVerCode();
        message.setText("尊敬的"+userName+",您好:\n"
                + "\n本次请求的邮件验证码为:" + verCode + ",本验证码 5 分钟内效,请及时输入。(请勿泄露此验证码)\n"
                + "\n如非本人操作,请忽略该邮件。\n(这是一封通过自动发送的邮件,请不要直接回复)");
        mailSender.send(message);
        Map<String,Object> mp=new HashMap<>();
        mp.put("success",verCode);
        mp.put("message","验证码已发送至邮箱,请注意查收!");
        return mp;
    }

}


生成验证码util代码

package com.example.homework.util;

import java.security.SecureRandom;
import java.util.Random;

// 随机生成6位验证码
public class VerCodeGenerateUtil {
    // 验证码包含的字段,可自己设置
    private static final String SYMBOLS = "0123456789ABCDEFGHIGKLMNOPQRSTUVWXYZ";
    private static final Random RANDOM = new SecureRandom();
    // 生成 6 位数的随机数字
    public static String generateVerCode() {
        // 如果是六位,就生成大小为 6 的数组
        char[] numbers = new char[6];
        for (int i = 0; i < numbers.length; i++) {
            numbers[i] = SYMBOLS.charAt(RANDOM.nextInt(SYMBOLS.length()));
        }
        return new String(numbers);
    }
}



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 Spring Boot邮件发送功能来发送电子邮件。首先,你需要在项目的依赖中添加 Spring Boot邮件依赖。在 `pom.xml` 文件中添加以下代码: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> ``` 接下来,你需要在配置文件中配置邮件相关的属性。在 `application.properties`(或 `application.yml`)文件中添加以下属性: ```properties # 邮件服务器主机名 spring.mail.host=your-mail-host # 邮件服务器端口 spring.mail.port=your-mail-port # 邮件发送者用户名 spring.mail.username=your-username # 邮件发送者密码 spring.mail.password=your-password # 邮件发送者地址 spring.mail.from=your-email-address ``` 现在,你可以在你的代码中使用 `JavaMailSender` 接口来发送邮件。你可以注入 `JavaMailSender` 接口的实例,并使用 `send()` 方法发送邮件。以下是一个简单的示例: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.stereotype.Service; @Service public class EmailService { @Autowired private JavaMailSender javaMailSender; public void sendEmail(String to, String subject, String text) { SimpleMailMessage message = new SimpleMailMessage(); message.setTo(to); message.setSubject(subject); message.setText(text); javaMailSender.send(message); } } ``` 你可以在需要发送邮件的地方调用 `sendEmail()` 方法,并传入收件人地址、邮件主题和邮件内容。 这是使用 Spring Boot 发送邮件的基本步骤。你可以根据自己的需求进行进一步的定制和配置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值