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

打开idea,创建一个project

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

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

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

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

java 复制代码
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文件

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 {
    @Autowired
    private 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){
      //定制复杂邮件信息对象MimeMessage
      MimeMessage message= mailSender.createMimeMessage();
      //使用MimeMessageHelper帮助类,并设置multipart多部件使用为true
      try {
          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文件添加

java 复制代码
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 {
    @Autowired
    private SendMail sendMail;  //自动注入邮件服务

    @Test
    public void testSendEmail() {
        String to = "251xxxxxxx5@qq.com";  //收件人邮件
        String subject = "验证邮件";  //邮件主题
        String text = "这是一封测试邮件,用于验证Spring Boot邮件发送功能。";  //邮件正文

        sendMail.sendSimpleEmail(to, subject, text);
    }
    @Test
    public 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);
    }
}

运行代码后看邮箱

相关推荐
阿蒙Amon几秒前
C#每日面试题-简述匿名方法
java·面试·c#
梦6502 分钟前
Vue3 计算属性 (computed) 与监听属性 (watch)
前端·javascript·vue.js
山峰哥5 分钟前
JOIN - 多表关联的魔法——3000字实战指南
java·大数据·开发语言·数据库·sql·编辑器
jghhh015 分钟前
C#中实现不同进程(EXE)间通信的方案
java·单例模式·c#
Mr.朱鹏7 分钟前
Spring Boot 配置文件加载顺序与优先级详解
java·spring boot·后端·spring·maven·配置文件·yml
m0_579146657 分钟前
Maven 编译的settings配置和pom、idea配置关系
java·maven·intellij-idea
洛阳泰山11 分钟前
一个人,一个项目,一年的坚持:关于我的 2025年 技术突围之路
java·人工智能·spring boot
虫小宝14 分钟前
企业微信API接口的Java SDK封装:可复用、可测试的工具类设计方法
java·开发语言·企业微信
hanjq_code15 分钟前
java使用阿里的easyExcel解决把excel每行的数据转成excel表格格式数据并打包成ZIP下载
java·开发语言·excel
六月June June17 分钟前
leaflet L.popup().setContent中挂载vue组件
前端·javascript·vue.js