Springboot集成Email

Springboot集成Email

1、Pom文件:

<!-- email config -->

<dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-mail</artifactId>

</dependency>

2、配置文件

#email module

spring.mail.host=smtp.qq.com

spring.mail.protocol=smtp

spring.mail.default-encoding=UTF-8

spring.mail.username=xxx@qq.com

spring.mail.password=xxx

spring.mail.test-connection=true

spring.mail.properties.mail.smtp.auth=true

spring.mail.properties.mail.smtp.starttls.enable=true

spring.mail.properties.mail.smtp.starttls.required=true

3、发送邮件实体

@Data

public class ToEmail implements Serializable {

    private static final long serialVersionUID = 1L;



    /**

     * 邮件接收方,可多人

     */

    private String[] tos;

    /**

     * 邮件主题

     */

    private String subject;

    /**

     * 邮件内容

     */

    private String content;

    /**

     * 邮件附件

     */

    private File[] files;



    public ToEmail(String[] tos, String subject, String content, File[] files) {

        this.tos = tos;

        this.subject = subject;

        this.content = content;

        this.files = files;

    }

}
4、接口类
public interface EmailService{



    void sendEmail(ToEmail toEmail);



}
5、接口实现类
@Service

@Slf4j

public class EmailServiceImpl implements EmailService {



    @Resource

    private JavaMailSender mailSender;



    @Value("${spring.mail.username}")

    private String from;



    @Override

    public void sendEmail(ToEmail toEmail) {

        //创建一个MINE消息

        MimeMessage message = mailSender.createMimeMessage();

        try {

            MimeMessageHelper helper = new MimeMessageHelper(message, true);

            //谁发

            helper.setFrom(from);

            //谁接收

            helper.setTo(toEmail.getTos());

            //邮件主题

            helper.setSubject(toEmail.getSubject());

            //邮件内容   true 表示带有附件或html

            helper.setText(toEmail.getContent(), true);

            //邮件附件

            if (toEmail.getFiles() != null && toEmail.getFiles().length > 0) {

                for (int i = 0; i <toEmail.getFiles().length ; i++) {

                    File file = toEmail.getFiles()[i];

                    FileSystemResource fileSystemResource = new FileSystemResource(file);

                    String filename = fileSystemResource.getFilename();

                    //添加附件

                    helper.addAttachment(filename, file);

                }

            }

            mailSender.send(message);

            log.info("邮件发送成功");

        } catch (MessagingException e) {

           log.error("邮件发送失败|",e);

        }

    }

}

6、测试类

@Api(tags = "测试管理")

@RestController

@RequestMapping("/test")

public class TestController {



    @Resource

    private EmailService emailService;



    @ApiOperation(value = "测试发送邮件")

    @GetMapping(path = "/testSendEmail")

    public Response testSendEmail() {

        return new ResponseCallBack() {

            @Override

            public void execute(ResponseCriteria criteria, Object... obj) {

                String[] tos = {"123@qq.com"};

                String subject = "测试标题";

                String content = "测试内容";

                File[] files = {

                        new File("E:\\home\\test\\xxx.pdf"),

                        new File("E:\\home\\test\\xxx.pdf")

                };

                emailService.sendEmail(new ToEmail(tos, subject, content, files));

                criteria.addSingleResult(true);

            }

        }.sendRequest();

    }

}


个人交流Q:772182648
以上是本人自己总结,并且在项目中实际用到的。新手一枚,不喜勿喷!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

熊GG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值