淺析Java Mail無法解析帶分號的收件人列表的問題
更新時間:2013年08月30日 09:30:34 作者:
JAVA MAIL嚴格按照RFC 822規(guī)范進行操作,沒有對分號做處理。大多數(shù)郵件服務器都是嚴格遵循RFC 822規(guī)范的
今天同事碰到一個問題,使用JAVA MAIL收取郵件時,如果收件人是個列表,且收件人列表是以分號進行分割的,則JAVA MAIL就會出現(xiàn)異常,不能正常解析,抽空看了一眼,自己寫了個簡單demo,很簡單,例如:
復制代碼 代碼如下:
@Test
public void testReceiveMail() {
try {
String host = "pop3.163.com";
Properties pops = new Properties();
pops.put("mail.pop3.host", host);
pops.put("mail.pop.auth", "true");
Session session = Session.getDefaultInstance(pops, null);
Store store = session.getStore("pop3");
//連接郵件服務器
store.connect(host, "chb_go", "3870359346");
//收取收件箱
Folder inbox = store.getDefaultFolder().getFolder("INBOX");
//只讀足夠了
inbox.open(Folder.READ_ONLY);
//得到所有郵件列表
Message[] msg = inbox.getMessages();
FetchProfile profile = new FetchProfile();
profile.add(FetchProfile.Item.ENVELOPE);
inbox.fetch(msg, profile);
for (int i = 0; i < msg.length; i++) {
System.out.println("===============================================");
System.out.println("主題:"+msg[i].getSubject());
InternetAddress[] toAddress = (InternetAddress[]) msg[i].getRecipients(Message.RecipientType.TO);
for(InternetAddress adress:toAddress){
System.out.println(adress.getAddress());
}
}
//關(guān)閉打開的資源
if (inbox != null)
inbox.close(true);
if (store != null)
store.close();
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
很簡單,可以正常收取郵件、正常顯示收件人列表;但是當連接一個內(nèi)部郵件服務器,并且收件人是以分號為分隔符的郵件時,確實無法正常顯示收件人。
查找了一下規(guī)范,發(fā)現(xiàn)FRC 822規(guī)定:收件人之間是要求以逗號為分割,看來分號分割不是標準的分隔符,如下:
復制代碼 代碼如下:
destination = "To" ":" 1#address ; Primary
/ "Resent-To" ":" 1#address
/ "cc" ":" 1#address ; Secondary
/ "Resent-cc" ":" 1#address
/ "bcc" ":" #address ; Blind carbon
/ "Resent-bcc" ":" #address
而#語法表示一個列表,列表之間的內(nèi)容以逗號進行分割,例如:
復制代碼 代碼如下:
2.7. #RULE: LISTS
A construct "#" is defined, similar to "*", as follows:
<l>#<m>element
indicating at least <l> and at most <m> elements, each separated by one or more commas (","). This makes the usual form of lists very easy; a rule such as '(element *("," element))' can be shown as "1#element". Wherever this construct is used, null elements are allowed, but do not contribute to the count of elements present. That is, "(element),,(element)" is permitted, but counts as only two elements. Therefore, where at least one ele- ment is required, at least one non-null element must be present. Default values are 0 and infinity so that "#(element)" allows any number, including zero; "1#element" requires at least one; and "1#2element" allows one or two.
JAVA MAIL嚴格按照RFC 822規(guī)范進行操作,沒有對分號做處理。大多數(shù)郵件服務器都是嚴格遵循RFC 822規(guī)范的,比如Lotus Notes、gmail(gmail的收件人是不能輸入分號的,甚至會自動替換成逗號,贊一個);但是,大家也會發(fā)現(xiàn)平時發(fā)送郵件,經(jīng)常以分號做分割,這是因為微軟的一些郵件工具,如:outlook、outlook Express或是其MAPI,則是以分號為分隔符的,又因為outlook使用用戶非常廣泛,甚至很多人會認為分號分隔才是規(guī)范,強烈鄙視Microsoft!不規(guī)范的東西太多了?。?br>
如果足夠倒霉,真的碰到了習慣使用分號作為分割符的用戶,而其郵件服務器又不會自動將分號替換為逗號,那我們只能通過程序做兼容了,可以考慮修訂JAVA MAIL源碼,修改
InternetAddress類的parse方法,這里源碼就不展現(xiàn)了,大家只需將對于分號的處理修改成和逗號一樣即可(但是分號在FRC 822中也有定義,這么修改有可能引起隱患,大家慎重)。
相關(guān)文章
springBoot使用openfeign來遠程調(diào)用的實現(xiàn)
這篇文章主要介紹了springBoot使用openfeign來遠程調(diào)用的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
詳解如何使用MyBatis實現(xiàn)數(shù)據(jù)庫的CRUD
這篇文章主要為大家詳細介紹了如何使用MyBatis實現(xiàn)數(shù)據(jù)庫的CRUD操作,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2024-12-12
詳解SpringSecurity如何實現(xiàn)前后端分離
這篇文章主要為大家介紹了詳解SpringSecurity如何實現(xiàn)前后端分離,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03
Spring Boot 使用WebAsyncTask異步返回結(jié)果
這篇文章主要介紹了Spring Boot 使用WebAsyncTask異步返回結(jié)果的相關(guān)資料,需要的朋友可以參考下2018-02-02
redis 使用lettuce 啟動內(nèi)存泄漏錯誤的解決方案
這篇文章主要介紹了redis 使用lettuce 啟動內(nèi)存泄漏錯誤的解決方案,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04

