我的漫漫程序之旅

          專注于JavaWeb開發
          隨筆 - 39, 文章 - 310, 評論 - 411, 引用 - 0
          數據加載中……

          用Apache的Jakarta Commons-Email開源框架發送Email

          Jakarta發布了Commons Emails 1.1leased 版本,目的是為了簡化JavaMail。
          該項目主頁:http://commons.apache.org/email/
          知道有它幾個class嗎?你一定想不到,只有8個!

          好了,開始我們的jakarta commons emails 之旅:)

          一:Quick Start
          通過SimpleEmail發送郵件

          java.lang.Object
          org.apache.commons.mail.Email
             org.apache.commons.mail.SimpleEmail

          SimpleEmail email = new SimpleEmail();
          email.setHostName(
          "mail.4ya.cn");
          email.setAuthentication(
          "<username>","<password>")
          email.addTo(
          "martin.xus@gmail.com""martin");
          email.setFrom(
          "martin@4ya.cn""martin");
          email.setSubject(
          "測試主題");
          email.setMsg(
          "這里是郵件內容");
          email.send();

          就如代碼里字面上的意思一樣簡單:
          1:創建以SimpleEmail對象
          2:設定發送信件的smtp服務器,如果沒有設定,會尋找系統變量中mail.host值。
          3:設定smtp的用戶和密碼
          4:收件人
          5:發件人
          6:主題
          7:內容
          8:發送

          二:發送帶附件的郵件
          我們可以發送本機的附件,當然我們也可以發送非本機的附件,如果發送的是一個存在網絡上的附件的url,則郵件發送的時候會自動下載,添加到附件中。

             1:)發送本地附件:
          EmailAttachment attachment = new EmailAttachment();
          attachment.setPath(
          "test/test.rar");
          attachment.setDisposition(EmailAttachment.ATTACHMENT);
          attachment.setDescription(
          "python resource");
          attachment.setName(
          "resource");
          2:)發送不存在本地的附件
          EmailAttachment attachment = new EmailAttachment();
          attachment.setURL(
          new URL("/pic/2006/2/25/1340002.jpg"));
          attachment.setDisposition(EmailAttachment.ATTACHMENT);
          attachment.setDescription(
          "微笑圖書館");
          attachment.setName(
          "微笑圖書館");
          next,添加附件到我們的郵件中
          MultiPartEmail email = new MultiPartEmail();
          email.setHostName(
          "mail.4ya.cn");
              email.setAuthentication(
          "<username>","<password>")
          email.addTo(
          "martin.xus@gmail.com""martin");
          email.setFrom(
          "martin@4ya.cn""martin");
          email.setSubject(
          "郵件主題");
          email.setMsg(
          "郵件內容");

          //添加附件
          email.attach(attachment);

          //發送郵件
          email.send();

          如果需要發送多個附件,只需創建多個EmailAttachement,即可
          email.attach(attachment1)
          email.attach(attachment2)

          三:發送html格式的郵件
          通過HtmlEmail我們可以發送Html格式的郵件:
          java.lang.Object
            org.apache.commons.mail.Email
              org.apache.commons.mail.MultiPartEmail
                    org.apache.commons.mail.HtmlEmail

          如下:
           //HtmlEmail!
           HtmlEmail email = new HtmlEmail();
           email.setHostName(
          "mail.4ya.cn");
              email.setAuthentication(
          "<username>","<password>")
           email.addTo(
          "martin@4ya.cn"martin");
           email.setFrom("martin.xus@gmail.com"martin");
           email.setSubject("主題:該郵件包括html格式內容");
           
           
          // embed the image and get the content id
           
          // 注意這里:embed 將幫助我們創建標簽如:cid:xxx url
          URL url = new URL("/pic/2006/2/25/1340003.gif");
          String cid 
          = email.embed(url, "Apache logo");

          /** *//**
          set the html message
          我們看到HtmlEmail extends Email的,它依然有setMsg(),但是這里發送的郵件包括了插入在郵件內容中的圖片,所以不能在使用了setMsg(),而要以setHtmlMsg 或setTextMsg代碼
          *
          */

          email.setHtmlMsg(
          "<html>The apache logo - <img src=\"cid:"+cid+"\"></html>");

          // set the alternative message
          email.setTextMsg("Your email client does not support HTML messages");
          //set mail
          email.send();

          四:最后一步
          如果需要實現更復雜authenticator 你可以extends javax.mail.Authenticator ,實現你自己的東西,然后調用Email.setAuthenticator(javax.mail.Authenticator newAuthenticator)即可

          這一點jakarta也做了,給我們提供了一個defaultAuthenticator
          java.lang.Object
            javax.mail.Authenticator
                org.apache.commons.mail.DefaultAuthenticator

          覆蓋掉該方法,實現你自己的東東 o_o
          protected javax.mail.PasswordAuthentication getPasswordAuthentication()
          如果需要運行在web容器里,可能需要輔助的mail.jar和activation.jar包.
          可以到我的網盤下載;下載


          posted on 2007-12-25 15:12 々上善若水々 閱讀(2006) 評論(1)  編輯  收藏 所屬分類: JavaWeb

          評論

          # Vnutrennyaya Optimizaciya  回復  更多評論   

          Excuse me. Everybody knows if you are too careful you are so occupied in being careful that you are sure to stumble over something.
          I am from Bolivia and bad know English, give true I wrote the following sentence: "Apr if you using wordpress for your niche websites or blogs, this plugin can help you optimize your blog post urls for search engines.Learn seo or hire hobo to do it for you."

          Waiting for a reply :), Gilbert.
          2009-05-18 20:48 | Vnutrennyaya Optimizaciya
          主站蜘蛛池模板: 广水市| 澄迈县| 朝阳区| 英山县| 靖宇县| 宜宾市| 丹东市| 长沙市| 东丰县| 淮南市| 甘肃省| 丰镇市| 义乌市| 进贤县| 东乡县| 孟津县| 乳山市| 方城县| 北流市| 东源县| 德格县| 平湖市| 漠河县| 凤城市| 海兴县| 都兰县| 广宁县| 枣强县| 凤庆县| 佛教| 沙坪坝区| 芜湖县| 屏山县| 改则县| 怀宁县| 诸暨市| 全南县| 长兴县| 金湖县| 潜山县| 平湖市|