打開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);}
}
運行代碼后看郵箱