Spring发送电子邮件

简介

Spring基于javax.mail封装了一套用于发送电子邮件的API,位于org.springframework.mail包下,下面将介绍下发送普通文本邮件和基于thymeleaf模版的邮件。

普通邮件

导入javax.mail依赖




    com.sun.mail
    javax.mail
    1.6.2

Spring配置类

@PropertySource(value="classpath:mail.properties")
@Configuration
public class SpringConfig {
    @Autowired
    Environment environment;
    
    /**
     * Spring邮件配置
     */
    @Bean
    public JavaMailSenderImpl javaMailSenderImpl(){
        JavaMailSenderImpl javaMailSenderImpl = new JavaMailSenderImpl();
        javaMailSenderImpl.setHost(environment.getProperty("mail.host"));
        javaMailSenderImpl.setUsername(environment.getProperty("mail.username"));
        javaMailSenderImpl.setPassword(environment.getProperty("mail.password"));
        javaMailSenderImpl.setDefaultEncoding(environment.getProperty("mail.encoding"));
        return javaMailSenderImpl;
    }
}

邮件配置:mail.properties

#QQ邮箱发送邮件服务器
mail.host=smtp.qq.com
#QQ邮箱账户名
[email protected]
#QQ邮箱授权码,开启IMAP/SMTP服务时会生成
mail.password=lrorsapyffcwbegg
#邮件发送编码
mail.encoding=UTF-8

测试

@RunWith(SpringRunner.class)
@ContextConfiguration(classes=SpringConfig.class)
public class MailTest {
    @Autowired
    private JavaMailSenderImpl javaMailSenderImpl;
    
    @Test
    public void testSimpleMail() throws MessagingException{
        MimeMessage message = javaMailSenderImpl.createMimeMessage();
        
        //发送带附件和内联元素的邮件需要将第二个参数设置为true
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        
        //发送方邮箱,和配置文件中的mail.username要一致
        helper.setFrom("[email protected]");
        
        //接收方
        helper.setTo("[email protected]");
        
        //主题
        helper.setSubject("邮件测试");
        
        //邮件内容
        helper.setText("", true);
        
        //邮件内容内嵌的图片,addInline中的contentId要和邮件内容中的cid相对应
        FileSystemResource res = new FileSystemResource(new File("e:/yc/1.jpg"));
        helper.addInline("myphoto", res);
        
        //邮件附件
        FileSystemResource file = new FileSystemResource(new File("e:/yc/myphoto.zip"));
        helper.addAttachment("myphoto.zip", file);
        
        javaMailSenderImpl.send(message);
    }
}

Spring发送电子邮件_第1张图片

基于thymeleaf模版的邮件

导入thymeleaf-spring依赖


    org.thymeleaf
    thymeleaf-spring4
    3.0.11.RELEASE

Spring配置类中添加thymeleaf模板配置

    @Bean
    public TemplateEngine emailTemplateEngine() {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
        templateResolver.setResolvablePatterns(Collections.singleton("*.html"));
        templateResolver.setPrefix("/templates/");
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode(TemplateMode.HTML);
        templateResolver.setCharacterEncoding("UTF-8");
        templateResolver.setCacheable(false);
        templateEngine.addTemplateResolver(templateResolver);
        return templateEngine;
    }

模版文件:/src/main/resources/templates/msg.html



  
    
    
  
  
    

您好, xxx,感谢您使用XXX服务,您正在进行邮箱验证,本次请求的验证码为:

1111(为了保障您帐号的安全性,请在1小时内完成验证。)

XXX团队
2019年06月06日

测试

    @Autowired
    private TemplateEngine emailTemplateEngine;
    
    @Test
    public void testTemplateMail() throws MessagingException{
        MimeMessage message = javaMailSenderImpl.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom("[email protected]");
        helper.setTo("[email protected]");
        helper.setSubject("邮件测试");
        
        //thymeleaf模版解析成String
        Context ctx = new Context();
        ctx.setVariable("name", "小姐姐");
        ctx.setVariable("validCode", "88888888");
        String htmlContent = emailTemplateEngine.process("msg.html", ctx);
        helper.setText(htmlContent, true);
        
        javaMailSenderImpl.send(message);
    }

Spring发送电子邮件_第2张图片

你可能感兴趣的:(Spring发送电子邮件)