在C#中检查有效的电子邮件地址 [英] Check Valid email address in c#

查看:134
本文介绍了在C#中检查有效的电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用c#Windows应用程序中的smtp服务发送电子邮件。我必须以最佳方式执行以降低电子邮件跳出率。我必须检查所提供的电子邮件地址是否有效。我正在使用代码。

I'm sending email using smtp services in my c# windows application. I have to perform in a best way to reduce the email bounce rate. I have to check the provided email address is valid or not. I'm using the code.

    private void btnCheckValid_Click(object sender, EventArgs e)
        {
            if (isRealDomain(textBox1.Text.Trim()) == true)
                MessageBox.Show("Valid Email Address!");
        }
        private bool isRealDomain(string inputEmail)
        {
            bool isReal = false;
            try
            {
                string[] host = (inputEmail.Split('@'));
                string hostname = host[1];

                IPHostEntry IPhst = Dns.GetHostEntry(hostname);
                IPEndPoint endPt = new IPEndPoint(IPhst.AddressList[0], 25);
                Socket s = new Socket(endPt.AddressFamily,
                        SocketType.Stream, ProtocolType.Tcp);
                s.Connect(endPt);
                s.Close();
                isReal = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                isReal = false;
            }

            return isReal;
        }

通过检查真实域,我可以识别托管IP,但是电子邮件地址是

By checking the real domain I can identified the Hosted IP but the email address is created on the host or not.

所以我只能验证格式。

我的问题是如何在c#中仅查找任何域的有效地址。

So my question is how to find only valid address of any domain in c#.

推荐答案

您可以导入 System.ComponentModel.DataAnnotations 并将其用于这样:

You can import System.ComponentModel.DataAnnotations and use it in this way:

private bool validMail(string address)
{
    EmailAddressAttribute e = new EmailAddressAttribute();
    if (e.IsValid(address))
        return true;
    else
        return false;
}

这篇关于在C#中检查有效的电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆