基于spring boot发送邮件
Spring提供了非常好用的 JavaMailSender接口实现邮件发送。只需引入依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
以qq邮箱为例,在 application.properties 中加入如下配置:
spring.mail.host=smtp.qq.com
spring.mail.password=xxx
spring.mail.default-encoding=UTF-8
spring.mail.port=465
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.debug=true
PS:
spring.mail.password=这里填写授权码
[email protected]:自己的邮箱
实现:
package com.tony.demo.service.impl; import com.tony.demo.service.SendMailService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.FileSystemResource; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Component; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.File; /** * @author tony.lu */ @Component public class SendMailServiceImpl implements SendMailService { Logger LOGGER= LoggerFactory.getLogger(SendMailServiceImpl.class); @Autowired private JavaMailSender javaMailSender; @Value("${mail.from.add}") private String fromAddr; @Override public void sendSimpleMail(String to, String subject, String content) { LOGGER.info("发送简单邮件,to:{},subject:{},content:{}",to,subject,content); SimpleMailMessage simpleMailMessage=new SimpleMailMessage(); simpleMailMessage.setFrom(fromAddr); simpleMailMessage.setTo(to); simpleMailMessage.setSubject(subject); simpleMailMessage.setText(content); try { javaMailSender.send(simpleMailMessage); LOGGER.info("邮件发送成功"); }catch (Exception e){ LOGGER.error("邮件发送失败",e); } } @Override public void sendAttachMail(String to, String subject, String content, String filePath) { LOGGER.info("发送带附件的邮件,to:{},subject:{},content:{},filePath:{}",to,subject,content,filePath); MimeMessage mimeMessage = javaMailSender.createMimeMessage(); try { MimeMessageHelper messageHelper=new MimeMessageHelper(mimeMessage,true); messageHelper.setFrom(fromAddr); messageHelper.setTo(to); messageHelper.setSubject(subject); messageHelper.setText(content); FileSystemResource file=new FileSystemResource(filePath); String fileName=filePath.substring(filePath.lastIndexOf(File.separator)); messageHelper.addAttachment(fileName,file); javaMailSender.send(mimeMessage); LOGGER.info("邮件发送成功"); } catch (MessagingException e) { LOGGER.error("邮件发送失败",e); } } @Override public void sendInlineResourceMail(String to, String subject, String content, String filePath, String pid) { LOGGER.info("发送静态资源的邮件,to:{},subject:{},content:{},filePath:{},rcid:{}",to,subject,content,filePath,pid); MimeMessage mimeMessage = javaMailSender.createMimeMessage(); try { MimeMessageHelper messageHelper=new MimeMessageHelper(mimeMessage,true); messageHelper.setFrom(fromAddr); messageHelper.setTo(to); messageHelper.setSubject(subject); messageHelper.setText(content,true); FileSystemResource file=new FileSystemResource(filePath); messageHelper.addInline(pid,file); javaMailSender.send(mimeMessage); LOGGER.info("邮件发送成功"); } catch (MessagingException e) { LOGGER.error("邮件发送失败",e); } } }
设置 smtp,获取授权码:
测试结果:
详见附件中 demo