本文使用Github開源項目oh-my-email進行測試郵件發送,並未進行更為深度的測試,如果想要快速使用,的確是一個很好的郵件發送組件。https://github.com/biezhi/oh-my-email
oh-my-email倉庫地址
<dependency>
<groupId>io.github.biezhi</groupId>
<artifactId>oh-my-email</artifactId>
<version>0.0.4</version>
</dependency>
配置oh-my-email
// 配置一次即可,可以配置為靜態方法
OhMyEmail.config(SMTP_QQ(false), "xxxx@qq.com", "your@password");
發送Email
測試發送text郵件
@Test
public void testSendText() throws MessagingException {
OhMyEmail.subject("這是一封測試TEXT郵件")
.from("小姐姐的郵箱")
.to("xxxx@gmail.com")
.text("信件內容")
.send();
}
測試發送html郵件
@Test
public void testSendHtml() throws MessagingException {
OhMyEmail.subject("這是一封測試HTML郵件")
.from("小姐姐的郵箱")
.to("xxxx@gmail.com")
.html("<h1 font=red>信件內容</h1>")
.send();
}
測試發送附件郵件
@Test
public void testSendHtml() throws MessagingException {
OhMyEmail.subject("這是一封測試HTML郵件")
.from("小姐姐的郵箱")
.to("xxxx@gmail.com")
.html("<h1 font=red>信件內容</h1>")
.send();
}
測試發送網絡資源附件郵件
@Test
public void testSendAttachURL() throws MessagingException {
try {
OhMyEmail.subject("這是一封測試網絡資源作為附件的郵件")
.from("小姐姐的郵箱")
.to("xxxx@gmail.com")
.html("<h1 font=red>信件內容</h1>")
.attachURL(new URL("https://avatars1.githubusercontent.com/u/2784452?s=40&v=4"), "測試圖片.jpeg")
.send();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}