<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ā)送本機(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();

    四:最后一步
    如果需要實(shí)現(xiàn)更復(fù)雜authenticator 你可以extends javax.mail.Authenticator ,實(shí)現(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

    覆蓋掉該方法,實(shí)現(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 々上善若水々 閱讀(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
    主站蜘蛛池模板: 亚洲天天做日日做天天欢毛片| 亚洲人成色99999在线观看| 日韩免费视频播放| 99精品视频免费在线观看| 猫咪免费人成网站在线观看入口 | 亚洲熟妇久久精品| 亚洲视频在线视频| 亚洲日韩国产精品第一页一区| 国产成人啪精品视频免费网| 男人的好免费观看在线视频| 久久久久久毛片免费播放| 成全视成人免费观看在线看| 免费无遮挡无遮羞在线看| 亚洲精品精华液一区二区| 亚洲一级高清在线中文字幕| 亚洲午夜在线电影| 亚洲AV无码国产丝袜在线观看| 久久亚洲高清综合| 亚洲无线一二三四区手机| mm1313亚洲精品无码又大又粗 | 亚洲国产精品一区二区三区在线观看 | 中文字幕亚洲不卡在线亚瑟| 免费在线一级毛片| 亚洲av区一区二区三| 国产大片91精品免费看3| 国产亚洲精品免费| 国产一级淫片视频免费看 | 国产精品亚洲va在线观看| 亚洲乱色伦图片区小说| 亚洲熟妇无码一区二区三区导航| 亚洲一区二区免费视频| 亚洲一区二区三区在线观看蜜桃| 亚洲天堂中文字幕在线观看| 亚洲成a人片毛片在线| 亚洲av无码不卡久久| 亚洲视频无码高清在线| 亚洲欧洲无卡二区视頻| 色欲aⅴ亚洲情无码AV蜜桃| 日韩色日韩视频亚洲网站| 精品成人一区二区三区免费视频 | 色www永久免费视频|