美文网首页
Spring Boot 邮件

Spring Boot 邮件

作者: 虫儿飞ZLEI | 来源:发表于2019-03-04 20:32 被阅读0次

1. 导包

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

2. 配置

spring.mail.username=534096094@qq.com
spring.mail.password=gtstkoszjelabijb
qq邮箱的host
spring.mail.host=smtp.qq.com
不配置可能报错
spring.mail.properties.mail.smtp.ssl.enable=true

3.使用

简单邮件

    @Autowired
    JavaMailSenderImpl mailSender;

    @Test
    public void contextLoads() {
        SimpleMailMessage message = new SimpleMailMessage();
        //邮件设置
        message.setSubject("通知-今晚开会");
        message.setText("今晚7:30开会");

        message.setTo("17512080612@163.com");
        message.setFrom("534096094@qq.com");

        mailSender.send(message);
    }

复杂邮件

    @Test
    public void test02() throws  Exception{
        //1、创建一个复杂的消息邮件
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);

        //邮件设置
        helper.setSubject("通知-今晚开会");
        //true:识别html
        helper.setText("<b style='color:red'>今天 7:30 开会</b>",true);

        helper.setTo("17512080612@163.com");
        helper.setFrom("534096094@qq.com");

        //上传文件
        helper.addAttachment("1.jpg",new File("C:\\Users\\lfy\\Pictures\\Saved Pictures\\1.jpg"));
        helper.addAttachment("2.jpg",new File("C:\\Users\\lfy\\Pictures\\Saved Pictures\\2.jpg"));

        mailSender.send(mimeMessage);

    }

相关文章

网友评论

      本文标题:Spring Boot 邮件

      本文链接:https://www.haomeiwen.com/subject/jbjwuqtx.html