📄 mailaddress.java
字号:
/**************************************************************** * Licensed to the Apache Software Foundation (ASF) under one * * or more contributor license agreements. See the NOTICE file * * distributed with this work for additional information * * regarding copyright ownership. The ASF licenses this file * * to you under the Apache License, Version 2.0 (the * * "License"); you may not use this file except in compliance * * with the License. You may obtain a copy of the License at * * * * http://www.apache.org/licenses/LICENSE-2.0 * * * * Unless required by applicable law or agreed to in writing, * * software distributed under the License is distributed on an * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * * KIND, either express or implied. See the License for the * * specific language governing permissions and limitations * * under the License. * ****************************************************************/package org.apache.mailet;import java.util.Locale;import javax.mail.internet.InternetAddress;import javax.mail.internet.ParseException;/** * A representation of an email address. * <p>This class encapsulates functionalities to access to different * parts of an email address without dealing with its parsing.</p> * * <p>A MailAddress is an address specified in the MAIL FROM and * RCPT TO commands in SMTP sessions. These are either passed by * an external server to the mailet-compliant SMTP server, or they * are created programmatically by the mailet-compliant server to * send to another (external) SMTP server. Mailets and matchers * use the MailAddress for the purpose of evaluating the sender * and recipient(s) of a message.</p> * * <p>MailAddress parses an email address as defined in RFC 821 * (SMTP) p. 30 and 31 where addresses are defined in BNF convention. * As the mailet API does not support the aged "SMTP-relayed mail" * addressing protocol, this leaves all addresses to be a <mailbox>, * as per the spec. The MailAddress's "user" is the <local-part> of * the <mailbox> and "host" is the <domain> of the mailbox.</p> * * <p>This class is a good way to validate email addresses as there are * some valid addresses which would fail with a simpler approach * to parsing address. It also removes parsing burden from * mailets and matchers that might not realize the flexibility of an * SMTP address. For instance, "serge@home"@lokitech.com is a valid * SMTP address (the quoted text serge@home is the user and * lokitech.com is the host). This means all current parsing to date * is incorrect as we just find the first @ and use that to separate * user from host.</p> * * <p>This parses an address as per the BNF specification for <mailbox> * from RFC 821 on page 30 and 31, section 4.1.2. COMMAND SYNTAX. * http://www.freesoft.org/CIE/RFC/821/15.htm</p> * * @version 1.0 */public class MailAddress implements java.io.Serializable { //We hardcode the serialVersionUID so that from James 1.2 on, // MailAddress will be deserializable (so your mail doesn't get lost) public static final long serialVersionUID = 2779163542539434916L; private final static char[] SPECIAL = {'<', '>', '(', ')', '[', ']', '\\', '.', ',', ';', ':', '@', '\"'}; private String user = null; private String host = null; //Used for parsing private int pos = 0; /** * strip source routing, according to RFC-2821 it is an allowed approach to handle mails * contaning RFC-821 source-route information */ private void stripSourceRoute(String address) throws ParseException { if (pos < address.length()) { if(address.charAt(pos)=='@') { int i = address.indexOf(':'); if(i != -1) { pos = i+1; } } } } /** * <p>Construct a MailAddress parsing the provided <code>String</code> object.</p> * * <p>The <code>personal</code> variable is left empty.</p> * * @param address the email address compliant to the RFC822 format * @throws ParseException if the parse failed */ public MailAddress(String address) throws ParseException { address = address.trim(); // Test if mail address has source routing information (RFC-821) and get rid of it!! //must be called first!! (or at least prior to updating pos) stripSourceRoute(address); StringBuffer userSB = new StringBuffer(); StringBuffer hostSB = new StringBuffer(); //Begin parsing //<mailbox> ::= <local-part> "@" <domain> try { //parse local-part //<local-part> ::= <dot-string> | <quoted-string> if (address.charAt(pos) == '\"') { userSB.append(parseQuotedLocalPart(address)); if (userSB.toString().length() == 2) { throw new ParseException("No quoted local-part (user account) found at position " + (pos + 2)); } } else { userSB.append(parseUnquotedLocalPart(address)); if (userSB.toString().length() == 0) { throw new ParseException("No local-part (user account) found at position " + (pos + 1)); } } //find @ if (pos >= address.length() || address.charAt(pos) != '@') { throw new ParseException("Did not find @ between local-part and domain at position " + (pos + 1)); } pos++; //parse domain //<domain> ::= <element> | <element> "." <domain> //<element> ::= <name> | "#" <number> | "[" <dotnum> "]" while (true) { if (address.charAt(pos) == '#') { hostSB.append(parseNumber(address)); } else if (address.charAt(pos) == '[') { hostSB.append(parseDotNum(address)); } else { hostSB.append(parseDomainName(address)); } if (pos >= address.length()) { break; } if (address.charAt(pos) == '.') { hostSB.append('.'); pos++; continue; } break; } if (hostSB.toString().length() == 0) { throw new ParseException("No domain found at position " + (pos + 1)); } } catch (IndexOutOfBoundsException ioobe) { throw new ParseException("Out of data at position " + (pos + 1)); } user = userSB.toString(); host = hostSB.toString(); } /** * Construct a MailAddress with the provided personal name and email * address. * * @param user the username or account name on the mail server * @param host the server that should accept messages for this user * @throws ParseException if the parse failed */ public MailAddress(String newUser, String newHost) throws ParseException { this(newUser+ "@" + newHost); } /** * Constructs a MailAddress from a JavaMail InternetAddress, using only the * email address portion, discarding the personal name. */ public MailAddress(InternetAddress address) throws ParseException { this(address.getAddress()); } /** * Return the host part. * * @return a <code>String</code> object representing the host part * of this email address. If the host is of the dotNum form * (e.g. [yyy.yyy.yyy.yyy]) then strip the braces first. */ public String getHost() { if (!(host.startsWith("[") && host.endsWith("]"))) { return host; } else { return host.substring(1, host.length() -1); } } /** * Return the user part. * * @return a <code>String</code> object representing the user part * of this email address. * @throws AddressException if the parse failed */ public String getUser() { return user; } public String toString() { StringBuffer addressBuffer = new StringBuffer(128) .append(user) .append("@") .append(host); return addressBuffer.toString(); } /** * Return MailAddress as InternetAddress * * @return the address */ public InternetAddress toInternetAddress() { try { return new InternetAddress(toString()); } catch (javax.mail.internet.AddressException ae) { //impossible really return null; } } public boolean equals(Object obj) { if (obj == null) { return false; } else if (obj instanceof String) { String theString = (String)obj; return toString().equalsIgnoreCase(theString);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -