其他分享
首页 > 其他分享> > springBoot项目中任务---邮件发送

springBoot项目中任务---邮件发送

作者:互联网

1)首先在pom.xml文件中导入邮件发送需要的依赖;

   <!--javax.mail-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

2)在application.properties配置文件上配置相关信息;

spring.mail.username=********@qq.com
spring.mail.password=wnnkesooiqlydhhc(授权码,开启SMTP服务处)
spring.mail.host=smtp.qq.com
#开启加密验证
spring.mail.properties.mail.smtp.ssl.enable=true

3)在测试方法中测试;

@SpringBootTest
class DemoApplicationTests {

    @Autowired
    JavaMailSenderImpl mailSender;

    @Test
    void contextLoads() {
        //简单的邮件发送
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setSubject("欢迎来到我的小课堂");
        mailMessage.setText("祝大家2021年快快乐乐的成长");
        mailMessage.setTo("*******@qq.com");
        mailMessage.setFrom("******@qq.com");
        mailSender.send(mailMessage);
    }

    @Test
    void contextLoads2() throws MessagingException {
        //复杂的邮件发送
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        //组装
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        //正文
        helper.setSubject("欢迎来到我的小课堂-plus");
        helper.setText("<p style='color:red'>祝大家2021年快快乐乐的成长</p>",true);
        //附件
        helper.addAttachment("1.jpg",new File("C:\\Users\\Administrator\\Desktop\\1.jpg"));
        helper.addAttachment("2.jpg",new File("C:\\Users\\Administrator\\Desktop\\1.jpg"));
        helper.setTo("********@qq.com");
        helper.setFrom("*********@qq.com");
        mailSender.send(mimeMessage);
    }

}

4)测试结果:

第一种,简单邮件发送:

 

 第二种,复杂邮件发送:

 

标签:qq,springBoot,helper,mailMessage,---,spring,mail,com,邮件
来源: https://www.cnblogs.com/xie-qi/p/14711348.html