smtp服务器怎么设置(SpringBoot整合QQ邮件发送)

前言邮件服务是我们工作中常用的服务之一,作用是非常的多,对外可以给用户发送活动、营销广告等;对内可以发送系统监控报告与告警。常用邮件的 smtp 服务器地址sohu:发送邮件服务器:http://smtp.sohu.com接收邮件服务器:http://pop.sohu.com126:接收邮件服务器: http://POP.126.com发送邮件服务器: http://SMTP.126.com163:POP3服务器:http://pop.163.comSMTP服务器:http://smtp.163.comQQ:接收邮件服务器:http://pop.qq.com发送邮件服务器:http://smtp.qq.com1、准备工作登录邮箱开启PO3/SMTP服务1.1、SMTP简单邮件传输协议定义了递送邮件的机制。在下文中,我们将使用基于Java-Mail的程序与公司或者ISP的SMTP服务器进行通讯。这个SMTP服务器将邮件转发到接收者的SMTP服务器,直至最后被接收者通过POP或者IMAP协议获取。这并不需要SMTP服务器使用支持授权的邮件转发,但是却的确要注意SMTP服务器的正确设置(SMTP服务器的设置与JavaMail API无关)。1.2、POPPOP是一种邮局协议,目前为第3个版本,即众所周知的POP3。POP定义了一种用户如何获得邮件的机制。它规定了每个用户使用一个单独的邮箱。大多数人在使用POP时所熟悉的功能并非都被支持,例如查看邮箱中的新邮件数量。而这个功能是微软的Outlook内建的,那么就说明微软Outlook之类的邮件客户端软件是通过查询最近收到的邮件来计算新邮件的数量来实现前面所说的功能。因此在我们使用JavaMail API时需要注意,当需要获得如前面所讲的新邮件数量之类的信息时,我们不得不自己进行计算。1.3、打开qq邮箱>点击设置>账户,默认是关闭的 ,开启PO3/SMTP服务。手机短信验证成功后,生成16位SMTP命令授权码,见下图:2、Java原生发送qq邮箱实现步骤2.1、创建一个JavaSE项目2.2、新建一个lib文件夹,放邮件发送所需要的架包2.3、编写核心代码 我这里封装了 也可以不封装2.4、封装邮箱,代码如下/**
* @author makeJava
*
* @create 2021-03-21日 18:43
* @describes qq邮箱工具类
*/
public class Email {
public void qqemai(String QQmail,String head,String body) throws AddressException, MessagingException {
Properties properties = new Properties();
properties.put("mail.transport.protocol", "smtp");// 连接协议
properties.put("mail.smtp.host", "smtp.qq.com");// 主机名
properties.put("mail.smtp.port", 465);// 端口号
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.ssl.enable", "true");// 设置是否使用ssl安全连接 —一般都使用
properties.put("mail.debug", "true");// 设置是否显示debug信息 true 会在控制台显示相关信息
// 得到回话对象
Session session = Session.getInstance(properties);
// 获取邮件对象
Message message = new MimeMessage(session);
// 设置发件人邮箱地址
message.setFrom(new InternetAddress("****@qq.com"));
// 设置收件人邮箱地址
message.setRecipients(Message.RecipientType.TO,
new InternetAddress[] { new InternetAddress(QQmail) });
//new InternetAddress();设置同时发送多个好友
// 设置邮件标题
message.setSubject(head);
// 设置邮件内容
message.setText(body);
// 得到邮差对象
Transport transport = session.getTransport();
// 连接自己的邮箱账户
transport.connect("****@qq.com", "*****授权码");// 密码为QQ邮箱开通的stmp服务后得到的客户端授权码
// 发送邮件
int i=0;
transport.sendMessage(message, message.getAllRecipients());
System.out.println("成功!");
transport.close();
}
}2.5、编写main方法/**
* @author makeJava
*
* @create 2021-03-21日 18:50
* @describes 测试qq邮件
*/
public class qqmain {
public static void main(String[] args) throws MessagingException {
Email qq=new Email();
Scanner input=new Scanner(System.in);
System.out.println("请输入QQ号");
String QQmail=input.next()+"@qq.com";
System.out.println("请输入要发送的标题");
String head=input.next();
System.out.println("请输入要发送的文本");
String body=input.next();
qq.qqemai(QQmail, head, body);
}
}3、运行测试:4、结果如下:测试成功5、Springboot实现QQ邮箱的发送打开qq邮箱>点击设置>账户,默认是关闭的 ,开启PO3/SMTP服务。手机短信验证成功后,生成16位SMTP命令授权码,见下图:==记住这串授权码 会用到==6、实现步骤6.1、创建一个Springboot项目6.2、勾选web依赖6.3、导入QQ邮件所需依赖 <!–qq邮件发送所需依赖–>
</dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependencies>
<dependency>
<groupId>com.troy.keeper</groupId>
<artifactId>keeper-core-boot</artifactId>
</dependency>
<dependency>
<groupId>com.troy.keeper</groupId>
<artifactId>keeper-starter-excel</artifactId>
</dependency>
<dependency>
<groupId>com.troy.keeper</groupId>
<artifactId>keeper-starter-swagger</artifactId>
</dependency>
<dependency>
<groupId>com.troy.keeper</groupId>
<artifactId>sd-user-api</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>com.troy.keeper</groupId>
<artifactId>sd-system-api</artifactId>
<version>${project.parent.version}</version>
</dependency>
</dependencies>6.4、配置application.properties#配置邮件消息
spring.mail.host=smtp.qq.com
#发送邮件者信箱
[email protected]
#PO3/SMTP服务时邮箱的授权码
spring.mail.password=xxxxxxxxxxxxxxxx
spring.mail.default-encoding=UTF-8
spring.mail.port=465
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.debug=true6.5、编写controller/**
* @author makeJava
*
* @create 2021-03-21日 19:50
* @describes 测试qq邮件前端层
*/
@Controller
@RequestBody
@Slf4j
@Api(tags="qq邮件前端层")
public class EmailController {
@Autowired
JavaMailSender mailSender;//注入QQ发送邮件的bean
//定义发送的内容 我这里发送一张图片 需要html标签
/**
* 查询所有数据
*
* @param goodTestDTO 查询实体
* @return 所有数据
*/
public static String body="<img src='https://images.cnblogs.com/cnblogs_com/joker-dj/1691556/t_20040706414135.png' alt=''>";
@RequestMapping("/qqemail")
@ApiOperation(value = "定义发送的内容")
public Object qqemail(@RequestParam String qq,String title) {
try {
MimeMessage mimeMessage = this.mailSender.createMimeMessage();
MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
message.setFrom("******@qq.com");//设置发件qq邮箱
qq+="@qq.com"; //补全地址
message.setTo(qq); //设置收件人
message.setSubject(title); //设置标题
message.setText(body,true); //第二个参数true表示使用HTML语言来编写邮件
// FileSystemResource file = new FileSystemResource(
// File file = new File("图片路径");
// helper.addAttachment("图片.jpg", file);//添加带附件的邮件
// helper.addInline("picture",file);//添加带静态资源的邮件
this.mailSender.send(mimeMessage);
return "发送成功";
} catch (Exception ex) {
ex.printStackTrace();
return "发送成功";
}
}
}6.6、编写前端页面<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>QQ邮件发送</title>
</head>
<body>
<form action="qqemail">
<input type="text" placeholder="请输入收件人qq号"name="qq" value="">
<input type="text" placeholder="请输入邮件标题" name="title">
<input type="submit" value="发送">
</form>
</body>
</html>7、启动运行 浏览器输入 http://localhost:8080/qqEmail.html输入qq号 和标题 点击发送已收到发送来的图片测试成功!


本文出自快速备案,转载时请注明出处及相应链接。

本文永久链接: https://www.xiaosb.com/beian/36701/