当前位置: 首页 > news >正文

java实现邮件发送

使用spring boot框架

pom.xml

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

yaml配置

spring:# 邮件,置以SSL的方式发送, 这个需要使用这种方式并且端口是465mail:host: smtp.exmail.qq.comport: 465username: aaa@bbb.compassword: 123456test-connection: falseproperties:mail:smtp:auth: truessl:enable: truesocketFactory:class: com.sun.mail.util.MailSSLSocketFactoryfallback: falsessl:enabled: true

host、username、password参数,根据自己的实际情况更改一下。

邮件发送实现类 MailService.java


import jakarta.annotation.Resource;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;;
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 java.io.File;
import java.util.List;/**** 发送邮件:*/
@Slf4j
@Component
public class MailService {@Resourceprivate JavaMailSender javaMailSender;@Value("${spring.mail.username}")private String clientMail;/*** 发送邮件** @param subject          主题* @param content          内容* @param fileList         文件* @param receiverUserList 接收方* @throws MessagingException*/public void sendMail(String subject, String content, List<File> fileList, List<String> receiverUserList, boolean isHtml) throws MessagingException {if (CollectionUtils.isEmpty(receiverUserList)) {throw new RuntimeException("接收方不能为空");}if (StringUtils.isBlank(content)) {throw new RuntimeException("邮件内容不能为空");}MimeMessage mimeMessage = javaMailSender.createMimeMessage();//是否为多文件上传boolean multiparty = !CollectionUtils.isEmpty(fileList);MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, multiparty);helper.setFrom(clientMail);helper.setTo(receiverUserList.toArray(new String[0]));helper.setSubject(subject);//发送html格式helper.setText(content, isHtml);//附件if (multiparty) {for (File file : fileList) {helper.addAttachment(file.getName(), file);}}javaMailSender.send(mimeMessage);}}

发送邮件测试类 

emailSendTest.java


import jakarta.annotation.Resource;
import net.lab1024.sa.base.module.support.mail.MailService;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;/*** 邮件发送测试*/
@SpringBootTest
public class emailSendTest {@Resourceprivate MailService mailService;@Testpublic void sendEmail() {System.out.println("=== 开始 发送测试邮件 ===");try {// 发送邮件HashMap<String, Object> mailParams = new HashMap<>();mailParams.put("massage", "测试内容");List<String> to = new ArrayList<>();to.add("11111@qq.com");mailService.sendMail("订单支付提醒","请尽快支付2025-09账单", null, to,false);} catch (Exception e) {System.err.println("测试过程中发生异常: " + e.getMessage());e.printStackTrace();}System.out.println("=== 结束 发送测试邮件 ===");}}
http://www.dtcms.com/a/419234.html

相关文章:

  • 网站建设部署与发布有效期手机上管理wordpress
  • 南宁网站建设咨q479185700上墙域名服务商网站
  • 高端网站制wordpress缩略图支持外链图
  • 雷卯针对米尔MYC-CZU3EG 4EV 5EV-V2 开发板防雷防静电方案
  • NLP算法岗位面试题分享:Zero-Shot、One-Shot与Few-Shot学习
  • 移动网站建设自助建站网络推广怎么学
  • 宝山网站建设宝山怎么做自己的网站卖东西
  • Spring Boot 启动时将数据库数据预加载到 Redis 缓存
  • Nginx 502 Bad Gateway从 upstream 日志到 FastCGI 超时深度复盘
  • NLP自然语言处理性能评估指标
  • 零基础从头教学Linux(Day 43)
  • 网站后期维护协议企业网站建立哪
  • k8s 兼容摩尔线程
  • 网站建设人员工作计划网站定制设计价目表
  • RKD论文阅读
  • 导航类网站模板自己怎么做一个企业官网
  • 广东平台网站建设制作青岛网站设计怎么选
  • 如何破除迷信思维掌握ROS1/ROS2机器人学习的唯物主义
  • 桌面版exe安装和Python命令行安装2种方法详细讲解图片去水印AI源码私有化部署Lama-Cleaner安装使用方法-优雅草卓伊凡
  • C++聊天系统从零到一:CMake构建系统-企业级C++项目的构建利器
  • 折扣影票api?如何选择对接渠道?
  • 移动网站优化宁波企业网站制作公司
  • Oracle 闪回过期后的解决方法
  • 慧博云通受邀参加全球数字贸易博览会,两大出海案例入选“数贸故事”
  • 暴雨山洪灾害的发生与防治虚拟仿真实验
  • 【精品资料鉴赏】400页可编辑word 软件系统通用技术方案及实施方案
  • 学网站开发要多少钱高端品牌手机有哪些
  • 阿里网站怎么建设苏州网站制作排名优化
  • List容器(上)实战探索解析
  • 旅游做的视频网站二手网站排名