

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, 12:17 PM
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, 7:06 AM
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, 7:01 AM
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, 9:51 AM
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, 9:15 AM
TcpClient tClient = new TcpClient("gmail-smtp-in.l.google.com", 587);
mehrtash souriPosted Feb 5, 2017, 9:14 AM
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, 1:06 PM
Great ...
Munesh SharmaPosted Jul 13, 2016, 3:23 AM
Nice
prashanth SuthojuPosted Mar 22, 2016, 6:33 AM
it only working for gmail i need others also can anyone help me..
Sudheer GPosted Mar 15, 2016, 2:57 AM
If am enter yahoo, hotmail it's not working. Other email ids how to write code???
dharmendar singhPosted Sep 28, 2015, 1:41 AM
Can u please provide to check all type of domain like yahoo,redif etc
manikandan cPosted Aug 27, 2015, 5:50 AM
can u please provide to check all type of domain like yahoo,redif etc
SandyPosted Apr 20, 2015, 2:36 AM
you can take any of yahoo mail id.e.g. [email protected]
sankar raoPosted Apr 18, 2015, 7:10 AM
can you please provide for all type of email addresses
sankar raoPosted Apr 17, 2015, 7:42 AM
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, 5:30 AM
thnaks Kirtan Patel Sir.
Riddhi ValechaPosted Dec 25, 2014, 8:07 AM
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, 8:45 AM
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, 7:52 AM
It block my 25 port ip
janvi pateleditedPosted Nov 21, 2012, 4:57 AMEdited Nov 22, 2012, 12:38 AM
nice artical
Syed AliPosted Mar 20, 2012, 2:21 AM
its check only domail not user id
Jimmy UnderwoodPosted Dec 24, 2011, 12:01 AM
Thanks Mr. Kirtan
Alok PandeyPosted Dec 23, 2011, 1:51 AM
Nice article, Kirtan. I was searching for such article.
Daisy KrausePosted Dec 23, 2011, 1:49 AM
Nice article. Thanks for sharing it.
Arjun PanwarPosted Dec 23, 2011, 1:41 AM
Thanks for sharing
Dinesh BeniwalPosted Dec 23, 2011, 1:31 AM
Welcome back