ServiceTest.java 2.15 KB
package com.huaheng.test;

import com.huaheng.pc.config.sendMail.service.MailService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

/**
 * Created by Enzo Cotter on 2019/9/6.
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class ServiceTest {

    @Resource
    private MailService mailService;

    /**
     * 发送简单纯文本邮件
     */
    @Test
    public void sendSimpleMail() {
        String[] to = {"752432958@qq.com"};
        String[] cc = {};
        mailService.sendSimpleMail(to, "发送邮件测试", "大家好,这是我用springboot进行发送邮件测试",cc);
    }

    /**
     * 发送HTML邮件
     */
    @Test
    public void sendHtmlMail() {
        String[] to = {"752432958@qq.com"};
        String[] cc = {};
        String content = "<html><body><h3><font color=\"red\">" + "大家好,这是springboot发送的HTML邮件" + "</font></h3></body></html>";
        mailService.sendHtmlMail(to, "发送邮件测试", content, cc);
    }

    /**
     * 发送带附件的邮件
     */
    @Test
    public void sendAttachmentMail() {
        String[] to = {"752432958@qq.com"};
        String[] cc = {};
        String content = "<html><body><h3><font color=\"red\">" + "大家好,这是springboot发送的HTML邮件,有附件哦" + "</font></h3></body></html>";
        String filePath = "your file path";
        mailService.sendAttachmentMail(to, "发送邮件测试", content, filePath, cc);
    }

    /**
     * 发送带图片的邮件
     */
    @Test
    public void sendInlineResourceMail() {
        String[] to = {"752432958@qq.com"};
        String[] cc = {};
        String rscPath = "your picture path";
        String rscId = "skill001";
        String content = "<html><body><h3><font color=\"red\">" + "大家好,这是springboot发送的HTML邮件,有图片哦" + "</font></h3>"
                + "<img src=\'cid:" + rscId + "\'></body></html>";
        mailService.sendInlineResourceMail(to, "发送邮件测试", content, rscPath, rscId, cc);
    }
}