文章目录
一、开启qq邮箱的POP3/SMTP服务
知道如何开启直接跳过该步骤,也可自行百度如何打开qq邮箱的POP3/SMTP服务
点击 设置
点击 账户
下拉找到 POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务 点击 (开启)
拿起您的手机发送短信 (必须用你的保密手机发送短信)
手机发送完短信后 点击 (我已发送)它就会给你个 密码 代码中会用到 如下
二、开始编码
请仔细阅读 尤其 注释
1.pom.xml文件中添加mail依赖
<!--邮箱所需的包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2.配置application.properties
温馨提示,使用了其他平台的邮箱 需要改 平台地址 和 你所属平台的邮箱
例如:网易邮箱 就把 平台地址 改成smtp.163.com
把spring.mail.username = xxxxxxx@qq.com 改成 pring.mail.username = xxxxxxx@163.com
#邮箱配置
#平台地址,用的是qq邮箱不用改
spring.mail.host = smtp.qq.com
#改成自己的邮箱
spring.mail.username = xxxxxxx@qq.com
#发送短信后它给你的授权码 填写到这里
spring.mail.password = xxxx
#这东西不用改
spring.mail.properties.mail.smtp.ssl.enable=true
3.编写MailUtil 和 UUid工具类
MailUtil工具类
package cn.aa.yy.util;
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;
@Service
public class MailUtil {
@Autowired
private JavaMailSender mailSender;//一定要用@Autowired
//application.properties中已配置的值
@Value("${spring.mail.username}")
private String from;
//有时@Value不生效 直接把from写死
//public String from = "你的邮箱";//填写你自己的邮箱
public boolean sendMimeMail( String subject, String text, String to) {
try {
SimpleMailMessage mailMessage = new SimpleMailMessage();
//mailMessage.setSubject("捉鬼吗兄弟?");
mailMessage.setSubject(subject);//主题
//mailMessage.setText("我是正统茅山弟子! 强的一批+激活码");
mailMessage.setText(text);//内容
//mailMessage.setTo("xxxxxxx@qq.com");
mailMessage.setTo(to);//发给谁
//mailMessage.setFrom("xxxxxx@qq.com");
mailMessage.setFrom(from);//你自己的邮箱
mailSender.send(mailMessage);//发送
return true;
}catch (Exception e){
e.printStackTrace();
return false;
}
}
}
UUidUtil 工具类
package cn.aa.yy.util;
import org.springframework.stereotype.Service;
import java.util.UUID;
@Service
public class UUidUtil {
//生成激活码
public static String uuid = UUID.randomUUID().toString().replace("-","");
}
4.测试
————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
有点懵懵懂懂的看看这张图
总结
提示:不要拿去 轰炸 别人的邮箱。到时你自己的邮箱也会封掉
本人也是个新萌,也算是自己 学习后的总结,写的不好还请见谅qaq。还是没有学废 可以去下面这两位大佬的帖子看看
1:https://www.cnblogs.com/smfx1314/archive/2019/01/29/10332330.html
2:https://blog.csdn.net/IndexMan/article/details/89409512