智能分析用戶輸入的郵件地址列表

 

在處理多個郵件地址串時, 分析並驗證郵件地址的有效性, 並格式化輸出所有可用的郵件地址列表:

        /// <summary>
        /// 智能分析用戶輸入的郵件地址列表
        /// </summary>
        /// <param name="emailList"></param>
        /// <returns></returns>
        public static Dictionary<string, string> SplitMailAddress(string emailList)
        {
            Dictionary<string, string> emails = new Dictionary<string, string>();

            if (String.IsNullOrEmpty(emailList))
            {
                return emails;
            }
           
            emailList = emailList.Trim(' ', '<', '>', ',', ';');

            while (emailList.Length > 0)
            {
                string s0 = String.Empty, s1 = String.Empty, s2 = String.Empty;
                int x = 0, i = 0;
                bool quit = true;

                x = emailList.IndexOf("@");
                while (x > 0)
                {
                    string _temp0 = emailList.Substring(0, x);
                    string _temp1 = emailList.Substring(x);
                    i = _temp0.Length;

                    System.Text.RegularExpressions.Match match0 = Regex.Match(_temp0, @"([</s])", RegexOptions.RightToLeft);
                    System.Text.RegularExpressions.Match match1 = Regex.Match(_temp1, @"([>,;])");

                    if (match0.Success)
                    {
                        s0 = _temp0.Substring(0, match0.Index).Trim(' ', '<', '>', ',', ';');
                        s1 = _temp0.Substring(match0.Index + 1).Trim(' ', '<', '>', ',', ';');
                    }
                    else
                    {
                        s1 = _temp0.Trim(' ', '<', '>', ',', ';');
                        s2 = _temp1.Trim(' ', '<', '>', ',', ';');
                    }

                    if (match1.Success)
                    {
                        i += match1.Index;
                        s2 = _temp1.Substring(0, match1.Index).Trim(' ', '<', '>', ',', ';');
                    }
                    else
                    {
                        i += _temp1.Length;
                        s2 = _temp1.Trim(' ', '<', '>', ',', ';');
                    }

                    if (!Regex.IsMatch(s2, @"^@[a-z-0-9]+(/.[a-z-0-9]+)+$", RegexOptions.IgnoreCase))
                    {
                        x = emailList.IndexOf("@", x + 1);
                        continue;
                    }

                    string _key = (s1 + s2).ToLower();
                    if (!emails.ContainsKey(_key))
                    {
                        emails.Add(_key, s0);
                    }

                    emailList = emailList.Substring(i).Trim(' ', '<', '>', ',', ';');

                    quit = false;
                    break;
                }

                if (quit)
                {
                    break;
                }
            }

            return emails;
        }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章