如何在 C# 控制台应用程序中验证电子邮件地址?

How to validate email address in c# console application?

我构建了一些简单的代码来像表格一样填写字段。我输入了电子邮件,我想验证,如果邮件地址正确,则允许代码执行另一项操作。

Console.Write("Enter Your Email : "); 字符串 acmail = Console.ReadLine();

验证"acmail"它是否是一个有效的电子邮件地址

using System;
using System.Collections.Generic;

class Demo {
   static void Main() {
      string val;

      bool validEmail = false;

      Console.Write("Enter Email: ");
      val = Console.ReadLine();

      int stringSplitCount = val.Split('@').Length;

      if(stringSplitCount > 1 && stringSplitCount <= 2) // should only have one @ symbol
        {
           stringSplitCount = val.Split('.').Length;

           if(stringSplitCount > 1)  // should have at least one . (dot)
             validEmail = true
        }

      val += (validEmail ? " is valid" ; "is invalid");

      Console.WriteLine("Your input: {0}", val);

   }
}