SpringBoot 实现发邮箱

163网易邮箱发邮件的服务

首先你需要在163开通一下服务

开通之后会显示授权码,拿好了别丢了

Spring配置文件

到SpringBoot里进行配置

java 复制代码
spring.mail.host=smtp.163.com
# 填你的邮箱↓
spring.mail.username=15032895039@163.com
# 填你的授权码
spring.mail.password=haorenyishenpinan
spring.mail.port=25
spring.mail.protocol=smtp
spring.mail.default-encoding=UTF-8

除了邮箱和授权码其实别的地方都一样。

pom里添加依赖

xml 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
    <version>3.0.0</version>
</dependency>

编写后端处理请求->发送右键的代码

配好之后,可以写一个接收发邮箱的请求

这里为了简单写,就把目标邮箱什么的都写在后端了

controller

java 复制代码
package com.example.demo.mail;

import jakarta.mail.MessagingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/email")
public class EmailController {

    @Autowired
    private EmailService emailService;

    @GetMapping("/send")
    public String sendEmail() {
        try {
        	// 第一个参数是目标邮箱,就是你要发到哪儿,可以填自己qq邮箱
            emailService.sendEmail("3131015733@qq.com", "测试发送邮箱服务", "5873");
            return "Email sent successfully to " + "3131015733@qq.com";
        } catch (MessagingException e) {
            return "Error sending email: " + e.getMessage();
        }
    }
}
java 复制代码
package com.example.demo.mail;

import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;



@Service
public class EmailService {

    private final MailSender mailSender;

    public EmailService(MailSender mailSender) {
        this.mailSender = mailSender;
    }

    public void sendEmail(String to, String subject, String text) throws MessagingException {
        System.out.println("尝试发送邮箱");
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("15032895039@163.com");
        message.setTo(to);
        message.setSubject(subject);
        message.setText(text);
        mailSender.send(message);
        System.out.println("发送成功");
    }
}

测试

在浏览器上输入

复制代码
localhost:8080/email/send

就ok了

相关推荐
文丑颜不良啊1 小时前
LeetCode3401~3420题目解析
java·leetcode
文丑颜不良啊2 小时前
LeetCode3421~3440题目解析
java·leetcode
code2cat3 小时前
【随笔】Java客户端选了HTTP/3,怎样知道请求实际走了哪个版本
java·开发语言·http
码云社区4 小时前
Java在线租房系统源码:VR看房素材、房源图片与内容维护方案
java·vue.js·vr·租房小程序·在线租房·看房租房
码云社区5 小时前
Spring Boot智慧物业系统源码:合同到期提醒、续签资料与资产协作
java·spring boot·后端·物业管理·物业系统·智慧物业
AI开源软件5 小时前
Vue3+SpringBoot3 业务配置统一管理:接管、保存与回退怎样保留有效值
java·前端·vue.js·spring boot·vue·ruoyi office
陈如水5 小时前
Spring AI Alibaba 在多 Agent 场景下的实践
java·人工智能·spring
杨运交6 小时前
091-功能模块-API Key 管理功能模块解析:feature-sa-token 架构设计与数据库持久化
spring boot
行百里er6 小时前
【Spring Insight 技术分享】如何给 AI 接口调用做滑动窗口限流
spring boot·后端·spring cloud
有个傻瓜6 小时前
一次「查到别家门店订单」的线上事故:Spring Boot 3 + MyBatis-Plus 多租户隔离踩坑复盘
spring boot·mybatis-plus·多租户·ruoyi-vue-plus·线上故障复盘