Springboot邮件任务

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>
spring.mail.username=XX@qq.com
spring.mail.password=XXX
spring.mail.host=smtp.qq.com
#qq需要开启ssl
spring.mail.properties.mail.smtp.ssl.enable=true
package com.example.scheduldemo;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@SpringBootTest
class SchedulDemoApplicationTests {

    @Autowired
    JavaMailSenderImpl mailSender;

    @Test
    void contextLoads() {
        // 邮箱设置1:一个简单的邮件
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setSubject("通知-明天来狂神这听课");
        simpleMailMessage.setText("今晚7:30开会");
        simpleMailMessage.setTo("xx@qq.com");
        simpleMailMessage.setFrom("xx@qq.com");
        mailSender.send(simpleMailMessage);
    }

    @Test
    void contextLoads2() throws MessagingException {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);

        mimeMessageHelper.setSubject("通知-明天来狂神这听课");
        mimeMessageHelper.setText("<b style=color:red'>今天7:30来开会</br>", true);
        // 发送附件
        mimeMessageHelper.addAttachment("1.jpg",new File("C:\\Users\\JW\\Pictures\\Screenshots\\屏幕截图(1).png"));
        mimeMessageHelper.addAttachment("1.jpg",new File("C:\\Users\\JW\\Pictures\\Screenshots\\屏幕截图(1).png"));
        mimeMessageHelper.setTo("xx@qq.com");
        mimeMessageHelper.setFrom("xx@qq.com");

        mailSender.send(mimeMessage);
    }

}
posted @ 2020-05-20 22:32  xl4ng  阅读(144)  评论(0编辑  收藏  举报