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

零基础,使用Idea工具写一个邮件报警程序

打开idea,创建一个project

打开文件目录下的pom.xml文件,添加下面的内容安装依赖,等待下载完成

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

打开src->main->resources->application.properties配置文件

添加邮箱账号、密码(这里使用的是qq邮箱的授权码)

spring.application.name=my-email-app
spring.mail.host=smtp.qq.com  //指定邮箱的SMTP服务器地址
spring.mail.port=587  //SMTP服务端口号#邮箱账号
spring.mail.properties.mail.smtp.auth=true  //启用SMTP身份验证(必须为true才能发送邮件)
spring.mail.properties.mail.smtp.starttls.enable=true  //启用STARTTLS加密(确保邮件传输安全)
spring.mail.username=25xxxxxxx@qq.com  //发件人邮箱
spring.mail.password=klptaxfztbtxxxx  //邮箱授权码
spring.mail.default-encoding=utf-8  //防止中文乱码

授权码在登录qq邮箱点击设置->账号->开启服务

在src->main->项目文件里面添加一个service包

添加一个SendMail.java文件

package com.qst.springbootjava4.service;
import jakarta.mail.MessagingException;  // 添加这行
import jakarta.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
// 其他已有导入...
import java.io.File;@ComponentScan
@Service
public class SendMail {@Autowiredprivate JavaMailSenderImpl mailSender;@Value("${spring.mail.username}")private String from;public void sendSimpleEmail(String to,String subject,String text){//定制纯文本邮件信息对象SimpleMailMessage message=new SimpleMailMessage();message.setFrom(from);message.setTo(to);message.setSubject(subject);message.setText(text);//发送邮件mailSender.send(message);try{mailSender.send(message);System.out.print("成功");}catch (MailException e){System.out.print("失败");}}//复杂的邮件发送public void  sendCompexEmail(String to,String subject,String text,String filePath){//定制复杂邮件信息对象MimeMessageMimeMessage message= mailSender.createMimeMessage();//使用MimeMessageHelper帮助类,并设置multipart多部件使用为truetry {MimeMessageHelper helper=new MimeMessageHelper(message,true);helper.setFrom(from);helper.setTo(to);helper.setSubject(subject);helper.setText(text);//设置邮件附件FileSystemResource file=new FileSystemResource(new File(filePath));String fileName=filePath.substring(filePath.lastIndexOf(File.separator));//发送邮件helper.addAttachment(fileName,file);mailSender.send(message);} catch (MessagingException e) {e.printStackTrace();}}}

打开text文件添加

package com.qst.springbootjava4;import com.qst.springbootjava4.service.SendMail;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
class Springbootjava4ApplicationTests {@Autowiredprivate SendMail sendMail;  //自动注入邮件服务@Testpublic void testSendEmail() {String to = "251xxxxxxx5@qq.com";  //收件人邮件String subject = "验证邮件";  //邮件主题String text = "这是一封测试邮件,用于验证Spring Boot邮件发送功能。";  //邮件正文sendMail.sendSimpleEmail(to, subject, text);}@Testpublic void test02(){String to = "251xxxxxxx@qq.com";  //收件人String subject = "测试";  //主题String text="这是一个文件";  //正文String filePath="C:\\Users\\Administrator\\Desktop\\新建 DOC 文档.doc";  //附件路径sendMail.sendCompexEmail(to,subject,text,filePath);}
}

运行代码后看邮箱

http://www.lryc.cn/news/581685.html

相关文章:

  • Solidity——什么是状态变量
  • 计算机网络:(七)网络层(上)网络层中重要的概念与网际协议 IP
  • Kafka “假死“现象深度解析与解决方案
  • UI前端大数据可视化进阶:交互式仪表盘的设计与应用
  • 数据驱动实时市场动态监测:让商业决策跑赢时间
  • 【LeetCode 热题 100】240. 搜索二维矩阵 II——排除法
  • 黑马点评系列问题之实战篇02短信登录 利用资料中的mysql语句创建数据表时报错
  • 关于 栈帧变化完整流程图(函数嵌套)
  • Java 双亲委派机制笔记
  • QML 使用QtObject定义私有变量
  • 基于Flask和机器学习开发的米其林餐厅数据可视化平台
  • 单片机:STM32F103的开发环境搭建
  • 单片机物联网应用中的 Pogopin、串口与外围模组通信技术解析
  • ABP VNext + Tye:本地微服务编排与调试
  • 基于udev规则固定相机名称
  • [netty5: WebSocketServerHandshaker WebSocketServerHandshakerFactory]-源码分析
  • 桥梁桥拱巡检机器人cad+【4张】设计说明书+绛重+三维图
  • 力扣 hot100 Day36
  • webUI平替应用,安装简单,功能齐全
  • LeetCode 75. 颜色分类(荷兰国旗问题)
  • 服务端向客户端主动推送数据的几种方法(Spring Boot 环境)
  • 11.进程间通信
  • VSCode+arm-none-eabi-gcc交叉编译+CMake构建+OpenOCD(基于Raspberry Pico RP2040)
  • 2.线性神经网络--Softmax回归
  • 算法分析与设计实验1:实现两路合并排序和折半插入排序
  • 3.8 java连接数据库
  • Vue2 day07
  • 工业相机和镜头
  • 基于Java+SpringBoot的医院信息管理系统
  • ARM 学习笔记(一)