springboot2入门到实战-整合QQ邮箱

springboot整合QQ邮箱

配置邮箱

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

springboot整合email

导入依赖

xml 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

配置

emai业务类

  • 接口
java 复制代码
public interface IMailService {
    void sendMail(String from , String to, String subject, String content);
}
  • 实现类
java 复制代码
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;

@Service
public class MailServiceImpl implements IMailService {

    @Value("${spring.mail.username}")
    private String from;

    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    public 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);
    }
}

单元测试

相关推荐
前进吧-程序员1 分钟前
现代 C++ 异步编程:从零实现一个高性能 ThreadPool (C++20 深度实践)
开发语言·c++·c++20
qqacj4 分钟前
Spring Security 官网文档学习
java·学习·spring
Rsun0455114 分钟前
10、Java 桥接模式从入门到实战
java·开发语言·桥接模式
金銀銅鐵16 分钟前
[Java] 从 class 文件看 cglib 对 MethodInterceptor 的处理 (下)
java·后端
lee_curry19 分钟前
Java中关于“锁”的那些事
java·线程·并发·juc
jieyucx23 分钟前
Golang 完整安装与 VSCode 开发环境搭建教程
开发语言·vscode·golang
pearlthriving25 分钟前
c++当中的泛型思想以及c++11部分新特性
java·开发语言·c++
智慧地球(AI·Earth)34 分钟前
规则引擎实战:Python中re库和pyknow库规则引擎实战教程
开发语言·python·程序人生
梦魇星虹1 小时前
idea Cannot find declaration to go to
java·ide·intellij-idea
小雅痞1 小时前
[Java][Leetcode hard] 42. 接雨水
java·开发语言·leetcode