1、发送简单邮件
导入依赖
implementation 'org.springframework.boot:spring-boot-starter-mail:3.0.2'
开启相关协议,获取密码~ 我是用的是QQ邮箱,其他的也一样
配置一下mail的相关信息
spring.mail.username=你的[email protected]
spring.mail.password=你的密码
spring.mail.host=smtp.qq.com
# 接收人的邮箱
[email protected]
编写发送代码
package com.qbb.mail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;
/**
* @author startqbb (个人博客:https://www.cnblogs.com/qbbit)
* @date 2023-02-08 21:34
* @tags 喜欢就去努力的争取
*/
@Component
public class SendMail {
@Autowired
private JavaMailSender javaMailSender;
@Value("${spring.mail.username}")
private String sender;
@Value("${mail.receiver}")
private String receiver;
private String title = "SpringBoot发送邮件";
private String body = "你好呀,我的宝宝~";
public void send() {
SimpleMailMessage mimeMessages = new SimpleMailMessage();
/**
* 需要传递四个信息
* 1、发送人
* 2、接收人
* 3、标题
* 4、主体信息
*/
mimeMessages.setFrom(sender+("哈哈哈"));
mimeMessages.setTo(receiver);
mimeMessages.setSubject(title);
mimeMessages.setText(body);
javaMailSender.send(mimeMessages);
System.out.println("send ......");
}
}
2、发送复杂邮件(图片、附件...)
其实就是换一个高级点的对象
package com.qbb.mail;
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.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import java.io.File;
/**
* @author startqbb (个人博客:https://www.cnblogs.com/qbbit)
* @date 2023-02-08 21:53
* @tags 喜欢就去努力的争取
*/
@Component
public class SendComplexMail {
@Autowired
private JavaMailSender javaMailSender;
@Value("${spring.mail.username}")
private String sender;
@Value("${mail.receiver}")
private String receiver;
private String title = "SpringBoot发送复杂邮件";
//private String body = "你好呀,我的宝宝,我用代码给我发消息 。 <a href='https://www.baidu.com'>点开有惊喜</a>";
private String body = "你好呀,我的宝宝,我用代码给我发消息 。 <img src='https://nimg.ws.126.net/?url=http%3A%2F%2Fdingyue.ws.126.net%2F2021%2F1028%2Fba8af181j00r1nr5e002lc000hs011xc.jpg&thumbnail=660x2147483647&quality=80&type=jpg'/>";
public void send() {
try {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
/**
* 需要传递四个信息
* 1、发送人
* 2、接收人
* 3、标题
* 4、主体信息
*/
//MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
// 第二个参数,开启发送附件
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
helper.setFrom(sender);
helper.setTo(receiver);
helper.setSubject(title);
// 第二个参数,用于开启解析html标签
helper.setText(body, true);
// 发送附件
File f1 = new File("E:\\JavaProject\\study_project\\springboot-javamail\\build\\libs\\springboot-javamail-0.0.1-SNAPSHOT-plain.jar");
File f2 = new File("E:\\JavaProject\\study_project\\springboot-javamail\\src\\main\\resources\\static\\avatar.ico");
helper.addAttachment(f1.getName(), f1);
helper.addAttachment("avatar.jpg", f2);
javaMailSender.send(mimeMessage);
System.out.println("send ......");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
标签:SpringBoot,org,springframework,private,JavaMail,整合,import,mail,String
From: https://www.cnblogs.com/qbbit/p/17103532.html