Rumah >Java >javaTutorial >Cara Springboot melaksanakan tugasan e-mel

Cara Springboot melaksanakan tugasan e-mel

WBOY
WBOYke hadapan
2023-05-22 17:07:061402semak imbas

邮件任务

pom.xml

 <dependency>       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-mail</artifactId>
       <scope>test</scope>
     </dependency>

配置文件

spring: 
 mail:
  username: ***********
  password: *********  (这是qq邮箱的授权码)
  host: smtp.qq.com
spring.mail.properties.mail.smtp.ssl.enable=true

测试类

@Autowired(required = false)
  JavaMailSenderImpl mailSender;

  @Test
  public void contextLoads() {
    SimpleMailMessage message = new SimpleMailMessage();
    //邮件设置
    message.setSubject("通知-今晚开会");
    message.setText("今晚7:30开会");

    message.setTo("**************");
    message.setFrom("**************");

    mailSender.send(message);
  }

  @Test
  public void test02() throws Exception{
    //1、创建一个复杂的消息邮件
    MimeMessage mimeMessage = mailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);

    //邮件设置
    helper.setSubject("测试");
    helper.setText("<b style=&#39;color:red&#39;>今天 7:30 开会</b>",true);

    helper.setTo("***************");
    helper.setFrom("**************");

    //上传文件
    helper.addAttachment("nginx.md",new File("C:\\Users\\asus\\Desktop\\nginx.md"));

    mailSender.send(mimeMessage);

  }

结果:

Cara Springboot melaksanakan tugasan e-mel

Atas ialah kandungan terperinci Cara Springboot melaksanakan tugasan e-mel. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Artikel ini dikembalikan pada:yisu.com. Jika ada pelanggaran, sila hubungi admin@php.cn Padam