由于gmail邮箱发送邮对安全性校验比较高,因此,首先设置
https://myaccount.google.com/lesssecureapps?pli=1,需要开启“允许不够安全的应用”
接下来就可以开始代码编写了。
pom.xml文件中引入架包:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> <version>2.1.5.RELEASE</version> </dependency>
封装实现
class EmailClient {
/* * 通过gmail邮箱发送邮件 */ public static void gmailSender(String email) { // Get a Properties object Properties props = new Properties(); //选择ssl方式 gmailssl(props); final String sendEmail = "####@gmail.com";//gmail邮箱 final String password = "******";//密码 Session session = Session.getDefaultInstance(props, new Authenticator() { protected PasswordAuthent