邮件发送系统,Springboot发送邮件通知(163邮箱为例)

2025-10-26
网站建设限时活动促销

1.前言

在实际项目中,经常需要用到邮件通知功能。比如,用户通过邮件注册,通过邮件找回密码等;又比如通过邮件发送系统情况,通过邮件发送报表信息等等,实际应用场景很多。这篇文章,和大家分享使用实现一个发送邮件的功能。(如下功能就是使用了消息推送)

2.开通POP3服务

什么是POP3、SMTP和IMAP?

2.1 PC端登录163邮箱,点击设置按钮,找到POP3/SMTP/IMAP,需要开启它,如图:

3.代码实现过程

3.1 引入依赖

  org.springframework.boot spring-boot-starter-mail 

3.2 配置文件

spring: mail: host: smtp.163.com username: 登录用户名 password: 刚刚自己设置的授权码 default-encoding: utf-8 properties: mail: smtp: auth: true starttls: enable: true required: true

3.3 实现类代码

package com.style.service.impl;import com.style.model.dto.MailDTO;import com.style.service.MailService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.mail.MailSender;import org.springframework.mail.SimpleMailMessage;import org.springframework.stereotype.Component;import org.springframework.stereotype.Service;@Service@Componentpublic class MailServiceImpl implements MailService { @Autowired private MailSender mailSender; @Override public void send(MailDTO mailDTO) { // new 一个简单邮件消息对象 SimpleMailMessage message = new SimpleMailMessage(); // 和配置文件中的的username相同,相当于发送方 message.setFrom("你自己的@163.com"); // 收件人邮箱 message.setTo(mailDTO.getMail()); // 标题 message.setSubject(mailDTO.getTitle()); // 正文 message.setText(mailDTO.getContent()); // 发送 mailSender.send(message); }}

3.4 入参DTO

package com.style.model.dto;import lombok.Data;import javax.validation.constraints.NotEmpty;import java.io.Serializable;@Datapublic class MailDTO implements Serializable { @NotEmpty(message = "邮箱不可为空") private String mail; @NotEmpty(message = "邮箱标题不可为空") private String title; @NotEmpty(message = "内容不可为空") private String content;}

4.启动服务,使用调用接口

请求成功

4.5 查看伙伴邮箱是否成功接收

本篇博客分享的是很简单的demo,项目中还要根据具体业务需求进行推送邮件。

标签: 邮件 发送 系统 Springboot 通知 邮箱 为例

本文地址:https://www.lifejia.cn/news/48764.html

免责声明:本站内容仅用于学习参考,信息和图片素材来源于互联网,如内容侵权与违规,请联系我们进行删除,我们将在三个工作日内处理。联系邮箱:cloudinto#qq.com(把#换成@)