程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 淺析Java Mail沒法解析帶分號的收件人列表的成績

淺析Java Mail沒法解析帶分號的收件人列表的成績

編輯:關於JAVA

淺析Java Mail沒法解析帶分號的收件人列表的成績。本站提示廣大學習愛好者:(淺析Java Mail沒法解析帶分號的收件人列表的成績)文章只能為提供參考,不一定能成為您想要的結果。以下是淺析Java Mail沒法解析帶分號的收件人列表的成績正文


明天同事碰著一個成績,應用JAVA MAIL收取郵件時,假如收件人是個列表,且收件人列表是以分號停止朋分的,則JAVA MAIL就會湧現異常,不克不及正常解析,抽閒看了一眼,本身寫了個簡略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());
                                }
                        }
                        //封閉翻開的資本
                        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();
                }
        }

很簡略,可以正常收取郵件、正常顯示收件人列表;然則當銜接一個外部郵件辦事器,而且收件人是以分號為分隔符的郵件時,確切沒法正常顯示收件人。

查找了一下標准,發明FRC 822劃定:收件人之間是請求以逗號為朋分,看來分號朋分不是尺度的分隔符,以下:

destination = "To" ":" 1#address ; Primary
/ "Resent-To" ":" 1#address
 / "cc" ":" 1#address ; Secondary
/ "Resent-cc" ":" 1#address
/ "bcc" ":" #address ; Blind carbon
/ "Resent-bcc" ":" #address

而#語法表現一個列表,列表之間的內容以逗號停止朋分,例如:

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標准停止操作,沒有對分號做處置。年夜多半郵件辦事器都是嚴厲遵守RFC 822標准的,好比Lotus Notes、gmail(gmail的收件人是不克不及輸出分號的,乃至會主動調換成逗號,贊一個);然則,年夜家也會發明日常平凡發送郵件,常常以分號做朋分,這是由於微軟的一些郵件對象,如:outlook、outlook Express或是其MAPI,則是以分號為分隔符的,又由於outlook應用用戶異常普遍,乃至許多人會以為分號分隔才是標准,激烈小看Microsoft!不標准的器械太多了!!

假如足夠不利,真的碰著了習氣應用分號作為朋分符的用戶,而其郵件辦事器又不會主動將分號調換為逗號,那我們只能經由過程法式做兼容了,可以斟酌修訂JAVA MAIL源碼,修正
InternetAddress類的parse辦法,這裡源碼就不展示了,年夜家只需將關於分號的處置修正成和逗號一樣便可(然則分號在FRC 822中也有界說,這麼修正有能夠惹起隱患,年夜家鄭重)。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved