UserServiceImplTest.java 4.0 KB
Newer Older
Q
qinyingjie 已提交
1 2
package com.kwan.springbootkwan;

Q
qinyingjie 已提交
3
import com.kwan.springbootkwan.entity.MailInfo;
Q
qinyingjie 已提交
4 5
import com.kwan.springbootkwan.entity.User;
import com.kwan.springbootkwan.mapper.UserMapper;
Q
qinyingjie 已提交
6
import com.kwan.springbootkwan.service.ISendMsgHandle;
Q
qinyingjie 已提交
7 8 9 10
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

Q
qinyingjie 已提交
11 12 13 14
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

Q
qinyingjie 已提交
15 16 17 18 19 20

@SpringBootTest
public class UserServiceImplTest {

    @Autowired
    private UserMapper userService;
Q
qinyingjie 已提交
21
    @Autowired
Q
qinyingjie 已提交
22
    private ISendMsgHandle emailSendMsgHandle;
Q
qinyingjie 已提交
23 24 25 26 27 28

    @Test
    public void queryAll() {
        User user = userService.selectById(1);
        System.out.println(user);
    }
Q
qinyingjie 已提交
29 30 31 32 33 34 35 36 37 38

    /**
     * 发邮件
     */
    @Test
    void sendSimpleTextEmail() {
        MailInfo mailInfo = new MailInfo();
        mailInfo.setReceiver(new String[]{"qinyingjie@deepexi.com"});
        mailInfo.setSubject("测试主题");
        mailInfo.setContent("邮件内容");
Q
qinyingjie 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
        emailSendMsgHandle.sendSimpleTextEmail(mailInfo);
    }


    /**
     * 带html
     */
    @Test
    public void sendHTMLMail() {
        MailInfo mailBean = new MailInfo();
        //接收人
        mailBean.setReceiver(new String[]{"qinyingjie@deepexi.com"});
        mailBean.setSubject("SpringBootMailHTML之这是一封HTML格式的邮件");
        //抄送给谁
        mailBean.setCc(new String[]{"786775527@qq.com", "286968900@qq.com"});
        StringBuilder sb = new StringBuilder();
        sb.append("<h2>SpringBoot测试邮件HTML</h2>")
                .append("<p style='text-align:left'>这是一封HTML邮件...</p>")
                .append("<p> 时间为:" + new Date() + "</p>");
        mailBean.setContent(sb.toString());
        //true、false控制以普通文本发送还是以html格式发送
        emailSendMsgHandle.sendHtmlEmail(mailBean, true);
    }

    @Test
    void sendEmail() {
        MailInfo mailBean = new MailInfo();
        mailBean.setReceiver(new String[]{"qinyingjie@deepexi.com"});
        mailBean.setSubject("SpringBootMailHTML之这是一封HTML格式的邮件");
        //抄送给谁
        mailBean.setCc(new String[]{"786775527@qq.com", "286968900@qq.com"});
        StringBuilder sb = new StringBuilder();
        sb.append("<h2>SpringBoot测试邮件HTML</h2>")
                .append("<p style='text-align:left'>这是一封HTML邮件...</p>")
                .append("<p> 时间为:" + new Date() + "</p>");
        mailBean.setContent(sb.toString());
        mailBean.setAttachFileNames(new String[]{"/Users/Downloads/密码管理.xlsx", "/Users/Downloads/1635834323888.jpg"});
        emailSendMsgHandle.sendEnclosureEmail(mailBean);
    }

    @Test
    void sendEmail2() {
        MailInfo mailBean = new MailInfo();
        mailBean.setReceiver(new String[]{"qinyingjie@deepexi.com"});
        mailBean.setSubject("SpringBootMailHTML之这是一封HTML格式的邮件");
        mailBean.setCc(new String[]{"786775527@qq.com", "286968900@qq.com"});
        StringBuilder sb = new StringBuilder();
        sb.append("<h2>SpringBoot测试邮件HTML</h2>")
                .append("<p style='text-align:left'>这是一封HTML邮件...</p>")
                .append("<a href=http://www.baidu.com>点击进入百度</a><br/>")
                //内嵌图片
                .append("<img src=\"cid:a00000001\"><br/><br/>")
                .append("<img src=\"cid:a00000002\">")
                .append("<p> 时间为:" + new Date() + "</p>");
        mailBean.setContent(sb.toString());
        //附件
        mailBean.setAttachFileNames(new String[]{"/Users/Downloads/密码管理.xlsx", "/Users/Downloads/1635834323888.jpg"});
        //内嵌了多少张图片,如果没有,则new一个不带值的Map
        Map<String, String> image = new HashMap<>();
        image.put("a00000001", "/Users/Downloads/WechatIMG22.jpg");
        image.put("a00000002", "/Users/Downloads/face_1631083961355.png");

        mailBean.setImageMap(image);
        emailSendMsgHandle.sendEnclosureEmail(mailBean);
Q
qinyingjie 已提交
103
    }
Q
qinyingjie 已提交
104
}