-2

我正在尝试测试我的 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;
}
4

1 回答 1

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());
}

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

这里有一些过去问过这个问题的人的参考资料。

验证以查看其邮箱中是否存在电子邮件

我可以使用 .net 检查电子邮件地址是否存在吗?

如何检查给定的电子邮件地址是否确实存在于c#中?

于 2013-03-20T22:06:59.653 回答