今天研究了一下怎麼用java代碼發送郵件,用的是Apache的commons-email包。html
聽說這個包是對javamail進行了封裝,簡化了操做。 這裏講一下具體用法吧java
須要進入到QQ郵箱或者是網易郵箱裏面去獲取。在郵箱的設置->帳戶裏面,開啓以下服務,就能獲得一個受權碼,這個受權碼要好好保管。有了這兩個東西就可以經過第三方客戶端發送郵件了。apache
我用的是1.4,也能夠去maven倉庫網站(https://mvnrepository.com)上面找別的版本。網絡
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-email</artifactId> <version>1.4</version> </dependency>
我在網上找了幾個案例,以下。maven
1.發送簡單文本郵件。這是最簡單也是最經常使用的。ide
/** * @describe 發送內容爲簡單文本的郵件 * @throws EmailException */ public static void sendSimpleTextEmail() throws EmailException { Email email = new SimpleEmail(); //設置主機名,QQ郵箱是"smtp.qq.com",網易郵箱是"smtp.163.com" email.setHostName("smtp.163.com"); // 用戶名和密碼爲郵箱的帳號和受權碼(不須要進行base64編碼) email.setAuthenticator(new DefaultAuthenticator("myemailaddress@163.com", "myshouquanma")); //設置SSL鏈接,這樣寫就對了 email.setSSLOnConnect(true); //設置來源,就是發送方的郵箱地址 email.setFrom("myemailaddress@163.com"); //設置主題,能夠不設置 email.setSubject("java發送郵件"); //設置信息,就是內容,這個必需要有 email.setMsg("這是測試郵件 ... :-)"); //接收人郵箱地址 email.addTo("receiveeraddress@qq.com"); email.send(); }
2.發送包含附件的郵件(附件爲本地資源),這裏用到了一個EmailAttachment對象,也就是附件的意思測試
/** * @describe 發送包含附件的郵件(附件爲本地資源) * @throws EmailException */ public static void sendEmailsWithAttachments() throws EmailException { // 建立一個attachment(附件)對象 EmailAttachment attachment = new EmailAttachment(); //設置上傳附件的地址 attachment.setPath("C:\\Users\\Administrator\\Pictures\\Saved Pictures\\conti.png"); attachment.setDisposition(EmailAttachment.ATTACHMENT); //這個描述能夠隨便寫 attachment.setDescription("Picture of conti"); //這個名稱要注意和文件格式一致,這將是接收人下載下來的文件名稱 attachment.setName("conti.png"); //由於要上傳附件,因此用MultiPartEmail()方法建立一個email對象,固定步驟都是同樣的 MultiPartEmail email = new MultiPartEmail(); email.setHostName("smtp.163.com"); email.setAuthenticator(new DefaultAuthenticator("myemailaddress@163.com", "myshouquanma")); email.setSSLOnConnect(true); email.addTo("receiveemail@qq.com", "Conti Zhang"); email.setFrom("myemailaddress@163.com", "Me"); email.setSubject("圖片"); email.setMsg("這是發送給你的圖片"); //將附件添加到郵件 email.attach(attachment); email.send(); }
3.發送包含附近的郵件(附件爲在線資源),這個與上傳本地附件稍有區別,注意一下就行網站
/** * @describe 發送包含附件的郵件(附件爲在線資源) * @throws EmailException * @throws MalformedURLException */ public static void sendEmailsWithOnlineAttachments() throws EmailException, MalformedURLException { EmailAttachment attachment = new EmailAttachment(); //設置在線資源路徑,和上傳本地附件的惟一區別 attachment.setURL(new URL("http://www.apache.org/images/asf_logo_wide.gif")); attachment.setDisposition(EmailAttachment.ATTACHMENT); attachment.setDescription("Apache logo"); attachment.setName("Apache logo.gif"); MultiPartEmail email = new MultiPartEmail(); email.setHostName("smtp.163.com"); email.setAuthenticator(new DefaultAuthenticator("myemailaddress@163.com", "myshouquanma")); email.setSSLOnConnect(true); email.addTo("receiveemail@qq.com", "Conti Zhang"); email.setFrom("myemailaddress@163.com", "Me"); email.setSubject("The logo"); email.setMsg("Here is Apache's logo"); email.attach(attachment); email.send(); }
4.發送內容爲HTML格式的郵件,有些郵件直接打開就是一個HTML頁面。雖然不必定用到,能夠了解一下編碼
/** * @describe 發送內容爲HTML格式的郵件 * @throws EmailException * @throws MalformedURLException */ public static void sendHTMLFormattedEmail() throws EmailException, MalformedURLException { // 這裏須要使用HtmlEmail建立一個email對象 HtmlEmail email = new HtmlEmail(); email.setHostName("smtp.163.com"); email.setAuthenticator(new DefaultAuthenticator("myemailaddresss@163.com", "myshouquanma")); email.addTo("receiveemail@qq.com", "Conti Zhang"); email.setFrom("myemailaddress@163.com", "Me"); email.setSubject("Test email with inline image"); // 嵌入圖像並獲取內容id,雖然案例這樣寫,但我感受直接在html內容裏面寫圖片網絡地址也能夠 URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif"); String cid = email.embed(url, "Apache logo"); // 設置html內容 email.setHtmlMsg("<html>The apache logo - <img src=\"cid:" + cid + "\"></html>"); // 設置替代內容,若是不支持html email.setTextMsg("你的郵件客戶端不支持html郵件"); email.send(); }
5.發送內容爲HTML格式的郵件(嵌入圖片更方便)url
這裏用到了DataSourceFileResolver對象,和DataSourceUrlResolver對象,前者能夠解析本地文件路徑,後者能夠解析網絡路徑
具體用法以下
/** * @describe 發送內容爲HTML格式的郵件(嵌入圖片更方便) * @throws MalformedURLException * @throws EmailException */ public static void sendHTMLFormattedEmailWithEmbeddedImages() throws MalformedURLException, EmailException { //html郵件模板
String htmlEmailTemplate = "<img src=\"http://www.conti.com/images/1.jpg\">"; DataSourceResolver[] dataSourceResolvers =new DataSourceResolver[]{new DataSourceFileResolver(),new DataSourceUrlResolver(new URL("http://"))}; email.setDataSourceResolver(new DataSourceCompositeResolver(dataSourceResolvers)); email.setHostName("smtp.qq.com"); email.setAuthenticator(new DefaultAuthenticator("myemailaddress@qq.com", "myshouquanma")); email.addTo("receiveemail@qq.com", "Conti Zhang"); email.setFrom("myemailaddress@qq.com", "Me"); email.setSubject("Test email with inline image"); email.setHtmlMsg(htmlEmailTemplate); email.setTextMsg("你的郵件客戶端不支持html郵件"); email.send(); }
此種方式可能會報錯,會被郵箱認爲是有害郵件不接收而致使發送失敗。解決方法就是,網易郵箱不行就換QQ郵箱,我就是這樣作的
好了,就這麼多,歡迎討論!