springboot发送邮件,以及携带邮件附件简单使用
可以通过springboot官方文档中Sending Email,找到类似如下java mail的使用文档
https://docs.spring.io/spring/docs/5.1.9.RELEASE/spring-framework-reference/integration.html#mail
一、导入java mail相关starter
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
二、发送不带附件和带附件邮件
1、发送邮件逻辑,再上面的java mail文档中,可以找到不带附件的邮件和带附件的邮件使用的发送邮件的bean不同,前者为JavaMailSender,后者为JavaMailSenderImpl,但通过查看集成关系可知,JavaMailSender的实现类其实只有JavaMailSenderImpl,所以,不管两者使用的是哪个,最终发送邮件调用的都是JavaMailSenderImpl中的send方法。并且在JavaMailSenderImpl中添加了一些配置属性,比如host,username等,使用JavaMailSender是获取不到这些属性的,故我们直接注入了JavaMailSenderImpl。通过测试得知,在springboot配置中,spring.mail.properties.开头的配置都会被放入到JavaMailSenderImpl中的javaMailProperties属性中,故有些额外的邮件发送参数,我们都可以通过spring.mail.properties.additional_attribute的方式来设置,比如设置邮件接收人:spring.mail.properties.receiver=xxx@163.com,xxxx@qq.com
package com.springbootdemo.mail;
import java.io.File;
import java.util.Properties;
import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
@Component
public class MailSenderBusi {
@Resource
private JavaMailSenderImpl mailSender;
public void sendMail() {
Properties mailProperties = mailSender.getJavaMailProperties();
SimpleMailMessage msg = new SimpleMailMessage();
msg.setFrom(mailSender.getUsername());
msg.setTo(mailProperties.getProperty("receiver").split(",")); // 可以是一个数组
msg.setText("nice day");
msg.setSubject("测试");
this.mailSender.send(msg);
}
public void sendMailAttach() throws Exception {
Properties mailProperties = mailSender.getJavaMailProperties();
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(mailSender.getUsername());
helper.setTo(mailProperties.getProperty("receiver").split(","));
// 第二个参数为是否支持html,如果为true,则表示支持,此时如果写入<h2>nice</h2>,则nice会被加粗,默认为false
helper.setText("nice day2", true);
helper.setSubject("测试2");
// 或者使用new FiledataSource
FileSystemResource file = new FileSystemResource(new File("F:\\learn\\workplace\\webtest.zip"));
helper.addAttachment("webdemo.zip", file);
mailSender.send(message);
}
}
2、触发邮件发送
这里我们只是简单做测试,故简单实现了CommandLineRunner,在程序启动后就开始发送邮件。
package com.springbootdemo;
import javax.annotation.Resource;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import com.springbootdemo.mail.MailSenderBusi;
@Component
public class InitMailSender implements CommandLineRunner{
@Resource
private MailSenderBusi mailSenderBusi;
@Override
public void run(String... args) throws Exception {
mailSenderBusi.sendMail();
mailSenderBusi.sendMailAttach();
}
}
三、添加springboot配置
spring.mail.protocol=smtp spring.mail.host=smtp.mxhichina.com spring.mail.port=25 spring.mail.username=xxxxx@example.com spring.mail.password=password spring.mail.properties.receiver=xxxx@163.com,xxxxx@qq.com spring.mail.properties.mail.smtp.auth=true spring.mail.default-encoding=UTF-8
可能对于有些情况,需要使用到如下两个参数,但在本例中是不需要的
spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.starttls.required=true
通过以上简单三步,就可正常发送邮件了,具体深入的其他参数设置,后续再研究