業務場景
- 是不是伺服器 down 了?
- 爬蟲的 Dom 解析沒有解析到內容?
- 用戶註冊
- 消息通知(代碼異常通知等)
郵件伺服器與傳輸協議
- 要在網路上實現郵件功能,必須要有專門的郵件伺服器。這些郵件伺服器類似於現實生活中的郵局,它主要負責接收用戶投遞過來的郵件,並把郵件投遞到郵件接收者的電子郵箱中。
- SMTP伺服器地址:一般是 smtp.xxx.com,比如163郵箱是smtp.163.com,qq郵箱是smtp.qq.com。
- SMTP協議
- 通常把處理用戶smtp請求(郵件發送請求)的伺服器稱之為SMTP伺服器(郵件發送伺服器)。
- POP3協議
- 通常把處理用戶pop3請求(郵件接收請求)的伺服器稱之為POP3伺服器(郵件接收伺服器)。
Java發送郵件

好了,基本原理和業務場景搞清楚了,下來以QQ郵箱作為案例(163等其他郵箱也是類似的),基於SSM框架的,SpringBoot同理,當然SSM框架都會配置了SpringBoot還不是手到擒來
開啟SMTP服務

點擊設置—— 賬戶

請記住這串編號,後面的配置會用到

導入依賴 關於spring的依賴自行刪減
<!--郵件發送-->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.1</version>
</dependency>
<!--引入spring的上下文jar-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.22.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
工程目錄結構

設置配置文件 mail.properties
在163郵箱中同上的申請配置,可能和QQ頁面所在位置不一樣,請自行查找(基本都是一樣的)
#伺服器主機名QQ郵箱 smtp.xx.com 根據自己郵箱的使用自行設置 163郵箱: smtp.163.com
mail.smtp.host=smtp.qq.com
#自己的郵箱
mail.smtp.username=********@qq.com
#密碼/客戶端授權碼 這裡的授權碼就是剛才在郵箱中生成的
mail.smtp.password=********
#編碼字元
mail.smtp.defaultEncoding=utf-8
#是否進行用戶名密碼校驗
mail.smtp.auth=true
#設置超時時間
mail.smtp.timeout=20000spring-.xml 有些內容自行刪減 這是我copy過來的
spring-core.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--讀取屬性文件-->
<context:property-placeholder location="classpath:mail.properties"/>
<!--配置郵件介面-->
<bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="${mail.smtp.host}"/>
<property name="username" value="${mail.smtp.username}"/>
<property name="password" value="${mail.smtp.password}"/>
<property name="defaultEncoding" value="${mail.smtp.defaultEncoding}"/>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
<prop key="mail.smtp.timeout">${mail.smtp.timeout}</prop>
</props>
</property>
</bean>
<context:component-scan base-package="com.*">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>
SendEmailController
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.IOException;
import java.util.Properties;
/**
* @Description: 發送郵件
* @Author: 張楚涵
* @Date: 2019/8/14 0014 15:58
* @version:1.0.0
*/
@RestController
public class SenEmailController {
@Autowired
private JavaMailSender javaMailSender;//在spring中配置的郵件發送的bean
@RequestMapping(value = "/send",method = RequestMethod.GET,produces = "text/html; charset=utf-8")
public Object sendMail03(){
MimeMessage mMessage=javaMailSender.createMimeMessage();//創建郵件對象
MimeMessageHelper mMessageHelper;
Properties prop = new Properties();
String from;
try {
//從配置文件中拿到發件人郵箱地址
//根據自己的目錄設置
prop.load(this.getClass().getClassLoader().getResourceAsStream("mail.properties"));
from = prop.get("mail.smtp.username")+"";
mMessageHelper=new MimeMessageHelper(mMessage,true);
// mMessageHelper.setFrom(from);//發件人郵箱
// 第二個參數是你想發送郵件時想用的名字
mMessageHelper.setFrom(new InternetAddress(from, "###", "UTF-8"));
mMessageHelper.setTo("*******@qq.com");//收件人郵箱
mMessageHelper.setSubject("******");//郵件的主題
mMessageHelper.setSubject("Spring的郵件發送");//郵件的主題
//郵件的文本內容,true表示文本以html格式打開
mMessageHelper.setText("<p>這是使用spring的郵件功能發送的一封郵件</p><br/>" +
"<a href='https://blog.csdn.net/qq_41840847'>打開我的博客主頁</a><br/>" +
"<img src='cid:fengye'>",true);
/*
File file=new File("F:/img/mr.png");//在郵件中添加一張圖片
FileSystemResource resource=new FileSystemResource(file);
mMessageHelper.addInline("fengye", resource);//這裡指定一個id,在上面引用
mMessageHelper.addAttachment("mr.png", resource);//在郵件中添加一個附件
*/
javaMailSender.send(mMessage);//發送郵件
} catch (MessagingException e) {
e.printStackTrace();
return "發送失敗";
} catch (IOException e) {
e.printStackTrace();
}
return "發送成功";
}
}
測試

原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/269375.html
微信掃一掃
支付寶掃一掃