springboot使用qq邮箱
1.首先导入邮箱相关的maven
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2.配置文件(application)
spring.mail.host=smtp.qq.com
spring.mail.username=qq邮箱
spring.mail.password=邮箱授权密码下面会说如何获取
spring.mail.default-encoding=UTF-8
spring.main.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.debug=true
3.获取邮箱的授权密码
步骤:邮箱首页—>点击设置—>账户—>开启pop3/smtp服务
开启之后会给一串授权密码
4.下面是我写的一个邮箱发送的工具类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class EmailUtil {
@Autowired
JavaMailSender jms;
public void send(String receiver, String text){
String sender="邮箱"; //这个是发送人的邮箱
//建立邮件消息
SimpleMailMessage mainMessage = new SimpleMailMessage();
//发送者
System.out.println("发送者 ------------------");
mainMessage.setFrom(sender);
System.out.println("接收者 ------------------");
//接收者
mainMessage.setTo(receiver);
//发送的标题
mainMessage.setSubject("标题");
//发送的内容
mainMessage.setText("发送的信息");
System.out.println(mainMessage);
jms.send(mainMessage);
}
}
当然有的小伙伴会遇到一些注入的问题:
相关的解决办法:
其中一种使用set方法注入
EmailUtil emailUtil;
@Autowired
public void setEmailUtil(EmailUtil emailUtil){
this.emailUtil=emailUtil;
}
还有很多种方法
下面祝大家都能开心快乐的学习编程。以上的方法适用于小白开发者快速完成对邮箱的使用。