Click here to Skip to main content
15,904,153 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good afternoon.
I have the following code which I derived from http://www.codeproject.com/KB/validation/Valid_Email_addresses.aspx:
preusing System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace EmailValidation
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public static string SmtpServer;
        private enum SMTPResponse : int
        {
            CONNECT_SUCCESS = 220,
            GENERIC_SUCCESS = 250,
            DATA_SUCCESS = 354,
            QUIT_SUCCESS = 221
        }
        public bool VerifyEmail()
        {
            string address = textBox2.Text.Trim();
            string[] host = (address.Split('@'));
            string hostname = host[1];
            IPHostEntry IPhst = Dns.GetHostEntry(hostname);
            IPEndPoint endPt = new IPEndPoint(IPhst.AddressList[0], 80);//25 //21 //Port?
            Socket s = new Socket(endPt.AddressFamily,
                         SocketType.Stream, ProtocolType.Tcp);
            s.Connect(endPt);
            //Attempting to connect
            if (!Check_Response(s, SMTPResponse.CONNECT_SUCCESS))
            {
                s.Close();
                return false;
            }
            //HELLO server
            Senddata(s, string.Format("HELLO {0}\r\n", Dns.GetHostName()));
            if (!Check_Response(s, SMTPResponse.GENERIC_SUCCESS))
            {
                s.Close();
                return false;
            }
            //Identify yourself
            //Servers may resolve your domain and check whether
            //you are listed in BlackLists etc.
            Senddata(s, string.Format("MAIL From: {0}\r\n",
                 "[my_email]"));
            if (!Check_Response(s, SMTPResponse.GENERIC_SUCCESS))
            {
                s.Close();
                return false;
            }
            //Attempt Delivery (I can use VRFY, but most
            //SMTP servers only disable it for security reasons)
            Senddata(s, address);
            if (!Check_Response(s, SMTPResponse.GENERIC_SUCCESS))
            {
                s.Close();
                return false;
            }
            return (true);
        }
        private static void Senddata(Socket s, string msg)
        {
            byte[] _msg = Encoding.ASCII.GetBytes(msg);
            s.Send(_msg, 0, _msg.Length, SocketFlags.None);
        }
        private static bool Check_Response(Socket s, SMTPResponse response_expected)
        {
            string sResponse;
            int response;
            byte[] bytes = new byte[1024];
            while (s.Available == 0)
            {
                System.Threading.Thread.Sleep(100);
            }
            s.Receive(bytes, 0, s.Available, SocketFlags.None);
            sResponse = Encoding.ASCII.GetString(bytes);
            response = Convert.ToInt32(sResponse.Substring(0, 3));
            if (response != (int)response_expected)
                return false;
            return true;
        }
        private void button1_Click(object sender, EventArgs e)
        {
                       bool Result = VerifyEmail();
            if (Result == true)
            {
                MessageBox.Show("Email is valid.");
            }
            else
            {
                MessageBox.Show("Email is not valid.");
            }
        }
    }
}

When I enter an email address and click the button, it enters and endless loop. Any suggestions? WHEELS
Posted

After a quick scan of your code, I don't see where the endless loop is. But, you could be interpreting a blocked function call as an 'endless loop".

Your trying to connect to an SMTP server using the domain name entered from the email address, but you're trying to connect to port 80, which is a web server, not SMTP. A quick Google would tell you that SMTP is on port 25.

Verifying the address by querying the SMTP host, nowadays, is a complete waste of time. Unless the host is extremely stupid and likes verifying the email lists of spammers, you shouldn't get back any result at all.

Just send the email to the address provided blindly. If there is anyone there to read it, you'll get a response from the user.
 
Share this answer
 
Comments
Dalek Dave 10-Jan-11 14:59pm    
I think that covers everything!
Your endless loop is here:

C#
while (s.Available == 0)            
{
    System.Threading.Thread.Sleep(100);
}


It goes into that loop and since s.Available cannot possibly change, it just sits and spins in that loop. Of course, you could have determined this if you had gone ahead and used the debugger that comes with Visual Studio.

How you're going to fix it is up to you, but I recommend that you look into setting up a thread that either calls VerifyEmail() or replaces it.
 
Share this answer
 
v3
As Dave Kreskowiak says, why would you even want to verify outgoing email?

OK, a simple regex validator could confirm the Syntax is correct, but other than that, why bother?



If you have a white list you could compare it, so that only mail to preapproved receivers would be sent, but it is time consuming and pointless unless there are serious security issues.
 
Share this answer
 
v2

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