spring boot 发送简单的QQ邮件

maven依赖

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-mailartifactId>
dependency>
配置文件
spring:
#邮件发送设置
  mail:
    host: smtp.qq.com
    username: //发送者邮箱
    password:   //授权码
    properties:
     mail:
       smtp:
           auth: true
           starttls:
             enable: true
             required: true

EmailUtil 工具类


@Component
public class EmailUtil {
    @Autowired
    private JavaMailSender mailSender;

    /**
     * 给某人发送异常邮件
     *
     * @param fromBody 发送至
     * @param toBody 发送到
     * @param topic 发送主题
     * @param content 发送内容
     */
    public void sendEmail(String fromBody, String toBody, String topic, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(fromBody);
        message.setTo(toBody);
        message.setSubject("主题:" + topic);
        message.setText(content);
        mailSender.send(message);
    }
}

调用方法

@Autowired
private EmailUtil emailUtil;
emailUtil.sendEmail("发送者邮箱", "接收者邮箱", "主题", "内容");
希望对大家有所帮助!
                                               
                                                        ---------------一个搬砖的小菜鸡



你可能感兴趣的:(spring boot 发送简单的QQ邮件)