Today, a colleague encountered a problem. When using JAVA MAIL to receive emails, if the recipient is a list and the recipient list is separated by semicolons, an exception will occur in JAVA MAIL and cannot be parsed normally. I took a moment to take a look. , I wrote a simple demo, very simple, for example:
Copy the code code as follows:
@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");
//Connect to the mail server
store.connect(host, "chb_go", "3870359346");
//Receive inbox
Folder inbox = store.getDefaultFolder().getFolder("INBOX");
//Read only is enough
inbox.open(Folder.READ_ONLY);
//Get all mailing lists
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("Subject: "+msg[i].getSubject());
InternetAddress[] toAddress = (InternetAddress[]) msg[i].getRecipients(Message.RecipientType.TO);
for(InternetAddress adress:toAddress){
System.out.println(adress.getAddress());
}
}
//Close the open resource
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();
}
}
It's very simple. You can receive emails normally and display the recipient list normally; but when connecting to an internal mail server and the recipients are emails with semicolons as delimiters, the recipients cannot be displayed normally.
After searching the specifications, I found that FRC 822 stipulates that recipients are required to be separated by commas. It seems that semicolon separation is not a standard delimiter, as follows:
Copy the code code as follows:
destination = "To" ":" 1#address ; Primary
/ "Resent-To" ":" 1#address
/ "cc" ":" 1#address ; Secondary
/ "Resent-cc" ":" 1#address
/ "bcc" ":" #address ; Blind carbon
/ "Resent-bcc" ":" #address
The # syntax represents a list, and the contents between the lists are separated by commas, for example:
Copy the code code as follows:
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 element 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 operates strictly in accordance with RFC 822 specifications and does not deal with semicolons. Most mail servers strictly follow the RFC 822 specification, such as Lotus Notes and gmail (the recipient of gmail cannot enter a semicolon, and it will even be automatically replaced with a comma, like it); however, you will also find that when sending Emails are often separated by semicolons. This is because some of Microsoft’s email tools, such as Outlook, Outlook Express or its MAPI uses semicolons as separators, and because Outlook is widely used by users, many people even think that semicolon separation is the norm, and they strongly despise Microsoft! There are too many irregularities! !
If you are unlucky enough and encounter users who are accustomed to using semicolons as delimiters, and their mail servers do not automatically replace semicolons with commas, then we can only make compatibility through programs. You can consider revising the JAVA MAIL source code. Revise
The source code of the parse method of the InternetAddress class will not be shown here. You only need to modify the processing of semicolons to be the same as commas (but semicolons are also defined in FRC 822. Such modifications may cause hidden dangers, so please be careful) .