Springboot javamailSender

javamailSender

在Spring Boot的Starter模块中也为此提供了自动化配置。如何在Spring Boot中使用 JavaMailSender 发送邮件。

  • maven引入jar包
  <dependency>  
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>  
  • 核心配置文件配置发送者信息
spring.mail.host=smtp.163.com  
spring.mail.username=用户名  
spring.mail.password=密码  
spring.mail.properties.mail.smtp.auth=true  
spring.mail.properties.mail.smtp.starttls.enable=true  
spring.mail.properties.mail.smtp.starttls.required=true  
  • 单元测试
package cn.kxind;

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.mail.SimpleMailMessage;  
import org.springframework.mail.javamail.JavaMailSender;  
import org.springframework.mail.javamail.MimeMessageHelper;  
import org.springframework.test.context.junit4.SpringRunner;

import javax.mail.MessagingException;  
import javax.mail.internet.MimeMessage;

/**
 * Created by Administrator on 2016/12/15.
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class My163MailTest {

    @Autowired
    private JavaMailSender javaMailSender;

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

    @Test
    public void testSendSimple() {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(username);
        message.setTo("641282092@qq.com");
        message.setSubject("标题:发送文本内容");
        message.setText("测试内容");
        javaMailSender.send(message);


    }
@Test
    public void testSendHtml() {
        MimeMessage message = null;

        try {
            message = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(username);
            helper.setTo("kxinds@gmail.com");
            helper.setSubject("标题:发送HTML内容");
            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());


        } catch (MessagingException e) {
            e.printStackTrace();
        }
        javaMailSender.send(message);
    }
}

kxind

性别:男. 敢于尝试 爱折腾的死宅程序猿. https://github.com/kxinds