Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to connect to mail servers (using a TcpClient) in order to test if an email address is valid without sending an actual email (this is for a client). I am currently using GMail for testing and I am able to successfully connect to the SMTP server. However, when I try to perform a RCPT TO command it says authentication failed (530 error). I have also testing this on the live.com SMTP server and also get an authentication error.

How can I test if an email address is valid on a SMTP, using a TcpClient, without actually sending an email or needing an account on that server?

Thank you in advance for any help.

What I have tried:

Below is the code I am currently using. It hasn't been optimized yet. This will be done once I get the RCPT TO command to work successfully.

C#
using (var client = new TcpClient())
            {
                host = "smtp.gmail.com";
                var port = 465;
                client.Connect(host, port);
                // as gmail requires ssl we should use sslstream
                // if your smtp server doesn't support ssl you can
                // work directly with the underlying stream
                using (var stream = client.GetStream())
                using (var sslstream = new SslStream(stream))
                {
                    sslstream.AuthenticateAsClient(host);
                    using (var writer = new StreamWriter(sslstream))
                    using (var reader = new StreamReader(sslstream))
                    {
                        string read = "";
                        if (stream.DataAvailable)
                        {
                            read = reader.ReadLine();
                        }

                        if (!read.StartsWith("220"))
                        {
                            return false;
                        }
                        writer.WriteLine("EHLO " + host);
                        writer.Flush();

                        do
                        {
                            read = reader.ReadLine();
                        } while (read.StartsWith("250-"));


                        if (read.StartsWith("220") || read.StartsWith("250"))
                        {
                            writer.WriteLine("mail from:<test@example.com>");
                            writer.Flush();
                            read = reader.ReadLine();
                            if (read.StartsWith("530"))
                            {
                                do
                                {
                                    read = reader.ReadLine();
                                } while (read.StartsWith("530-"));
                            }
                            if (read.StartsWith("250"))
                            {
                                writer.WriteLine("rcpt to:<" + _emailAddress + ">");
                                writer.Flush();
                                read = reader.ReadLine();
                                if (read.StartsWith("250"))
                                {
                                    writer.WriteLine("quit");
                                    writer.Flush();
                                    read = reader.ReadLine();
                                    if (read.StartsWith("221"))
                                    {
                                        return true;
                                    }
                                }
                                else
                                {
                                    writer.WriteLine("quit");
                                    writer.Flush();
                                }
                            }
                            else
                            {
                                writer.WriteLine("quit");
                                writer.Flush();
                            }
                        }
                        else
                        {
                            writer.WriteLine("quit");
                            writer.Flush();
                        }
                        // gmail responds with: 220 mx.google.com esmtp
                    }
                }
            }
            return true;
Posted
Comments
MadMyche 16-Nov-18 17:28pm    
Are you really using "test@example.com" as the MailFrom value?
Dominick Marciano 16-Nov-18 18:46pm    
I originally tried that for testing purposes but I also tried using my own GMail address. Should the MAIL FROM field matter as I really only need to validate the RCPT TO value? And just for clarification, my client basically just wants it to function like this
[^]. This RCPT TO checking is the only thing I'm stuck on at the moment.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900