淺析Java Mail無(wú)法解析帶分號(hào)的收件人列表的問(wèn)題
今天同事碰到一個(gè)問(wèn)題,使用JAVA MAIL收取郵件時(shí),如果收件人是個(gè)列表,且收件人列表是以分號(hào)進(jìn)行分割的,則JAVA MAIL就會(huì)出現(xiàn)異常,不能正常解析,抽空看了一眼,自己寫了個(gè)簡(jiǎn)單demo,很簡(jiǎn)單,例如:
@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");
//連接郵件服務(wù)器
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)閉打開(kāi)的資源
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();
}
}
很簡(jiǎn)單,可以正常收取郵件、正常顯示收件人列表;但是當(dāng)連接一個(gè)內(nèi)部郵件服務(wù)器,并且收件人是以分號(hào)為分隔符的郵件時(shí),確實(shí)無(wú)法正常顯示收件人。
查找了一下規(guī)范,發(fā)現(xiàn)FRC 822規(guī)定:收件人之間是要求以逗號(hào)為分割,看來(lái)分號(hào)分割不是標(biāo)準(zhǔn)的分隔符,如下:
destination = "To" ":" 1#address ; Primary
/ "Resent-To" ":" 1#address
/ "cc" ":" 1#address ; Secondary
/ "Resent-cc" ":" 1#address
/ "bcc" ":" #address ; Blind carbon
/ "Resent-bcc" ":" #address
而#語(yǔ)法表示一個(gè)列表,列表之間的內(nèi)容以逗號(hào)進(jìn)行分割,例如:
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嚴(yán)格按照RFC 822規(guī)范進(jìn)行操作,沒(méi)有對(duì)分號(hào)做處理。大多數(shù)郵件服務(wù)器都是嚴(yán)格遵循RFC 822規(guī)范的,比如Lotus Notes、gmail(gmail的收件人是不能輸入分號(hào)的,甚至?xí)詣?dòng)替換成逗號(hào),贊一個(gè));但是,大家也會(huì)發(fā)現(xiàn)平時(shí)發(fā)送郵件,經(jīng)常以分號(hào)做分割,這是因?yàn)槲④浀囊恍┼]件工具,如:outlook、outlook Express或是其MAPI,則是以分號(hào)為分隔符的,又因?yàn)閛utlook使用用戶非常廣泛,甚至很多人會(huì)認(rèn)為分號(hào)分隔才是規(guī)范,強(qiáng)烈鄙視Microsoft!不規(guī)范的東西太多了??!
如果足夠倒霉,真的碰到了習(xí)慣使用分號(hào)作為分割符的用戶,而其郵件服務(wù)器又不會(huì)自動(dòng)將分號(hào)替換為逗號(hào),那我們只能通過(guò)程序做兼容了,可以考慮修訂JAVA MAIL源碼,修改
InternetAddress類的parse方法,這里源碼就不展現(xiàn)了,大家只需將對(duì)于分號(hào)的處理修改成和逗號(hào)一樣即可(但是分號(hào)在FRC 822中也有定義,這么修改有可能引起隱患,大家慎重)。
相關(guān)文章
Java中excel表數(shù)據(jù)的批量導(dǎo)入方法
這篇文章主要為大家詳細(xì)介紹了Java中excel表數(shù)據(jù)的批量導(dǎo)入方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
SpringBoot 應(yīng)用程序測(cè)試實(shí)現(xiàn)方案
這篇文章主要介紹了SpringBoot 應(yīng)用程序測(cè)試實(shí)現(xiàn)方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
Java JDK11基于嵌套的訪問(wèn)控制的實(shí)現(xiàn)
這篇文章主要介紹了Java JDK11基于嵌套的訪問(wèn)控制的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
springBoot使用openfeign來(lái)遠(yuǎn)程調(diào)用的實(shí)現(xiàn)
這篇文章主要介紹了springBoot使用openfeign來(lái)遠(yuǎn)程調(diào)用的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
詳解如何使用MyBatis實(shí)現(xiàn)數(shù)據(jù)庫(kù)的CRUD
這篇文章主要為大家詳細(xì)介紹了如何使用MyBatis實(shí)現(xiàn)數(shù)據(jù)庫(kù)的CRUD操作,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-12-12
詳解SpringSecurity如何實(shí)現(xiàn)前后端分離
這篇文章主要為大家介紹了詳解SpringSecurity如何實(shí)現(xiàn)前后端分離,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Spring Boot 使用WebAsyncTask異步返回結(jié)果
這篇文章主要介紹了Spring Boot 使用WebAsyncTask異步返回結(jié)果的相關(guān)資料,需要的朋友可以參考下2018-02-02
redis 使用lettuce 啟動(dòng)內(nèi)存泄漏錯(cuò)誤的解決方案
這篇文章主要介紹了redis 使用lettuce 啟動(dòng)內(nèi)存泄漏錯(cuò)誤的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04

