4.SpringBoot集成发送邮件功能

前期准备工作

准备一个开通邮件发送功能的账号(开发阶段)

例子QQ邮箱

邮箱设置账号->

开启如下配置->

4.SpringBoot集成发送邮件功能_第1张图片

 

上面步骤弄完之后

增加配置文件,由于是QQ邮箱所以host是smtp.qq.com,username就是邮箱,password就是开启smtp时候的密码

spring.mail.host = smtp.qq.com
spring.mail.username = [email protected]
spring.mail.password = couifojuwakniheh
 
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

 

添加依赖


   org.springframework.boot
   spring-boot-starter-mail

之后的使用方法

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.mail.internet.MimeMessage;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
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.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import freemarker.template.Configuration;
import freemarker.template.Template;

@RunWith(SpringRunner.class)
@SpringBootTest
//@ActiveProfiles("qq")
public class EmailTest {

   @Autowired
   private JavaMailSender mailSender;

   @Value("${spring.mail.username}")
   private String Sender;

    @Test
   public void sendSimpleMail() throws Exception {
      SimpleMailMessage message = new SimpleMailMessage();
      message.setFrom(Sender);
      message.setTo("[email protected]");
      message.setSubject("主题:简单邮件");
      message.setText("测试邮件内容");
      mailSender.send(message);
   }

    @Test
   public void sendHtmlMail() {
      MimeMessage message = null;
      try {
         message = mailSender.createMimeMessage();
         MimeMessageHelper helper = new MimeMessageHelper(message, true);
         helper.setFrom(Sender);
         helper.setTo("[email protected]");
         helper.setSubject("主题:HTML邮件");

         StringBuffer sb = new StringBuffer();
         sb.append("

大标题-h1

").append("

红色字

") .append("

右对齐

"); helper.setText(sb.toString(), true); } catch (Exception e) { e.printStackTrace(); } mailSender.send(message); } @Test public void sendAttachmentsMail() { MimeMessage message = null; try { message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(Sender); helper.setTo("[email protected]"); helper.setSubject("主题:带附件的邮件"); helper.setText("带附件的邮件内容"); // 注意项目路径问题,自动补用项目路径 FileSystemResource file = new FileSystemResource(new File("src/main/resources/static/image/picture.jpg")); // 加入邮件 helper.addAttachment("图片.jpg", file); } catch (Exception e) { e.printStackTrace(); } mailSender.send(message); } @Test public void sendTemplateMail() { MimeMessage message = null; try { message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(Sender); helper.setTo("[email protected]"); helper.setSubject("主题:模板邮件"); Map model = new HashMap<>(); model.put("username", "zggdczfr"); Configuration cfg = new Configuration(Configuration.VERSION_2_3_23); cfg.setClassForTemplateLoading(this.getClass(), "/templates"); // Template template = cfg.getTemplate("mail.html"); Template template = cfg.getTemplate("template.ftl"); String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, model); helper.setText(html, true); mailSender.send(message); } catch (Exception e) { e.printStackTrace(); } } }

 

项目结构如下

后续问题:本地运行都能正常发送的邮件,为什么一到部署到服务器上就不能正常发送了

1.检测,端口号是否开启

2.检测,linux上面是否能正常的获取到静态资源

3.打开邮件发送的日志记录功能, 会把发送邮件出现的一些日志都详细的打印出来方便进行问题的调试

开启方式

配置文件中加上下面一段

spring.mail.properties.mail.debug=true

开启日志后的效果

 

你可能感兴趣的:(SpringBoot)