• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

邮件发送

互联网 diligentman 1周前 (10-18) 14次浏览

添加依赖

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

程序员灯塔
转载请注明原文链接:https://www.wangt.cc/2020/10/%e9%82%ae%e4%bb%b6%e5%8f%91%e9%80%81/
喜欢 (0)