Java mail cannot parse the recipients list with semicolons

Source: Internet
Author: User

Java mail cannot parse the recipients list with semicolons

Today, my colleague encountered a problem. When receiving an email using Java mail, if the recipient list is a list separated by a semicolon, Java mail will encounter an exception and cannot be parsed properly, after taking a look, I wrote a simple demo, which is very simple, for example:

@ Test <br/> Public void testreceivemail () {</P> <p> try {<br/> string host = "pop3.163.com "; <br/> properties pops = new properties (); <br/> pops. put ("mail. pop3.host ", host); <br/> pops. put ("mail. pop. auth "," true "); <br/> session = session. getdefaultinstance (POPs, null); <br/> store = session. getstore ("POP3"); <br/> // connect to the email server <br/> store. connect (host, "chb_go", "3870359346"); <br/> // Collect inbox <br/> folder inbox = store. getdefaultfolder (). getfolder ("inbox"); <br/> // read-only enough <br/> inbox. open (folder. read_only); <br/> // obtain the list of all emails <br/> message [] MSG = inbox. getmessages (); <br/> fetchprofile profile = new fetchprofile (); <br/> profile. add (fetchprofile. item. envelope); <br/> inbox. fetch (MSG, profile); <br/> for (INT I = 0; I <MSG. length; I ++) {<br/> system. out. println ("============================ ======================== "); <Br/> system. out. println ("topic:" + MSG [I]. getsubject (); <br/> internetaddress [] toaddress = (internetaddress []) MSG [I]. getrecipients (message. recipienttype. to); <br/> for (internetaddress adress: toaddress) {<br/> system. out. println (adress. getaddress (); <br/>}< br/> // close open resources <br/> If (Inbox! = NULL) <br/> inbox. Close (true); <br/> If (store! = NULL) <br/> store. close (); <br/>}catch (nosuchproviderexception e) {<br/> E. printstacktrace (); <br/>} catch (messagingexception e) {<br/> E. printstacktrace (); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}< br/>}You can receive emails and display the recipients list normally. However, when an internal email server is connected and the recipients are separated by semicolons, the recipients cannot be displayed normally. After looking at the specifications, we found that FRC 822 requires that recipients be separated by commas. It seems that the semicolon splitting is not a standard separator, as shown below: Destination = "to" ":" 1 # address; Primary
/"Resent-to" ":" 1 # address /"Cc": "1 # address; secondary /"Resent-cc" ":" 1 # address /"BCC": "# address; Blind carbon /"Resent-BCC" ":" # address
# Syntax indicates a list. The content of the list is separated by commas (,). For example: 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 contrients 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 #2 element" allows one or two. Java mail operates in strict accordance with RFC 822 specifications and does not process semicolons. Most email servers strictly follow RFC 822 specifications, such as Lotus Notes and Gmail (Gmail recipients cannot enter semicolons, or even automatically replace them with commas (,). However, you will also find that emails are sent at ordinary times and are often separated by semicolons. This is because Microsoft's email tools, such as outlook, Outlook Express, or its mapi, use semicolons as the separator, because outlook is widely used by many users, even many people think that semicolon separation is the standard, Microsoft! There are too many nonstandard things !! If we are unlucky enough to meet a user who is used to using semicolons as separators, and its mail server does not automatically replace semicolons with commas, we can only Program If it is compatible, you can consider revising the Java mail source code and modifying it. Internetaddress The source code is not displayed here for the parse method of the class. You only need to modify the processing of the semicolon to be the same as that of the comma (but the semicolon is also defined in FRC 822). Such modification may cause potential risks, exercise caution ).

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.