admin管理员组文章数量:1529463
环境:JDK1.8、MAVEN 3.6.1 、eclipse
1.添加email发送的依赖
当前的
pom
文件的内容:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.22.RELEASE</version>
<relativePath />
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 添加mail的支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!-- junit配置 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
2.查看文档
发现官网上有这样一句话:
Spring Framework提供了一个使用 JavaMailSender
界面发送电子邮件的简单抽象 ,Spring Boot为它提供了自动配置以及启动器模块。
所以:可以使用JavaMailSender发送邮箱查看配置
Email (MailProperties)email邮箱的配置
spring.mail.default-encoding=UTF-8
# Default MimeMessage encoding.spring.mail.host=
# SMTP server host. For instance smtp.examplespring.mail.jndi-name=
# Session JNDI name. When set, takes - precedence to others Session settings.spring.mail.password=
# Login password of the SMTP server.spring.mail.port=
# SMTP server port.spring.mail.properties.*=
# Additional JavaMail Session properties.spring.mail.protocol=smtp
# Protocol used by the SMTP server.spring.mail.test-connection=false
# Test that the mail server is available on startup.spring.mail.username=
# Login user of the SMTP server.
3.配置QQ邮箱的信息
当前application.properties中的内容为:
spring.mail.host=smtp.qq
spring.mail.username= xxx@qq
spring.mail.password=//your password
spring.mail.default-encoding=UTF-8
spring.mail.port=465
spring.mail.properties.mail.smtp.socketFactory.class=javax.ssl.SSLSocketFactory
spring.mail.properties.mail.debug=true
4.编写工具类
当前的
EmailMessageUtils
内容如下
/**
* @description 发送邮件的工具类
* @author hy
* @date 2019-08-13
*/
public class EmailMessageUtils {
/**
* @description 创建简单的邮件对象
* @param context 邮件的内容
* @param sendTo 邮件的接收者
* @param sendFrom 发送邮件的人的邮箱
* @return 简单的邮件实例
*/
public static SimpleMailMessage write(String context, String sendTo, String sendFrom,String title) {
SimpleMailMessage simpleMessage = new SimpleMailMessage();
simpleMessage.setTo(sendTo);//邮件的接收者
simpleMessage.setFrom(sendFrom);//发送邮件的人的邮箱
simpleMessage.setText(context);//设置内容
simpleMessage.setSubject(title);//设置标题
return simpleMessage;
}
}
5.编写入口类
当前的
Application
类中的内容:
/**
* @description 使用SpringBoot发送Email
* @author hy
* @date 2019-08-13
*/
@RestController
@SpringBootApplication
public class Application {
@Autowired
JavaMailSender mailSender;
@RequestMapping("/sendEmail")
public String sendEmail(String sendFrom, String context, String sendTo, String title) {
SimpleMailMessage mailMessage = EmailMessageUtils.write(context, sendTo, sendFrom, title);
// 通过查询源码获得当前的send可以使用SimpleMailMessage发送一个简单的邮件
mailSender.send(mailMessage);
return "【发送邮箱成功!】";
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
6.测试
结果:成功,但是需要使用自己的账号和密码,并且开启那个许可才能使用。网上有就不发了!
7.总结
1.当前发送邮箱的时候需要开启一定的东西,并且需要自己的用户名和密码,还有对应的邮箱的端口
2.发送邮箱可以使用JavaMailSender
来发送邮箱,发送的邮件可以使用SimpleMailMessage
来创建
以上纯属个人见解,如有问题请联系本人!
本文标签: 邮箱SpringBootEmailqq
版权声明:本文标题:SpringBoot中发送Email(基于QQ邮箱版的) 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dianzi/1726720368a1081799.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论