springboot整合QQ邮箱

配置邮箱

登录邮箱服务器: 登录QQ邮箱

springboot整合email

导入依赖

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

配置

emai业务类

  • 接口
public interface IMailService {void sendMail(String from , String to, String subject, String content);}
  • 实现类
package com.wnhz.mq.tools.service.impl;import com.wnhz.mq.tools.service.IMailService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.mail.SimpleMailMessage;import org.springframework.mail.javamail.JavaMailSender;import org.springframework.stereotype.Service;import java.util.Date;@Servicepublic class MailServiceImpl implements IMailService {@Value("${spring.mail.username}")private String from;@Autowiredprivate JavaMailSender javaMailSender;@Overridepublic void sendMail(String to, String subject, String content) {SimpleMailMessage mail = new SimpleMailMessage();mail.setFrom(from);mail.setTo(to);mail.setSubject(subject);mail.setSentDate(new Date());mail.setText(content);javaMailSender.send(mail);}}

单元测试