欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

浅析Java Mail无法解析带分号的收件人列表的问题

程序员文章站 2023-12-20 09:21:04
今天同事碰到一个问题,使用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中也有定义,这么修改有可能引起隐患,大家慎重)。

上一篇:

下一篇: