System.Net.Mail.MailAddressParser.ParseAddress C# (CSharp) Method

ParseAddress() private static method

private static ParseAddress ( string data, bool expectMultipleAddresses, int &index ) : System.Net.Mail.MailAddress
data string
expectMultipleAddresses bool
index int
return System.Net.Mail.MailAddress
        private static MailAddress ParseAddress(string data, bool expectMultipleAddresses, ref int index)
        {
            Debug.Assert(!string.IsNullOrEmpty(data));
            Debug.Assert(index >= 0 && index < data.Length, "Index out of range: " + index + ", " + data.Length);

            // Parsed components to be assembled as a MailAddress later
            string domain = null;
            string localPart = null;
            string displayName = null;

            // Skip comments and whitespace
            index = ReadCfwsAndThrowIfIncomplete(data, index);

            // Do we expect angle brackets around the address?
            // e.g. ("display name" <user@domain>)
            bool expectAngleBracket = false;
            if (data[index] == MailBnfHelper.EndAngleBracket)
            {
                expectAngleBracket = true;
                index--;
            }

            domain = ParseDomain(data, ref index);

            // The next character after the domain must be the '@' symbol
            if (data[index] != MailBnfHelper.At)
            {
                throw new FormatException(SR.MailAddressInvalidFormat);
            }

            // Skip the '@' symbol
            index--;

            localPart = ParseLocalPart(data, ref index, expectAngleBracket, expectMultipleAddresses);

            // Check for a matching angle bracket around the address
            if (expectAngleBracket)
            {
                if (index >= 0 && data[index] == MailBnfHelper.StartAngleBracket)
                {
                    index--; // Skip the angle bracket
                    // Skip white spaces, but leave comments, as they may be part of the display name.
                    index = WhitespaceReader.ReadFwsReverse(data, index);
                }
                else
                {
                    // Mismatched angle brackets, throw
                    throw new FormatException(SR.Format(SR.MailHeaderFieldInvalidCharacter,
                        (index >= 0 ? data[index] : MailBnfHelper.EndAngleBracket)));
                }
            }

            // Is there anything left to parse?
            // There could still be a display name or another address
            if (index >= 0 && !(expectMultipleAddresses && data[index] == MailBnfHelper.Comma))
            {
                displayName = ParseDisplayName(data, ref index, expectMultipleAddresses);
            }
            else
            {
                displayName = string.Empty;
            }

            return new MailAddress(displayName, localPart, domain);
        }

Same methods

MailAddressParser::ParseAddress ( string data ) : System.Net.Mail.MailAddress

Usage Example

        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;
            }
        }
All Usage Examples Of System.Net.Mail.MailAddressParser::ParseAddress