apache commons-email發送郵件亂碼問題__亂碼問題

來源:互聯網
上載者:User

很早前我們用mail.jar 和 activation.jar 兩個包編寫發送Email的代碼,雖然不難,但麻煩。 現在 apache 網站有一個 jarkata/commons/email 子項目,也為我們實現了發送 Email 的功能,在 http://jakarta.apache.org/commons/email/ 把包 commons-email-1.0.jar 下來,自己要寫的代碼就十分少了,並且非常明了。 這個包的大小隻有23K,也就是9個類而已,卻能讓您省不少事。

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("This is a simple test of commons-email"); //添加附件 email.attach(attachment); //發送
下面提供一個完整的程式樣本:


import org.apache.commons.mail.*; public class SendEMail { public static void main ( String[] arg ) throws Exception { SimpleEmail email = new SimpleEmail ( ); // smtp host email.setHostName ( "smtp.sina.com" ); // 登陸郵件伺服器的使用者名稱和密碼 email.setAuthentication ( "tomcat", "123456" ); // 接收人 email.addTo ( "tomcat@yahoo.com.cn", "Zieckey" ); // 發送人 email.setFrom ( "tocat@sina.com", "Me" ); // 標題 email.setSubject ( "hello" ); // 郵件內容 email.setMsg ( "This is a simple test of commons-email" ); // 發送 email.send ( ); System.out.println ( "Send email successful!" ); } }

commons-email 提供了 SimpleEmail、MultiPartEmail、HtmlEmail、EmailAttachment 等類,只需要您按正常思維簡單的寫幾行代碼就能發各種類型的 Email,一般我們用 JavaMail 發送 Email 會碰到中文亂碼問題,主要是出現在把代碼放在英文系統中執行時,處理方法是主題和內容使用 GBK 或 UTF-8 字元集。

在 http://jakarta.apache.org/commons/email/userguide.html 有 commons-email 的使用樣本,如果 直接用第一個例子放在英文環境中發送帶中文主題或內容的郵件也會出現亂碼,

這裡對第一個例子稍做改造,可以讓發出的 Email 中文不出現亂碼,如下:

package com.unmi; import org.apache.commons.mail.SimpleEmail; public class MailTo { /** * @Author Unmi * @param args * @throws Exception */ public static void main(String[] args) throws Exception { //發送簡單郵件 SimpleEmail email = new SimpleEmail(); email.setHostName("mail.2911.net"); //需要郵件發送伺服器驗證,使用者名稱/密碼 email.setAuthentication("broodwar", "xxxxxx"); email.addTo("fantasia@sina.com", "fantasia"); email.setFrom("broodwar@2911.net", "Broodwar"); //設定主題的字元集為UTF-8 email.setCharset("UTF-8"); email.setSubject("測試郵件主題"); email.buildMimeMessage(); //設定內容的字元集為UTF-8,先buildMimeMessage才能設定內容文本 email.getMimeMessage().setText("測試郵件內容","UTF-8"); email.sendMimeMessage(); } }


在以後在java中發郵件就用這個 commons-email 組件就好了,如果要發送 HTML 郵件或者帶附件的郵件就學著 http://jakarta.apache.org/commons/email/userguide.html 中的例子做即可,出現中文亂碼,跟到原始碼中想想辦法自己動動腦。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.