Java專案使用oh-my-email傳送郵件
本文使用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), "[email protected]", "your@password");
傳送Email
測試傳送text郵件
@Test public void testSendText() throws MessagingException { OhMyEmail.subject("這是一封測試TEXT郵件") .from("小姐姐的郵箱") .to("[email protected]") .text("信件內容") .send(); }
測試傳送html郵件
@Test public void testSendHtml() throws MessagingException { OhMyEmail.subject("這是一封測試HTML郵件") .from("小姐姐的郵箱") .to("[email protected]") .html("<h1 font=red>信件內容</h1>") .send(); }
測試傳送附件郵件
@Test public void testSendHtml() throws MessagingException { OhMyEmail.subject("這是一封測試HTML郵件") .from("小姐姐的郵箱") .to("[email protected]") .html("<h1 font=red>信件內容</h1>") .send(); }
測試傳送網路資源附件郵件
@Test public void testSendAttachURL() throws MessagingException { try { OhMyEmail.subject("這是一封測試網路資源作為附件的郵件") .from("小姐姐的郵箱") .to("[email protected]") .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(); } }