Question

I am trying to test whether my hotmail account exists or not , but the following code did not help me.

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;
}
Was it helpful?

Solution

There are many questions on this, and a lot of them have the same general answer. If the SMTP server you're sending to allows it, you can confirm if the email address exists by using SMTP commands like RCPT TO, VRFY or EXPN.

the problem is, it looks like hotmail doesn't allow this.

The following code works on gmail (and authenticates whether a user is there), but you can't use it to authenticate hotmail addresses - their SMTP server returns invalid for everything.

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

And something like this will let you log into hotmail's SMTP to send an email but it won't return whether or not the email is correct as long as your credentials are correct.

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

What I would recommend is sending an activation link in your email to these people, and if they click it, it sets their email to verified so you know you can send emails to them. You would then also have to make sure you don't send to unauthenticated emails otherwise you're sending emails into a massive black hole.

Here are some references for people who have asked this in the past.

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#?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top