springboot实现给邮箱发送信息(邮件任务)
开发工具与关键技术:mailSender/IntelliJ IDEA
作者:老龙
撰写时间:2020/08/09
本文章基于springboot开发,开发工具IntelliJ IDEA ,话不多说,直接上干货
1.pom.xml导入mailSender的启动器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
点击组件进去看,发现其实主要是依赖jakarta.mail这个jar包
接下来是配置属性文件,以properties为例(这里使用的是QQ邮箱)
spring.mail.username=3441817786@qq.com
spring.mail.password=msisilccohrwfiih
#QQ邮箱smtp.qq.com,163邮箱smtp.163.com,以此类推
spring.mail.host=smtp.qq.com
#开启加密验证,QQ邮箱需要,其他不需要
spring.mail.properties.mail.smtp.ssl.enable=true
spring.mail.password的密码是QQ邮箱SMTP加密过的,需要手动开启(因为使用明文密码贼危险,你可以使用明文的)
设置---->账号---->smtp
接下来先写一封最简单的邮件
import org.springframework.core.io.ByteArrayResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
@SpringBootTest
class Springboot09TestApplicationTests {
@Autowired
JavaMailSenderImpl mailSender;
@Test
//一个简单的邮件
void contextLoads() {
SimpleMailMessage mailMessage = new SimpleMailMessage();
//邮件主题
mailMessage.setSubject("java你好");
//邮件内容
mailMessage.setText("ok,邮件任务走起!");
//发送到。。。。
mailMessage.setTo("xxxxxxxx@qq.com");
//谁发的。。。。
mailMessage.setFrom("3441817786@qq.com");
mailSender.send(mailMessage);
}
}
下面是一封相对复杂的邮件
@Test
void mailMessage() throws MessagingException, IOException {
//一个复杂的邮件
MimeMessage message= mailSender.createMimeMessage();
//组装 multipart开启多文件上传
MimeMessageHelper helper=new MimeMessageHelper(message,true);
//正文
helper.setSubject("来自XX的邮件");
//html:true 开启html文本识别
helper.setText("<div style=\"color: red\">这是一封复杂的邮件</div>",true);
//添加附件
helper.addAttachment("5.jpg",new File("C:\\Users\\JiaLong\\Pictures\\七濑胡桃\\5.jpg"));
helper.addAttachment("33.jpg",new File("C:\\Users\\JiaLong\\Pictures\\七濑胡桃\\33.jpg"));
helper.addAttachment("音乐.mp3",new File("C:\\Users\\JiaLong\\Music\\BGM\\Alan Walker - Routine [mqms2].mp3"));
helper.setTo("xxxxxxx@qq.com");
helper.setFrom("3441717786@qq.com");
mailSender.send(message);
}
注意:helper.addAttachment添加附件时,文件的附件名字要带文件后缀,不然传到邮箱的就是一个object文件,还有这是需要联网的,这是常识。。。。。。
上面添加附件时使用new 一个File文件,找到它的路径进行发送的,下面是接收IO流的形式发送
@Autowired
private IOUtils ioUtils;
@Test
void mailMessage() throws MessagingException, IOException {
//一个复杂的邮件
MimeMessage message= mailSender.createMimeMessage();
//组装 multipart开启多文件上传
MimeMessageHelper helper=new MimeMessageHelper(message,true);
//正文
helper.setSubject("来自XX的邮件");
//html:true 开启html文本识别
helper.setText("<div style=\"color: red\">这是一封复杂的邮件</div>",true);
File file= new File("C:\\Users\\JiaLong\\Pictures\\沐.jpg");
InputStream inputStream=new FileInputStream(file);
//添加附件
helper.addAttachment("这是一张沐浴的图片.jpg",new ByteArrayResource(ioUtils.toByteArray(inputStream)));
helper.setTo("2955564688@qq.com");
helper.setFrom("1147174244@qq.com");
import org.springframework.stereotype.Component;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
@Component
public class IOUtils {
public byte[] toByteArray(InputStream file) throws IOException {
ByteArrayOutputStream out=new ByteArrayOutputStream();
int len=1024;
int i;
byte[] bytes=new byte[len];
while ((i=file.read(bytes,0,len))>0){
out.write(bytes,0,i);
}
return out.toByteArray();
}
}
想了解更多的话可以点击源码进行了解,就很有趣