淺析Java Mail無法解析帶分號(hào)的收件人列表的問題
今天同事碰到一個(gè)問題,使用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)閉打開的資源
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í)無法正常顯示收件人。
查找了一下規(guī)范,發(fā)現(xiàn)FRC 822規(guī)定:收件人之間是要求以逗號(hào)為分割,看來分號(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
而#語法表示一個(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)行操作,沒有對(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ī)范的東西太多了?。?br>
如果足夠倒霉,真的碰到了習(xí)慣使用分號(hào)作為分割符的用戶,而其郵件服務(wù)器又不會(huì)自動(dòng)將分號(hào)替換為逗號(hào),那我們只能通過程序做兼容了,可以考慮修訂JAVA MAIL源碼,修改
InternetAddress類的parse方法,這里源碼就不展現(xiàn)了,大家只需將對(duì)于分號(hào)的處理修改成和逗號(hào)一樣即可(但是分號(hào)在FRC 822中也有定義,這么修改有可能引起隱患,大家慎重)。
相關(guān)文章
SSM 實(shí)現(xiàn)登錄驗(yàn)證碼功能(附源碼)
這篇文章主要介紹了SSM 實(shí)現(xiàn)登錄驗(yàn)證碼功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-12-12Java后臺(tái)Controller實(shí)現(xiàn)文件下載操作
這篇文章主要介紹了Java后臺(tái)Controller實(shí)現(xiàn)文件下載操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-10-10關(guān)于bootstrap.yml和bootstrap.properties的優(yōu)先級(jí)問題
這篇文章主要介紹了關(guān)于bootstrap.yml和bootstrap.properties的優(yōu)先級(jí)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03詳解Java實(shí)現(xiàn)批量壓縮圖片裁剪壓縮多種尺寸縮略圖一鍵批量上傳圖片
這篇文章主要介紹了Java實(shí)現(xiàn)批量壓縮圖片裁剪壓縮多種尺寸縮略圖一鍵批量上傳圖片,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03JAVA實(shí)現(xiàn)FTP斷點(diǎn)上傳的方法
這篇文章主要介紹了JAVA實(shí)現(xiàn)FTP斷點(diǎn)上傳的方法,涉及java使用FTP實(shí)現(xiàn)文件傳輸?shù)南嚓P(guān)技巧,需要的朋友可以參考下2015-06-06