How to Check if Email Id exists or Not
You can validate email id input by your user in application.Email can be validated using :
1) Socket
2) TcpClient
Both comes with System.Net.Sockets namespace
Here' the code and the explanation :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void EmailExistsOrNot(string emailId) | |
{ | |
//This code checks if Email Id exists or not | |
string host = "gmail-smtp-in.l.google.com"; | |
byte[] dataBuffer; | |
string ctrl = "\r\n"; | |
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); | |
IPAddress[] IPs = Dns.GetHostAddresses(host); | |
s.Connect(IPs[0], 25); | |
NetworkStream netStream = new NetworkStream(s); | |
StreamReader reader = new StreamReader(netStream); | |
string responseString = reader.ReadLine(); | |
//HELO : The client sends this command to the SMTP server to identify itself and initiate the SMTP conversation. | |
//The domain name is usually sent as an arguement together with command | |
dataBuffer = Encoding.ASCII.GetBytes("HELO client.demo.com" + ctrl); | |
//Call the Write method to send data to the remote host. | |
netStream.Write(dataBuffer, 0, dataBuffer.Length); | |
//Call the Read method to receive data arriving from the remote host | |
// The server acknowledges to HELO command and is ready for the service | |
responseString = reader.ReadLine(); | |
dataBuffer = Encoding.ASCII.GetBytes("MAIL FROM:<customercare@hotehomes.com>"+ctrl); | |
netStream.Write(dataBuffer, 0, dataBuffer.Length); | |
responseString = reader.ReadLine(); | |
dataBuffer = Encoding.ASCII.GetBytes("RCPT TO:<" + emailId + ">" + ctrl); | |
netStream.Write(dataBuffer, 0, dataBuffer.Length); | |
responseString = reader.ReadLine(); | |
if(responseString.Contains("250")) | |
{ | |
Response.WriteLine("EmailId exists"); | |
} | |
else | |
{ | |
Response.WriteLine("EmailId doesn't exist"); | |
} | |
} |
- Socket provides properties and methods for network communication, allowing both synchronous and asynchronous data transfer. More about Sockets : Sockets
- IPAddress[] IPs = Dns.GetHostAddresses(host);
Connecting the socket with port number is very important otherwise it will throw exception.
More about Socket.Connect : Socket.Connect()
- NetworkStream : Provides methods for sending and receiving data over Stream sockets. To create instance of NetworkStream, we must provide a connected socket. Once the network stream is instantiated, we use Write() method to send data to remote host and StreamReader use Read method to receive data arriving from remote host on NetworkStream.
|
This is how we can check If email Id exists or not.
Note : Your ISP might have block Port 25 to refrain spammers . In that case RCPT to command will return 550 even if EmailId exists.
Note : Your ISP might have block Port 25 to refrain spammers . In that case RCPT to command will return 550 even if EmailId exists.
That is why during registration, most websites send an Activation Link mail which helps them recognize that the email Id is valid.
In next posts :
2) How to send Activation Link mail from your application
No comments:
Post a Comment