Springboot邮件发送配置
pom.xml依赖:
<dependency><groupId>org.eclipse.angus</groupId><artifactId>jakarta.mail</artifactId><version>2.0.3</version>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>
yml:
email:user: 发送邮箱地址code: 邮箱授权码(QQ、网易邮箱等的授权码可百度获取)host: smtp.126.com或smtp.qq.com,不同邮箱配置不同auth: true
pojo/EmailProperties.java:
package com.ztr.springmail.pojo;import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;@Component
@ConfigurationProperties(prefix = "email")
public class EmailProperties {//发件人邮箱public String user ;//发件人邮箱授权码public String code ;//发件人邮箱对应的服务器域名,如果是163邮箱:smtp.163.com qq邮箱: smtp.qq.compublic String host ;//身份验证开关private boolean auth ;public String getHost() {return host;}public void setHost(String host) {this.host = host;}public boolean isAuth() {return auth;}public void setAuth(boolean auth) {this.auth = auth;}public String getUser() {return user;}public void setUser(String user) {this.user = user;}public String getCode() {return code;}public void setCode(String code) {this.code = code;}@Overridepublic String toString() {return "EmailProperties{" +"host='" + host + '\'' +", auth=" + auth +", user='" + user + '\'' +", code='" + code + '\'' +'}';}
}
utils/MailUtil.java:
package com.ztr.springmail.utils;import com.ztr.springmail.pojo.EmailProperties;
import jakarta.mail.*;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;import java.util.Properties;public class MailUtil {/*** 发送邮件* @param emailProperties 发件人信息(发件人邮箱,发件人授权码)及邮件服务器信息(邮件服务器域名,身份验证开关)* @param to 收件人邮箱* @param title 邮件标题* @param content 邮件正文* @return*/public static boolean sendMail(EmailProperties emailProperties, String to, String title, String content){MimeMessage message = null;try {Properties properties = new Properties();properties.put("mail.smtp.host", emailProperties.getHost());properties.put("mail.smtp.auth",emailProperties.isAuth());properties.put("mail.user", emailProperties.getUser());properties.put("mail.password", emailProperties.getCode());// 构建授权信息,用于进行SMTP进行身份验证Authenticator authenticator = new Authenticator() {@Overrideprotected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(emailProperties.getUser(), emailProperties.getCode());}};// 使用环境属性和授权信息,创建邮件会话Session mailSession = Session.getInstance(properties, authenticator);// 创建邮件消息message = new MimeMessage(mailSession);}catch (Exception e){e.printStackTrace();}//如果邮件创建失败,直接返回if (message==null){return false;}try {// 设置发件人InternetAddress form = new InternetAddress(emailProperties.getUser());message.setFrom(form);// 设置收件人InternetAddress toAddress = new InternetAddress(to);message.setRecipient(Message.RecipientType.TO, toAddress);// 设置邮件标题message.setSubject(title);// 设置邮件的内容体message.setContent(content, "text/html;charset=UTF-8");// 发送邮件Transport.send(message);}catch (Exception e){e.printStackTrace();}return true;}
}
service/EmailService.java:
package com.ztr.springmail.service;public interface EmailService {boolean send(String to,String title,String content);}
service/EmailServiceImpl.java:
package com.ztr.springmail.service.impl;import com.ztr.springmail.pojo.EmailProperties;
import com.ztr.springmail.service.EmailService;
import com.ztr.springmail.utils.MailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class EmailServiceImpl implements EmailService {//注入email配置信息实体类@Autowiredprivate EmailProperties emailProperties;/*** @param to 收件人邮箱* @param title 邮件标题* @param content 邮件正文* @return*/@Overridepublic boolean send(String to, String title, String content) {//打印email配置信息System.out.println(emailProperties);//发送邮件boolean flag = MailUtil.sendMail(emailProperties,to, title, content);return flag;}
}
controller/EmailController.java:
package com.ztr.springmail.controller;import com.ztr.springmail.service.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class EmailController {//注入email配置信息实体类@Autowiredprivate EmailService emailService;//测试方法@RequestMapping("/send")public Boolean send(){//收件人信箱String to = "Michealzou@126.com";//邮件标题String title = "测试邮件";//邮件正文String content = "邮件发送成功。。。";//发送邮件boolean flag = emailService.send(to,title,content);return flag;}}
启程程序,发送请求:localhost:8080/send
查看邮箱,收到测试邮件。