採用commons-emai寄送電子郵件

來源:互聯網
上載者:User
Jakarta Commons-Email 1.0 版本發布了。 

    Commons-Email 提供了一套更簡潔的 操作 郵件的API. 

    Commons-Email 是 基於 JavaMail API 構建. 核心組建只包含很少的幾個classes. 

    下面是一些其中的類的簡單介紹: 

    SimpleEmail - This class is used to send basic text based emails. 

    MultiPartEmail - This class is used to send multipart messages. This allows a text message with attachments either inline or attached. 

    HtmlEmail - This class is used to send HTML formatted emails. It has all of the capabilities as MultiPartEmail allowing attachments to be easily added. It also supports embedded images. 

    EmailAttachment - This is a simple container class to allow for easy handling of attachments. It is for use with instances of MultiPartEmail and HtmlEmail. commons-email是apache提供的一個開源的API,是對javamail的封裝,因此在使用時要將javamail.jar加到class path中,主要包括SimpleEmail,MultiPartEmail,HtmlEmail,EmailAttachment四個類。
 
SimpleEmail:發送簡單的email,不能添加附件
MultiPartEmail:文本郵件,可以添加多個附件
HtmlEmail:HTML格式郵件,同時具有MultiPartEmail類所有“功能”
EmailAttchment:附件類,可以添加本地資源,也可以指定網路上資源,在發送時自動將網路上資源下載發送。
 
發送基本文字格式設定郵件:
==============
SimpleEmail email = new SimpleEmail();
//smtp host
email.setHostName("mail.test.com");
//登陸郵件伺服器的使用者名稱和密碼
email.setAuthentication("test","testpassword");
//接收人
email.addTo("jdoe@somewhere.org", "John Doe");
//發送人
email.setFrom("me@apache.org", "Me");
//標題
email.setSubject("Test message");
//郵件內容
email.setMsg("This is a simple test of commons-email");
//發送
email.send();發送文字格式設定,帶附件郵件:
==================
//附件,可以定義多個附件對象
EmailAttachment attachment = new EmailAttachment();
attachment.setPath("e://1.pdf");
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("Picture of John");
//
MultiPartEmail email = new MultiPartEmail();
//smtp host
email.setHostName("mail.test.com");
//登陸郵件伺服器的使用者名稱和密碼
email.setAuthentication("test","testpassword");
//接收人
email.addTo("jdoe@somewhere.org", "John Doe");
//發送人
email.setFrom("me@apache.org", "Me");
//標題
email.setSubject("Test message");
//郵件內容
email.setMsg("This is a simple test of commons-email");
//添加附件
email.attach(attachment);
//發送
email.send();
 
發送HTML格式帶附件郵件:
=================
//附件,可以定義多個附件對象
EmailAttachment attachment = new EmailAttachment();
attachment.setPath("e://1.pdf");
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("Picture of John");
//
HtmlEmail email = new HtmlEmail ();
//smtp host
email.setHostName("mail.test.com");
//登陸郵件伺服器的使用者名稱和密碼
email.setAuthentication("test","testpassword");
//接收人
email.addTo("jdoe@somewhere.org", "John Doe");
//發送人
email.setFrom("me@apache.org", "Me");
//標題
email.setSubject("Test message");
//郵件內容
email.setHtmlMsg("<b>This is a simple test of commons-email</b>");
//添加附件
email.attach(attachment);
//發送
email.send();

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.