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
查看郵箱,收到測試郵件。