<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    我的漫漫程序之旅

    專注于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ā)送本機的附件,當(dāng)然我們也可以發(fā)送非本機的附件,如果發(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)即可

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

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


    posted on 2007-12-25 15:12 々上善若水々 閱讀(2001) 評論(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
    主站蜘蛛池模板: 久久这里只精品99re免费| 亚洲欧美日韩国产精品一区| 亚洲人成网77777亚洲色| 亚洲另类少妇17p| 亚洲一区二区三区无码影院| 亚洲国产天堂久久综合| 亚洲午夜日韩高清一区| 伊人久久大香线蕉亚洲五月天| 成人亚洲性情网站WWW在线观看| 中文字幕亚洲综合久久男男| 久久久久亚洲AV无码专区网站| 国产精品亚洲产品一区二区三区 | 牛牛在线精品免费视频观看| 日韩亚洲综合精品国产| 男人和女人高潮免费网站| 一级毛片在线免费播放| 国产一级一毛免费黄片| 一级毛片**不卡免费播| 一级毛片免费观看不卡视频| 亚州免费一级毛片| 免费看国产精品3a黄的视频| 好吊妞在线新免费视频| 国产免费人成视频在线观看 | av无码免费一区二区三区| 免费a级毛片高清视频不卡| 日韩午夜免费视频| 久久乐国产精品亚洲综合| 国产亚洲精品va在线| 亚洲精品视频观看| 亚洲狠狠婷婷综合久久| 伊人久久国产免费观看视频| 日本免费一区二区久久人人澡| 日本免费一区二区在线观看| 韩国18福利视频免费观看| 国产福利电影一区二区三区,亚洲国模精品一区 | 久久国产精品免费网站| 成人免费a级毛片| 亚洲M码 欧洲S码SSS222| 亚洲AV午夜福利精品一区二区 | 久久久久亚洲精品天堂久久久久久| 午夜亚洲www湿好大|