예제 #1
0
        /// <summary>
        /// Creates a new <see cref="EmailAddress"/> using the specified email address.
        /// </summary>
        /// <param name="address"></param>
        /// <returns>a new <see cref="EmailAddress"/> address or null</returns>
        public static EmailAddress TryCreate(string address)
        {
            var valid = Validate.EmailAddress(address);

            if (valid == null)
            {
                return(null);
            }

            return(new EmailAddress(valid, true));
        }
예제 #2
0
        private EmailAddress(string address, bool skipValidation)
        {
            this.original = address;

            if (!skipValidation)
            {
                address = Validate.EmailAddress(address);

                if (address == null)
                {
                    throw new ArgumentException("Invalid email address", "address");
                }
            }

            this.address = address;

            this.localPart   = GetLocalPart(address);
            this.domainPart  = GetDomainPart(address);
            this.accountPart = GetAccountPart(this.localPart);
            this.tagPart     = GetTagPart(this.localPart);
        }
예제 #3
0
        /// <summary>
        /// Creates a new <see cref="EmailAddress"/> using the specified email address parts.
        /// </summary>
        /// <param name="accountPart">The account part (required).</param>
        /// <param name="tagPart">The tag part.</param>
        /// <param name="domainPart">The domain part (required).</param>
        /// <exception cref="System.ArgumentException">
        /// The value cannot be empty;localPart
        /// or
        /// The value cannot be empty;domainPart
        /// or
        /// Invalid email address;address
        /// </exception>
        public EmailAddress(string accountPart, string tagPart, string domainPart)
        {
            if (string.IsNullOrEmpty(accountPart))
            {
                throw new ArgumentException("The value cannot be empty", "localPart");
            }
            if (string.IsNullOrEmpty(domainPart))
            {
                throw new ArgumentException("The value cannot be empty", "domainPart");
            }

            this.domainPart  = domainPart;
            this.accountPart = accountPart;

            string address;

            if (string.IsNullOrWhiteSpace(tagPart))
            {
                this.tagPart   = null;
                this.localPart = accountPart;
                address        = accountPart + "@" + domainPart;
            }
            else
            {
                this.tagPart   = tagPart.Trim();
                this.localPart = accountPart + "+" + this.tagPart;
                address        = accountPart + "+" + tagPart + "@" + domainPart;
            }

            address = Validate.EmailAddress(address);
            if (address == null)
            {
                throw new ArgumentException("Invalid email address", "address");
            }

            this.address = address;
        }