Spring Boot 配置并发送邮件
1.引入依赖,在 pom.xml 文件中引入邮件配置:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2.配置文件
spring.mail.host=smtp.qq.com
spring.mail.username=xxxxxx@qq.com
spring.mail.password=xxxxxx
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
使用QQ邮箱发送邮件,spring.mail.username为你的qq邮箱,需要将spring.mail.password改为QQ邮箱的授权码。
QQ邮箱->设置->账户->POP3/SMTP服务:开启服务后会获得QQ的授权码
3.代码实现
3.1新建一个类,用来告知收件人、主题、内容、附件等
public class EmailInfo {
private String from; //发件人(获取配置文件中的发送人信息)
private String to; //收件人
private String subject; //邮件主题
private String content; //邮件内容,(包括html)
private List<String> attachPath; //附件路径
//getter and setter
}
3.2发送邮件的服务类(3个方法–1发送简单文本邮件、2发送Html邮件、3发送带附件邮件)
@Autowired
private JavaMailSender mailSender; //自动注入的Bean
@Value("${spring.mail.username}")
private String Sender; //读取配置文件中的参数 -- 发件人
/**
* 发送简单文本邮件
* @param emailModel
*/
public void sendSimpleMail(EmailInfo emailModel){
try {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(Sender);
message.setTo(emailModel.getTo());
message.setSubject(emailModel.getSubject());
message.setText(emailModel.getContent());
mailSender.send(message);
}catch (Exception e){
e.printStackTrace();
}
}
/**
* 发送Html邮件
* @param emailModel
*/
public void sendHtmlMail(EmailInfo emailModel){
MimeMessage message = null;
try {
message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(Sender);
helper.setTo(emailModel.getTo());
helper.setSubject(emailModel.getSubject());
//StringBuffer sb = new StringBuffer();
//sb.append("<h1>大标题-h1</h1>").append("<p style='color:#F00'>红色字</p>").append("<p style='text-align:right'>右对齐</p>");
//helper.setText(sb.toString(), true);
helper.setText(emailModel.getContent(), true);
} catch (Exception e) {
e.printStackTrace();
}
mailSender.send(message);
}
/**
* 发送带附件的邮件
* @param emailModel
*/
public void sendAttachmentsMail(EmailInfo emailModel){
MimeMessage message = null;
try {
message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(Sender);
helper.setTo(emailModel.getTo());
helper.setSubject(emailModel.getSubject());
helper.setText(emailModel.getContent()); //发送普通邮件,发送html邮件要加一个boolean参数
//注意项目路径问题,自动补用项目路径
if(emailModel.getAttachPath() != null){
for (String path : emailModel.getAttachPath()){
//FileSystemResource file = new FileSystemResource(new File("src/main/resources/static/image/picture.jpg"));
FileSystemResource file = new FileSystemResource(new File(path));
//加入邮件
helper.addAttachment(file.getFilename(), file);
}
}
} catch (Exception e){
e.printStackTrace();
}
mailSender.send(message);
}
3.3运行测试发送邮件 ,在 ApplicationTests 中测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class EmailApplicationTests {
@Autowired
private SendEmailService sendEmailService;
//发送简单文本邮件
@Test
public void sendSimpleEmail(){
EmailInfo model = new EmailInfo();
model.setTo("[email protected]");
model.setSubject("spring boot test send text email "+ new Date());
model.setContent("test send email ,content is date :" + new Date());
sendEmailService.sendSimpleMail(model);
}
//发送html邮件
@Test
public void sendHtmlEmail(){
EmailInfo model = new EmailInfo();
model.setTo("[email protected]");
model.setSubject("spring boot test send html email "+ new Date());
StringBuffer html = new StringBuffer();
html.append("<h1>大标题-h1</h1>").append("<p style='color:#F00'>test红色字</p>").append("<p style='text-align:right'>右对齐</p><div>恭喜您,邮件发送成功</div>");
model.setContent(html.toString());
sendEmailService.sendHtmlMail(model);
}
@Test
public void sendAttachmentsMail(){
EmailInfo model = new EmailInfo();
model.setTo("[email protected]");
model.setSubject("spring boot test send attach email "+ new Date());
model.setContent("test send email ,content is date :" + new Date());
List<String> attachPath = new ArrayList<>();
attachPath.add("src/main/resources/static/image/1.jpg");
attachPath.add("src/main/resources/static/image/2.jpg");
model.setAttachPath(attachPath);
sendEmailService.sendAttachmentsMail(model);
}
}
3.4测试结果:发送和接收都 successful