1. pom.xml文件中引入依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2. 接口及實現類
2.1 接口
複製代碼
package com.yuhuashang.demo.mail;
/**
* @Author:
* @Date: 2018/12/5 15:28
* @Description: 郵件服務的接口
*/
public interface MailService {
public void sendSimpleMail(String to, String subject, String content);
}
複製代碼
2.2 實現類
複製代碼
package com.yuhuashang.demo.mail.impl;
import com.yuhuashang.demo.mail.MailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;
/**
* @Author:
* @Date: 2018/12/5 15:23
* @Description: 郵件服務的實現類
*/
@Component
public class MailServiceImpl implements MailService{
private final Logger logger = (Logger)
LoggerFactory.getLogger(this.getClass());
@Autowired
private JavaMailSender mailSender;
@Value("${mail.fromMail.addr}")
private String from;
@Override
public void sendSimpleMail(String to, String subject, String content) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from);
message.setTo(to);
message.setSubject(subject);
message.setText(content);
try {
mailSender.send(message);
logger.info("簡單郵件已經發送。");
}catch (Exception e) {
logger.error("發送簡單郵件時發生異常!", e);
}
}
}
複製代碼
3. application.properties的配置
複製代碼
# 發送一方郵件信息
mail.fromMail.addr=XXXXX
spring.mail.host=XXXXX
spring.mail.password=XXXXX
spring.mail.properties.smtp.auth=true
spring.mail.properties.smtp.timeout=25000
spring.mail.username=XXXXXX
複製代碼
4. 常用郵箱的配置
4.1 QQ個人郵箱
複製代碼
# 利用QQ個人郵箱發送郵件
[email protected]
spring.mail.host=smtp.qq.com
spring.mail.password=XXXXXXX // 授權密碼,非登錄密碼
spring.mail.properties.smtp.auth=true
spring.mail.properties.smtp.timeout=25000
[email protected]
複製代碼
注意: spring.mail.password使用的開啓stmp時,QQ給的授權碼,目前需要發送短信開啓。
4.2 163個人郵箱
複製代碼
# 利用163個人郵箱發送郵件
[email protected]
spring.mail.host=smtp.163.com
spring.mail.password=XXXXXXX // 授權密碼,非登錄密碼
spring.mail.properties.smtp.auth=true
spring.mail.properties.smtp.timeout=25000
[email protected]
複製代碼
注意: spring.mail.password使用的開啓stmp時,163的授權碼,目前需要自己設定。
4.3 阿里雲個人郵箱
複製代碼
# 利用aliyun個人郵箱發送
[email protected]
spring.mail.host=smtp.aliyun.com
spring.mail.password=XXXXXX // 默認開啓,填登陸密碼即可
spring.mail.properties.smtp.auth=true
spring.mail.properties.smtp.timeout=25000
[email protected]
複製代碼
注意: spring.mail.password使用的是登陸密碼,阿里雲的stmp默認開啓的,我在郵箱上沒有發現可以開啓或關閉的地方,自己用的登陸密碼試過,是可以用的。
4.4 QQ企業郵箱
複製代碼
# 利用QQ企業郵箱發送郵件
[email protected]
spring.mail.host=smtp.www.quwanyule157.com exmail.qq.com
spring.mail.password=XXXXXXX // 授權密碼,非登錄密碼
spring.mail.www.dfgjpt.com properties.smtp.auth=true
spring.mail.properties.smtp.timeout=25000
[email protected]
複製代碼
注意: spring.mail.password使用的是授權碼,非登陸密碼,最近沒有用QQ企業郵箱,之前實現過,目前應該是這樣的。
4.5 163企業郵箱
複製代碼
# 利用163企業郵箱發送郵件
[email protected]
spring.mail.host=smtp.qiye.163.com
spring.mail.password=XXXXXXX // 授權密碼,非登錄密碼
spring.mail.properties.smtp.auth=true
spring.mail.properties www.mhylpt.com.smtp.www.gcyL157.com timeout=25000
[email protected]
複製代碼
注意: spring.mail.password使用的是授權碼,非登陸密碼,由於沒有使用過163企業郵箱,故不能保證可用性,如果有用過的朋友,可以給箇中肯的答案。
4.6 阿里雲企業郵箱
複製代碼
# 利用aliyun企業郵箱發送
[email protected]
spring.mail.host=smtp.mxhichina.com
spring.mail.password=XXXXX // 默認開啓,填登陸密碼即可
spring.mail.properties.smtp.auth=true
spring.mail.properties.smtp.timeout=25000
[email protected]
複製代碼
注意: spring.mail.password使用的是登陸密碼,目前正使用阿里雲企業郵箱,自己親自測試過,目前是可以用的,填登陸密碼即可。
分類: Spring-Boot系列,Java常用功能總結
Spring Boot實現郵件服務,附常見郵箱的配置
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
瞭解Spring之BeanDefinition對象
流年與你作伴之人
2019-02-24 14:04:20
Spring學習之對JDBC的支持
s5650326
2019-02-23 12:59:04
JEESZ分佈式框架簡介---技術介紹文檔
qq593e3b758e013
2019-02-23 12:55:03
聲明式事務
liudajiang
2019-02-23 12:53:51
Dubbo與Zookeeper、SpringMVC整合和使用(負載均衡、容錯)
mengmeng89012
2019-02-23 00:33:41
Spring Cloud雲架構 - SSO單點登錄之OAuth2.0登錄認證(1)
mingliabc123
2019-02-23 00:28:04
Spring Cloud雲架構 - SSO單點登錄之OAuth2.0登錄認證(1)
mingliabc123
2019-02-23 00:27:49
Spring Cloud雲服務架構 - commonservice-eureka 項目過程構建
mingliabc123
2019-02-23 00:27:48
Spring Cloud微服務分佈式雲架構 - spring cloud集成項目
mingliabc123
2019-02-23 00:27:48
Spring Cloud微服務架構代碼結構詳細講解
mingliabc123
2019-02-23 00:27:47
Spring Cloud自定義引導屬性源
mingliabc123
2019-02-23 00:27:47
Spring Cloud Config 加密和解密
mingliabc123
2019-02-23 00:27:47
spring cloud微服務分佈式雲架構簡介
mingliabc123
2019-02-23 00:27:46
Spring Cloud構建微服務架構—服務網關過濾器
mingliabc123
2019-02-23 00:27:38