如何测试Hotmail帐户是否存在?

时间:2013-03-20 22:03:34

标签: c# asp.net network-programming websocket hotmail

我正在尝试测试我的Hotmail帐户是否存在,但以下代码对我没有帮助。

public bool IsExists_SMTPMethod(string email)
{
    string domain = email.Substring(email.IndexOf("@") + 1);
    var servers = _commander.GetMXServers(domain);

    Socket socket = null;

    foreach (MXServer mxserver in servers)
    {
        IPHostEntry ipHost = Dns.Resolve(mxserver.MailExchanger);
        IPEndPoint endPoint = new IPEndPoint(ipHost.AddressList[0], 25);
        socket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
        socket.Connect(endPoint);

        if (!CheckResponse(socket, ResponseEnum.ConnectSuccess))
        {
            socket.Close();
        }
        else
        {
            // If connected, send SMTP commands
            {
                SendData(socket, string.Format("HELO {0}\r\n", "machinename"));
                if (!CheckResponse(socket, ResponseEnum.GenericSuccess))
                {
                    socket.Close();
                    continue;
                }

                SendData(socket, string.Format("MAIL FROM:  <{0}>\r\n", "from@domain.com"));
                CheckResponse(socket, ResponseEnum.GenericSuccess);

                SendData(socket, string.Format("RCPT TO:  <{0}>\r\n", email));
                bool result = CheckResponse(socket, ResponseEnum.GenericSuccess);
                if (!result)
                {
                    socket.Close();
                    continue;
                }
                else
                    return true;
            }
        }
    }

    return false;
}

1 个答案:

答案 0 :(得分:0)

这方面有很多问题,其中很多都有相同的答案。如果您要发送的SMTP服务器允许,您可以使用RCPT TO,VRFY或EXPN等SMTP命令确认电子邮件地址是否存在。

问题是,看起来hotmail不允许这样做。

以下代码适用于gmail(并验证用户是否在那里),但您无法使用它来验证Hotmail地址 - 他们的SMTP服务器对所有内容都返回无效。

var email = "invalidemailasifnasiofnasonfaso@gmail.com";
TcpClient tClient = new TcpClient("gmail-smtp-in.l.google.com", 25);
string CRLF = "\r\n";
byte[] dataBuffer;
string ResponseString;
NetworkStream netStream = tClient.GetStream();
StreamReader reader = new StreamReader(netStream);
ResponseString = reader.ReadLine();
/* Perform HELO to SMTP Server and get Response */
dataBuffer = BytesFromString("HELO KirtanHere" + CRLF);
netStream.Write(dataBuffer, 0, dataBuffer.Length);
ResponseString = reader.ReadLine();
dataBuffer = BytesFromString("MAIL FROM:<test@gmail.com>" + CRLF);
netStream.Write(dataBuffer, 0, dataBuffer.Length);
ResponseString = reader.ReadLine();
/* Read Response of the RCPT TO Message to know from google if it exist or not */
dataBuffer = BytesFromString("RCPT TO:<"+email+">"+CRLF);
netStream.Write(dataBuffer, 0, dataBuffer.Length);
ResponseString = reader.ReadLine();
if (GetResponseCode(ResponseString) == 550)
{
    Console.WriteLine("Mail Address Does not Exist");
    Console.WriteLine("Original Error from Smtp Server "+ResponseString);
}
else
{
    Console.WriteLine("Original response from Smtp Server "+ResponseString);
}
/* QUITE CONNECTION */
dataBuffer = BytesFromString("QUITE" + CRLF);
netStream.Write(dataBuffer, 0, dataBuffer.Length);
tClient.Close();

这样的内容可以让您登录hotmail的SMTP发送电子邮件,但只要您的凭据正确,它就不会返回电子邮件是否正确。

try
{
    SmtpClient SmtpServer = new SmtpClient("smtp.live.com"); 
    SmtpServer.EnableSsl = true;
    SmtpServer.UseDefaultCredentials = false;
    SmtpServer.Port = 587;
    SmtpServer.Credentials = new System.Net.NetworkCredential("test@hotmail.com", "password");
    var message = new MailMessage {
    From = new MailAddress("asd@hotmail.com"),
    Subject = "test"

    };

    message.To.Add("sodfinaosidgnoasbeguosebofubasgfsgrsgr@hotmail.com");
    SmtpServer.Send(message);
}
catch(SmtpException x)
{
    //This unfortunately only really calls authentication failures
    Console.WriteLine(x.ToString());
}

我建议您在电子邮件中向这些人发送激活链接,如果他们点击它,则会将其电子邮件设置为已验证,以便您知道可以向他们发送电子邮件。然后,您还必须确保不发送到未经身份验证的电子邮件,否则您将发送电子邮件到大块黑洞。

以下是过去曾经问过这个问题的人的一些参考资料。

Validating to see if an email exists in its mailbox

Can I check if an email address exists using .net?

how to check if a given email address actually exist in c#?