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();