ParseAddress() static private method

static private ParseAddress ( string data ) : System.Net.Mail.MailAddress
data string
return System.Net.Mail.MailAddress
        public MailAddress(string address, string displayName, Encoding displayNameEncoding)
        {
            if (address == null)
            {
                throw new ArgumentNullException("address");
            }
            if (address == string.Empty)
            {
                throw new ArgumentException(SR.GetString("net_emptystringcall", new object[] { "address" }), "address");
            }
            this.displayNameEncoding = displayNameEncoding ?? Encoding.GetEncoding("utf-8");
            this.displayName         = displayName ?? string.Empty;
            if ((!string.IsNullOrEmpty(this.displayName) && (this.displayName.Length >= 2)) && ((this.displayName[0] == '"') && (this.displayName[this.displayName.Length - 1] == '"')))
            {
                this.displayName = this.displayName.Substring(1, this.displayName.Length - 2);
            }
            MailAddress address2 = MailAddressParser.ParseAddress(address);

            this.host     = address2.host;
            this.userName = address2.userName;
            if (string.IsNullOrEmpty(this.displayName))
            {
                this.displayName = address2.displayName;
            }
        }
Beispiel #2
0
        // Parse a single MailAddress from the given string.
        //
        // Throws a FormatException if any part of the MailAddress is invalid.
        internal static MailAddress ParseAddress(string data)
        {
            int         index         = data.Length - 1;
            MailAddress parsedAddress = MailAddressParser.ParseAddress(data, false, ref index);

            Debug.Assert(index == -1, "The index indicates that part of the address was not parsed: " + index);
            return(parsedAddress);
        }
Beispiel #3
0
        // Parse a comma separated list of MailAddress's
        //
        // Throws a FormatException if any MailAddress is invalid.
        internal static IList <MailAddress> ParseMultipleAddresses(string data)
        {
            IList <MailAddress> results = new List <MailAddress>();
            int index = data.Length - 1;

            while (index >= 0)
            {
                // Because we're parsing in reverse, we must make an effort to preserve the order of the addresses.
                results.Insert(0, MailAddressParser.ParseAddress(data, true, ref index));
                Debug.Assert(index == -1 || data[index] == MailBnfHelper.Comma,
                             "separator not found while parsing multiple addresses");
                index--;
            }
            return(results);
        }
Beispiel #4
0
        //
        // This constructor validates and stores the components of an e-mail address.
        //
        // Preconditions:
        // - 'address' must not be null or empty.
        //
        // Postconditions:
        // - The e-mail address components from the given 'address' are parsed, which should be formatted as:
        // "EncodedDisplayname" <username@host>
        // - If a 'displayName' is provided separately, it overrides whatever display name is parsed from the 'address'
        // field.  The display name does not need to be pre-encoded if a 'displayNameEncoding' is provided.
        //
        // A FormatException will be thrown if any of the components in 'address' are invalid.
        public MailAddress(string address, string displayName, Encoding displayNameEncoding)
        {
            if (address == null)
            {
                throw new ArgumentNullException("address");
            }
            if (address == String.Empty)
            {
                throw new ArgumentException(SR.GetString(SR.net_emptystringcall, "address"), "address");
            }

            this.displayNameEncoding = displayNameEncoding ?? Encoding.GetEncoding(MimeBasePart.defaultCharSet);
            this.displayName         = displayName ?? string.Empty;

            // Check for bounding quotes
            if (!String.IsNullOrEmpty(this.displayName))
            {
                this.displayName = MailAddressParser.NormalizeOrThrow(this.displayName);

                if (this.displayName.Length >= 2 && this.displayName[0] == '\"' &&
                    this.displayName[this.displayName.Length - 1] == '\"')
                {
                    // Peal bounding quotes, they'll get re-added later.
                    this.displayName = this.displayName.Substring(1, this.displayName.Length - 2);
                }
            }

            MailAddress result = MailAddressParser.ParseAddress(address);

            this.host     = result.host;
            this.userName = result.userName;

            // If we were not given a display name, use the one parsed from 'address'.
            if (String.IsNullOrEmpty(this.displayName))
            {
                this.displayName = result.displayName;
            }
        }
Beispiel #5
0
        //
        // This constructor validates and stores the components of an e-mail address.
        //
        // Preconditions:
        // - 'address' must not be null or empty.
        //
        // Postconditions:
        // - The e-mail address components from the given 'address' are parsed, which should be formatted as:
        // "EncodedDisplayname" <username@host>
        // - If a 'displayName' is provided separately, it overrides whatever display name is parsed from the 'address'
        // field.  The display name does not need to be pre-encoded if a 'displayNameEncoding' is provided.
        //
        // A FormatException will be thrown if any of the components in 'address' are invalid.
        public MailAddress(string address, string displayName, Encoding displayNameEncoding)
        {
            if (address == null)
            {
                throw new ArgumentNullException(nameof(address));
            }
            if (address == string.Empty)
            {
                throw new ArgumentException(SR.Format(SR.net_emptystringcall, nameof(address)), nameof(address));
            }

            _displayNameEncoding = displayNameEncoding ?? Encoding.GetEncoding(MimeBasePart.DefaultCharSet);
            _displayName         = displayName ?? string.Empty;

            // Check for bounding quotes
            if (!string.IsNullOrEmpty(_displayName))
            {
                _displayName = MailAddressParser.NormalizeOrThrow(_displayName);

                if (_displayName.Length >= 2 && _displayName[0] == '\"' &&
                    _displayName[_displayName.Length - 1] == '\"')
                {
                    // Peal bounding quotes, they'll get re-added later.
                    _displayName = _displayName.Substring(1, _displayName.Length - 2);
                }
            }

            MailAddress result = MailAddressParser.ParseAddress(address);

            _host     = result._host;
            _userName = result._userName;

            // If we were not given a display name, use the one parsed from 'address'.
            if (string.IsNullOrEmpty(_displayName))
            {
                _displayName = result._displayName;
            }
        }
Beispiel #6
0
 public MailAddress(string address)
 {
     MailAddressParser.ParseAddress(address);
 }