我正在尝试测试我的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}rn", "machinename"));
if (!CheckResponse(socket, ResponseEnum.GenericSuccess))
{
socket.Close();
continue;
}
SendData(socket, string.Format("MAIL FROM: <{0}>rn", "from@domain.com"));
CheckResponse(socket, ResponseEnum.GenericSuccess);
SendData(socket, string.Format("RCPT TO: <{0}>rn", email));
bool result = CheckResponse(socket, ResponseEnum.GenericSuccess);
if (!result)
{
socket.Close();
continue;
}
else
return true;
}
}
}
return false;
}
关于这个问题有很多,其中很多都有一个大致的答案。如果您要发送的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 = "rn";
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#中?