Home > Java > javaTutorial > How Springboot implements email tasks

How Springboot implements email tasks

WBOY
Release: 2023-05-22 17:07:06
forward
1424 people have browsed it

邮件任务

pom.xml

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

配置文件

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

测试类

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

  }
Copy after login

结果:

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template