SpringBoot 邮件任务

SpringBoot 邮件任务

导入依赖


        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-mailartifactId>
            <version>2.3.2.RELEASEversion>
        dependency>

配置 mail

#配置email
[email protected]
spring.mail.password=rtdyfolrfocobefc
spring.mail.host=smtp.qq.com
#开启加密验证
spring.mail.properties.mail.smtp.ssl.enable=true

发送一个简单邮件

@SpringBootTest
class EmailApplicationTests {


    @Autowired
    JavaMailSender mailSender;


    @Test
    void contextLoads() {

        //一个简单的邮件发送
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setSubject("程序员你好啊");
        simpleMailMessage.setText("谢谢你");
        simpleMailMessage.setTo("[email protected]");
        simpleMailMessage.setFrom("[email protected]");
        mailSender.send(simpleMailMessage);
    }

发送一个复杂邮件

 @Test
    void contextLoads2() throws MessagingException {

        //一个复杂的邮件发送

        MimeMessage mimeMessage = mailSender.createMimeMessage();
        //组装
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,true);
        mimeMessageHelper.setSubject("你好啊--plus");
        mimeMessageHelper.setText("

谢谢你

"
,true); //附件 mimeMessageHelper.addAttachment("1.jpg",new File("C:\\Users\\Administrator\\Desktop\\1.jpg")); mimeMessageHelper.setTo("[email protected]"); mimeMessageHelper.setFrom("[email protected]"); mailSender.send(mimeMessage); }

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