springboot邮件任务

  
            org.springframework.boot
            spring-boot-starter-mail
        

依赖

配置文件

[email protected]
spring.mail.password=*************
spring.mail.host=smtp.qq.com
#开启加密验证  表示是否自动开启安全性
spring.mail.properties.mail.smtl.ssl.enable=true
package com.qf.sping09test;

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 Sping09TestApplicationTests {

    @Autowired
    JavaMailSenderImpl javaMailSender;

    @Test
    void contextLoads() {
        //一个简单的邮件~
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setSubject("基哥你好啊~~~");//邮件标题
        mailMessage.setText("谢谢你观看狂神说java课程");

        mailMessage.setTo("[email protected]");
        mailMessage.setFrom("[email protected]");


        javaMailSender.send(mailMessage);

    }




    @Test
    void contextLoad2() throws MessagingException {
        //一个复杂的邮件~
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
    //组装~
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);

        //正文
        helper.setSubject("基哥又见面了!!");
       helper.setText("

谢谢你看我的邮件~~~

",true); //附件 helper.addAttachment("a.jpg",new File("F:\\JavaStudy\\sping-09-test\\src\\main\\resources\\static\\a.jpg")); helper.setTo("[email protected]"); helper.setFrom("[email protected]"); javaMailSender.send(mimeMessage); } }

发送邮件

springboot邮件任务_第1张图片

 

你可能感兴趣的:(SpringBoot,spring,boot,java,spring)