邮件发送

原创
10/18 23:55
阅读数 24

添加依赖

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

配置文件

# Login user of the SMTP server.
spring.mail.username=xxx@qq.com
# Login password of the SMTP server.
spring.mail.password=生成授权码
# SMTP server host
spring.mail.host=smtp.qq.com   

测试

@SpringBootTest
class MailApplicationTests {

    @Autowired
    JavaMailSenderImpl sender;

    @Test
    void test1() {
        SimpleMailMessage message = new SimpleMailMessage();

        message.setSubject("通知");
        message.setText("下午开会!");

        message.setFrom("xxx@qq.com");
        message.setTo("yyy@126.com");

        sender.send(message);
    }

    @Test
    void test2() throws MessagingException {
        MimeMessage message = sender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message,true); // boolean 添加附件

        helper.setSubject("通知!");
        helper.setText("<i>下午开会</i>",true); //开启html标签识别

        helper.setFrom("xxx@qq.com");
        helper.setTo("yyy@126.com");

        helper.addAttachment("color.png",new File("C:/Users/hp/Desktop/color.png"));
        helper.addAttachment("anime.png",new File("C:/Users/hp/Desktop/anime.png"));

        sender.send(message);
    }
}
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部