注入依赖
jdk17
springboot3
<!-- 发送电子邮件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
application配置
server:
port: 9999 # ?????
spring:
mail:
# QQhost: smtp.exmail.qq.com
host: smtp.qq.com
# tencent mail port ??????
port: 465
# ??QQ??
username: xxxxxxxxxxxxx
password: xxxxxxxxxxxxx
test-connection: true
properties:
mail:
smtp:
ssl:
enable: true
SendMailUtils工具类
package com.mailtoqq.utils;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMailMessage;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import java.io.File;
@Component
public class SendMailUtils {
@Autowired
JavaMailSenderImpl javaMailSender;
//发送普通文字邮件
public void sendText(String Subject,String Text,String setFrom,String setTo){
SimpleMailMessage simpleMailMessage=new SimpleMailMessage();
simpleMailMessage.setSubject(Subject); //标题
simpleMailMessage.setText(Text); //内容
simpleMailMessage.setFrom(setFrom); //发送人邮件
simpleMailMessage.setTo(setTo); //发送目的地邮箱
javaMailSender.send(simpleMailMessage);
}
//发送带页面格式加文件邮件
public void sendTexts(String Subject,String Text,Boolean t,String setFrom,String setTo
,String attachmentFilename,String filePathName) throws MessagingException{
MimeMessage mimeMessage=javaMailSender.createMimeMessage();
MimeMessageHelper helper=new MimeMessageHelper(mimeMessage,true);
helper.setSubject(Subject);//标题
helper.setText(Text,t);//内容
helper.setFrom(setFrom);//发送人邮箱
helper.setTo(setTo);//目的地邮箱
helper.addAttachment(attachmentFilename,new File(filePathName)); //图片路径
javaMailSender.send(mimeMessage);
}
}
SendMailController
package com.mailtoqq.controller;
import com.mailtoqq.utils.SendMailUtils;
import jakarta.mail.MessagingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@Controller
public class SendMailController {
@Autowired
SendMailUtils sendMailUtils;
@GetMapping("/sendMail")
public String sendMail() {
String Subject = "你好,世界"; //题目
String Text = "hello World"; //内容
String From = "[email protected]"; //发送邮箱
String To = "[email protected]"; //接收邮箱
sendMailUtils.sendText(Subject, Text, From, To);
return "发送成功";
}
@GetMapping("/sendMails")
@ResponseBody
public String sendMails() throws MessagingException {
String Subject = "你好,世界";
String Text = "hello World";
String From = "[email protected]"; //发送邮箱
String To = "[email protected]"; //接收邮箱
String filename = "xxx.png"; //文件名
String filePath = "xxx.png"; //文件绝对路径
sendMailUtils.sendTexts("发送带页面格式加文件邮件测试", "<p style='color:red;'>红</p>", true, From,
To, filename, filePath);
// return "<img src=\"images/002.png\">";
return "123";
}
}
标签:String,org,mail,springframework,发送,模块,import,邮箱,com
From: https://www.cnblogs.com/yuey6670/p/18064500