SpringBoot集成email

SpringBoot集成email

1. 导入Maven依赖

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

2.编写properties配置文件

spring.mail.username=1318248167@qq.com
spring.mail.password=zpsodspfdmaqjcbh //密码
spring.mail.host=smtp.qq.com
#qq需要开启加密认证
spring.mail.properties.mail.smtp.ssl.enable=true

3. 编写测试类

package com.qilin;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@SpringBootTest
class ShiroSpringBootApplicationTests {

    @Autowired
    JavaMailSenderImpl mailSender;
    @Test
    //一个简单的邮件
    void contextLoads() {
            SimpleMailMessage mailMessage = new SimpleMailMessage();

            mailMessage.setSubject("测试标题");//文件标题
            mailMessage.setText("测试内容");//文件内容

            mailMessage.setTo("1318248167@qq.com");
            mailMessage.setFrom("1318248167@qq.com");
    }

    @Test
    //一个复杂的邮件
    void contextLoads2() throws MessagingException {

        MimeMessage mimeMessage = mailSender.createMimeMessage();
        //组装
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);

        helper.setSubject("测试标题");
        helper.setText("<p style='color:red'>测试内容</p>",true);

        //附件
        helper.addAttachment("1.jpg",new File("C:\\Users\\ASUS\\Pictures\\表情包\\1.jpg"));
        helper.addAttachment("2.jpg",new File("C:\\Users\\ASUS\\Pictures\\表情包\\1.jpg"));

        helper.setTo("1318248167@qq.com");
        helper.setFrom("1318248167@qq.com");

        mailSender.send(mimeMessage);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值