package com.tj.ustb.hellodemo.util;
import com.tj.ustb.hellodemo.entity.EmailProperties;
import jakarta.mail.*;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
import java.util.Properties;
/**
* @description:
* @author: zyl
* @date:
*/
public class MailUtil {
/**
* 发送邮件
* @param emailProperties 发件人信息(发件人邮箱,发件人授权码)及邮件服务器信息(邮件服务器域名,身份验证开关)
* @param to 收件人邮箱
* @param title 邮件标题
* @param content 邮件正文
* @return
*/
public static boolean sendMail(EmailProperties emailProperties, String to, String title, String content){
MimeMessage message = null;
try {
Properties properties = new Properties();
properties.put("mail.smtp.host", emailProperties.getHost());
properties.put("mail.smtp.auth",emailProperties.isAuth());
properties.put("mail.user", emailProperties.getUser());
properties.put("mail.password", emailProperties.getCode());
// 构建授权信息,用于进行SMTP进行身份验证
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(emailProperties.getUser(), emailProperties.getCode());
}
};
// 使用环境属性和授权信息,创建邮件会话
Session mailSession = Session.getInstance(properties, authenticator);
// 创建邮件消息
message = new MimeMessage(mailSession);
}catch (Exception e){
e.printStackTrace();
}
//如果邮件创建失败,直接返回
if (message==null){
return false;
}
try {
// 设置发件人
InternetAddress form = new InternetAddress(emailProperties.getUser());
message.setFrom(form);
// 设置收件人
InternetAddress toAddress = new InternetAddress(to);
message.setRecipient(Message.RecipientType.TO, toAddress);
// 设置邮件标题
message.setSubject(title);
// 设置邮件的内容体
message.setContent(content, "text/html;charset=UTF-8");
// 发送邮件
Transport.send(message);
}catch (Exception e){
e.printStackTrace();
}
return true;
}
}
获取配置文件内容练习-发邮件.zip
需积分: 0 10 浏览量
2024-05-13
16:56:31
上传
评论
收藏 7KB ZIP 举报
m0_64676694
- 粉丝: 0
- 资源: 1
最新资源
- 基于yolov3+python实现的计算机视觉的交通场景智能应用,现道路目标如人、车牌、交通灯等物体的识别+数据权重+测试视频
- 基于C++和Yolo5检测和React前端开发的人流量检测系统源码+文档说明+详细注释(高分项目)
- C#毕业设计-跟踪机器人运动坐标并可视化路径轨迹源码+数据库
- 基于浏览器JS做路径跟踪渲染+源代码+界面截图
- 基于USV路径跟踪LOS控制算法matlab仿真源码+详细注释(下载直接使用)(高分项目)
- CentOS7的docker镜像
- 基于C++和Pure-pursuit算法实现的路径跟踪和给予LQR的轨迹跟踪+源代码+文档说明+脚本(高分项目)
- 基于python实现的路径跟踪控制实现的项目源代码+文档说明(高分课程设计)
- 基于神经网络的虚假评论识别系统(Python源码+文档资料+数据集+代码流程说明文档+详细注释)
- 科大讯飞开发者大赛锂离子电池生产参数调控及生产温度预测挑战赛记录python源码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈