如何识别用户发送maild是否正确?

时间:2011-04-12 14:00:03

标签: c# .net email smtp response

我从.net应用程序发送邮件,我的邮件代码工作正常,但我不想允许不正确的邮件ID。那么如何识别发送邮件的用户是否具有正确的ID。

我想知道smtpclient发送的邮件的响应。

    Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
    MailSettingsSectionGroup settings = (MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");
     smtpHost = settings.Smtp.Network.Host;
     smtpport = settings.Smtp.Network.Port.ToString();
     smtpuser = settings.Smtp.Network.UserName;
     smtppwd = settings.Smtp.Network.Password;

    string MessageBody = string.Empty;
    try
    {

        message = new MailMessage();
        message.From = new MailAddress(smtpuser);

        message.To.Add(toMailAddress);

        message.BodyEncoding = System.Text.Encoding.UTF8;

        message.Subject = mailSubject;
        message.Body = mailMessage.ToString();



        message.IsBodyHtml = true;

        client = new SmtpClient();
        client.Host = smtpHost;
        //if (isSSLEnabled)
        //{
        //    client.EnableSsl = true;
        //}

        client.Port =  Convert.ToInt32(smtpport);
        client.EnableSsl = false;
        client.Credentials = new System.Net.NetworkCredential(smtpuser, smtppwd);

        client.Send(message);

    }
    catch (Exception ex)
    {
        string x = ex.Message;
    } 

1 个答案:

答案 0 :(得分:1)

我会使用正则表达式来验证电子邮件。

http://www.codeproject.com/KB/recipes/EmailRegexValidator.aspx

至于捕获smtp响应,此链接应该会帮助你。只需查看响应部分,您就会知道介绍项目。

http://www.csharphelp.com/2007/01/howto-smtp-in-c/