

Here is how typical mail server communication is done.
Receiver: 220 server.com
Simple Mail Transfer Service Ready
Sender: HELO server.com
Receiver: 250 server.com
Sender: MAIL FROM: <[email protected]>
Receiver: 250 OK
Sender: RCPT TO: <[email protected]>
Receiver: 250 OK
- Connect to server
- Perform HELO
- Fire MAIL FROM command
- Fire RCPT TO command and check Response
Now to perform all this we need to communicate with the server using Sockets I selected TcpClient to perform code. code like below to perform communication with server.
- using System.Net.Sockets;
- using System.IO;
- using System.Text;
- public partial class _Default: System.Web.UI.Page {
- protected void btnCheck_Click(object sender, EventArgs e) {
- 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:<[email protected]>" + 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:<" + TextBox1.Text.Trim() + ">" + CRLF);
- netStream.Write(dataBuffer, 0, dataBuffer.Length);
- ResponseString = reader.ReadLine();
- if (GetResponseCode(ResponseString) == 550) {
- Response.Write("Mai Address Does not Exist !<br/><br/>");
- Response.Write("<B><font color='red'>Original Error from Smtp Server :</font></b>" + ResponseString);
- }
- /* QUITE CONNECTION */
- dataBuffer = BytesFromString("QUITE" + CRLF);
- netStream.Write(dataBuffer, 0, dataBuffer.Length);
- tClient.Close();
- }
- private byte[] BytesFromString(string str) {
- return Encoding.ASCII.GetBytes(str);
- }
- private int GetResponseCode(string ResponseString) {
- return int.Parse(ResponseString.Substring(0, 3));
- }
- }


madhu mithaPosted May 17, 2021, 8:17 AM EDT
The code is not working for me." A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. 74.125.200.26:25 " shows this error
kokila selvamaniPosted Jun 4, 2018, 3:06 AM EDT
Hello i can only check for gmail account not able to check for outlook ,live accounts pls help to solve this
Vicky SktPosted Sep 21, 2017, 3:01 AM EDT
I use your code for yahoo and outlook domain. But this gives me an exception on connection after the hello message the exception is the following "550 5.7.1 Service unavailable, Client host [103.255.5.117] blocked using Spamhaus. To request removal from this list see http://www.spamhaus.org/lookup.lasso (AS3130)."Please help me what should i do. I try www.spamhaus.org/lookup.lasso but i nothing understand what should i do for this.
Akshay PorwalPosted Sep 19, 2017, 5:51 AM EDT
Hello, very nice article, i am facing one issue that , whatever the email id i give in dataBuffer = BytesFromString("RCPT TO:<" + txtDLApprovalSendTo.Text.Trim() + ">" + CRLF); its always coming "250"(i.e correct),.. Please help me to solve this issue . why it is always coming correct instead to goving wrong address. or you can email me on [email protected] in advance]
mehrtash souriPosted Feb 5, 2017, 4:15 AM EST
TcpClient tClient = new TcpClient("gmail-smtp-in.l.google.com", 587);
mehrtash souriPosted Feb 5, 2017, 4:14 AM EST
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 74.125.68.27:587
Manav PandyaPosted Oct 4, 2016, 9:06 AM EDT
Great ...
Munesh SharmaPosted Jul 12, 2016, 11:23 PM EDT
Nice
prashanth SuthojuPosted Mar 22, 2016, 2:33 AM EDT
it only working for gmail i need others also can anyone help me..
Sudheer GPosted Mar 14, 2016, 10:57 PM EDT
If am enter yahoo, hotmail it's not working. Other email ids how to write code???
dharmendar singhPosted Sep 27, 2015, 9:41 PM EDT
Can u please provide to check all type of domain like yahoo,redif etc
manikandan cPosted Aug 27, 2015, 1:50 AM EDT
can u please provide to check all type of domain like yahoo,redif etc
SandyPosted Apr 19, 2015, 10:36 PM EDT
you can take any of yahoo mail id.e.g. [email protected]
sankar raoPosted Apr 18, 2015, 3:10 AM EDT
can you please provide for all type of email addresses
sankar raoPosted Apr 17, 2015, 3:42 AM EDT
For gmail accounts it is working fine but how to check other mails like yahoo, rediff etc...... can you please help for this
Punit SinghPosted Feb 26, 2015, 12:30 AM EST
thnaks Kirtan Patel Sir.
Riddhi ValechaPosted Dec 25, 2014, 3:07 AM EST
Hi...Please help me out. I am also stuck in this issue. I have a domain (in.infosys.com). I have to check whether the email ID ([email protected]) really exists or not. Also, I tried this example. But I am getting error - Connection Time out.
Bhaskar SPosted Oct 13, 2014, 4:45 AM EDT
How to make this work for remaining mail servers like yahoo, rediff....etc... i tried as you explained above in command prompt to get the mail server and used it in your code, it is showing error not working.. please help me
SandyPosted Jun 12, 2014, 3:52 AM EDT
It block my 25 port ip
janvi pateleditedPosted Nov 20, 2012, 11:57 PM ESTEdited Nov 21, 2012, 7:38 PM EST
nice artical
Syed AliPosted Mar 19, 2012, 10:21 PM EDT
its check only domail not user id
Jimmy UnderwoodPosted Dec 23, 2011, 7:01 PM EST
Thanks Mr. Kirtan
Alok PandeyPosted Dec 22, 2011, 8:51 PM EST
Nice article, Kirtan. I was searching for such article.
Daisy KrausePosted Dec 22, 2011, 8:49 PM EST
Nice article. Thanks for sharing it.
Arjun PanwarPosted Dec 22, 2011, 8:41 PM EST
Thanks for sharing
Dinesh BeniwalTOP 10Posted Dec 22, 2011, 8:31 PM EST
Welcome back