我的漫漫程序之旅

          專注于JavaWeb開發(fā)
          隨筆 - 39, 文章 - 310, 評論 - 411, 引用 - 0
          數(shù)據(jù)加載中……

          用Apache的Jakarta Commons-Email開源框架發(fā)送Email

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

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

          一:Quick Start
          通過SimpleEmail發(fā)送郵件

          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(
          "這里是郵件內(nèi)容");
          email.send();

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

          二:發(fā)送帶附件的郵件
          我們可以發(fā)送本機(jī)的附件,當(dāng)然我們也可以發(fā)送非本機(jī)的附件,如果發(fā)送的是一個存在網(wǎng)絡(luò)上的附件的url,則郵件發(fā)送的時候會自動下載,添加到附件中。

             1:)發(fā)送本地附件:
          EmailAttachment attachment = new EmailAttachment();
          attachment.setPath(
          "test/test.rar");
          attachment.setDisposition(EmailAttachment.ATTACHMENT);
          attachment.setDescription(
          "python resource");
          attachment.setName(
          "resource");
          2:)發(fā)送不存在本地的附件
          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(
          "郵件內(nèi)容");

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

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

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

          三:發(fā)送html格式的郵件
          通過HtmlEmail我們可以發(fā)送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格式內(nèi)容");
           
           
          // embed the image and get the content id
           
          // 注意這里:embed 將幫助我們創(chuàng)建標(biāo)簽如: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(),但是這里發(fā)送的郵件包括了插入在郵件內(nèi)容中的圖片,所以不能在使用了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();

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

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

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


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

          評論

          # Vnutrennyaya Optimizaciya  回復(fù)  更多評論   

          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
          主站蜘蛛池模板: 郑州市| 紫阳县| 井研县| 宣武区| 景宁| 吉水县| 许昌县| 米林县| 福安市| 安乡县| 嘉义县| 高碑店市| 驻马店市| 临西县| 通化市| 黑山县| 兴城市| 昌平区| 绥江县| 开封市| 抚顺市| 屯门区| 文登市| 锦州市| 珠海市| 靖州| 略阳县| 温州市| 申扎县| 庄河市| 浠水县| 观塘区| 新民市| 梁山县| 湛江市| 巴南区| 青海省| 西吉县| 墨竹工卡县| 石景山区| 新野县|