Home  >  Article  >  Java  >  How Springboot implements email tasks

How Springboot implements email tasks

WBOY
WBOYforward
2023-05-22 17:07:061382browse

邮件任务

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);

  }

结果:

How Springboot implements email tasks

The above is the detailed content of How Springboot implements email tasks. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete