使用Apache commons email發送郵件

今天研究了一下怎麼用java代碼發送郵件,用的是Apache的commons-email包。

據說這個包是對javamail進行了封裝,簡化了操作。 這裏講一下具體用法吧

 

一.首先你需要有郵箱賬號和一個授權碼。

需要進入到QQ郵箱或者是網易郵箱裏面去獲取。在郵箱的設置->賬戶裏面,開啟如下服務,就能得到一個授權碼,這個授權碼要好好保管。有了這兩個東西就能夠通過第三方客戶端發送郵件了。

 

二.導入commons-email的maven依賴。

我用的是1.4,也可以去maven倉庫網站(https://mvnrepository.com)上面找別的版本。

<dependency>
     <groupId>org.apache.commons</groupId>
     <artifactId>commons-email</artifactId>
     <version>1.4</version>
 </dependency>

三.然後就可以寫發送郵件的代碼了。

我在網上找了幾個案例,如下。

 1.發送簡單文本郵件。這是最簡單也是最常用的。

    /**
     * @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格式的郵件(嵌入圖片更方便)

這裏用到了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郵箱,我就是這樣做的

 

好了,就這麼多,歡迎討論!

 

本站聲明:網站內容來源於博客園,如有侵權,請聯繫我們,我們將及時處理

【其他文章推薦】

USB CONNECTOR掌控什麼技術要點? 帶您認識其相關發展及效能

台北網頁設計公司這麼多該如何選擇?

※智慧手機時代的來臨,RWD網頁設計為架站首選

※評比南投搬家公司費用收費行情懶人包大公開

※回頭車貨運收費標準