Spring Boot筆記(二) springboot 集成 SMTP 發送郵件


個人博客網:https://wushaopei.github.io/    (你想要這里多有)

筆記:乘着項目迭代的間隙,把一些可復用的功能從項目中抽取出來,這是其中之一,

一、添加SMTP 及 MAIL 的 pom.xml 依賴

               <!--郵箱發送-->

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-mail</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>

注意: 依賴添加后會要求刷新 springBootConguration 的配置信息,可執行也可不理睬

具體操作如下:

二、配置yml郵箱參數

1、在 application-dev.yml中配置對應的郵箱參數


#郵件配置 email: host: smtp.163.com username: 1598974689@163.com password: 1598974689wsp senderName: 1598974689@163.com #發送提醒郵件的接收方 sendToMail: 1862030785@foxmail.com sendToPeopleName: wen

2、由於是使用SMTP實現郵箱發送,除了提供必要的郵箱、密碼等參數外,還要開啟郵箱賬號的 SMTP 設置,

打勾選中要設置的服務項,通過校驗短信息設置授權碼,使用授權碼代替  郵箱密碼 實現郵件的發送,流程很簡單的。

三、相關工具類和 實體類的創建

1、郵箱 參數 config

package com.example.poiutis.model.common; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; /** * @ClassName EmailConfig * @Description TODO * @Author wushaopei * @Date 2019/7/25 10:24 * @Version 1.0 */ @Configuration @ConfigurationProperties(prefix = "email", ignoreUnknownFields = false) //@PropertySource("classpath:/application.yml") public class EmailConfig { private String host; private String username; private String password; private String senderName; public String getHost() { return host; } public void setHost(String host) { this.host = host; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getSenderName() { return senderName; } public void setSenderName(String senderName) { this.senderName = senderName; } @Override public String toString() { return "EmailConfig{" + "host='" + host + '\'' + ", username='" + username + '\'' + ", password='" + password + '\'' + ", senderName='" + senderName + '\'' + '}'; } } 

2、 Email 發送實現工具類

package com.example.poiutis.utils.Email; import com.example.poiutis.model.common.EmailConfig; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Component; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.util.Properties; /** * @ClassName MailSenderFacade * @Description TODO * @Author wushaopei * @Date 2019/7/25 10:22 * @Version 1.0 */ @Component public class MailSenderFacade { @Autowired private EmailConfig emailConfig; private final Logger logger = LoggerFactory.getLogger(MailSenderFacade.class); public void sendMailHtml(final String toEmail,final String subject,final String text){ try{ JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl(); javaMailSender.setUsername(emailConfig.getUsername()); javaMailSender.setPassword(emailConfig.getPassword()); javaMailSender.setHost(emailConfig.getHost()); javaMailSender.setPort(25); javaMailSender.setProtocol("smtp"); javaMailSender.setDefaultEncoding("UTF-8"); Properties mailProperties = new Properties(); mailProperties.put("mail.smtp.auth", true); mailProperties.put("mail.smtp.starttls.enable", true); javaMailSender.setJavaMailProperties(mailProperties); MimeMessage mimeMessage = getMimeMessage(toEmail,subject,text, javaMailSender); javaMailSender.send(mimeMessage); logger.info("發往 "+toEmail+" 郵件發送成功"); } catch (MessagingException e) { logger.error("發往 "+toEmail+" 郵件發送異常", e); } } private MimeMessage getMimeMessage(String toEmail,String subject,String text, JavaMailSenderImpl javaMailSender) throws MessagingException { MimeMessage mimeMessage = javaMailSender.createMimeMessage(); MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8"); mimeMessageHelper.setFrom(emailConfig.getSenderName()); mimeMessageHelper.setTo(toEmail); mimeMessageHelper.setSubject(subject); mimeMessageHelper.setText(text, true); return mimeMessage; } } 

四、測試SMTP功能發送一個郵件試試

 @RequestMapping(value = "/please/emailTest", method = RequestMethod.GET) public Object getEmailTest() { Map<String,Object> map = null; try{ map = new HashMap<>(); mailSenderFacade.sendMailHtml("18620307785@163.com", "來自郵箱測試接口郵件", "來自郵箱測試接口郵件"); map.put("state","0"); map.put("message", "導出成功"); }catch (Exception e){ map.put("state","1"); map.put("message", "導出失敗"); e.printStackTrace(); } return map; }

發送成功:

收到的郵件:

GitHub


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM