In previous programming, for example, when writing user data, you sometimes need to check whether the email entered by the user is authentic and valid. In the past, we could only verify whether the email contains certain special characters, for example "@",". ",". com, etc., only to determine the legitimacy of the email, prove that the user entered the email format is correct, but whether this email actually exists in the network, there is no way.
First, you need to understand the SMTP protocol.
1. SMTP works in two situations: one is to transfer an email from the client to the server, and the other is to transfer it from one server to another.
Server
2. SMTP is a request/response protocol. commands and responses are based on ASCII text and end with Cr and LF characters. The response includes a response
Three-digit code of the return status
3. SMTP listens for connection requests on TCP port 25
4. Connection and sending Process
The SMTP protocol is not complicated (the word "simple" is clearly included). It is simple if you know Sock. But now we only use the first article to say that when we send an email to a server from a client, the email server first verifies whether the email Sending address actually exists on the server.
The procedure is as follows:
Port 25 of the connection server (if there is no email service, the connection is white)
Send helo greetings
Send the mail from command. If 250 is returned, the server can be connected. Otherwise, the server must be verified by the sender.
Send the rcpt to command. If 250 is returned, the email exists.
Send the quit command to exit the connection.
Next we will perform this operation:
First, let's look at the page architecture:
<B> common email verification </B>
<Form runat = "server">
<Table boder = "# 6699ff">
<Tr> <TD colspan = "2" align = "center"> <asp: label id = "lblmsgshow" forecolor = "red" runat = "server"/> </TD> </tr>
<Tr> <TD> email address to be verified: </TD> <asp: textbox id = "tbemail" runat = "server"/> </TD> </tr>
<Tr> <TD> smtp mail server: </TD> <asp: textbox id = "tbserver" runat = "server"/> </TD> </tr>
<Tr> <TD> SMTP port: </TD> <asp: textbox id = "tbport" text = "25" runat = "server"/> </TD> </tr>
<Tr> <TD colspan = "2"> <asp: button id = "btnvalidate" text = "verify" onclick = "validate_email" runat = "server"/> </TD> </tr>
</Table>
<B> verification process: </B>
<Asp: Panel id = "showpro" runat = "server"/>
</Form>
The click of the button control will trigger the valiate_email event. All main program operations are completed in this event. The following describes the code used to process this event.
I don't want to repeat the TCP connection .. Please read my previous articles by yourself:
String stremail, strserver;
Int intport;
Stremail = tbemail. text;
Strserver = tbserver. text;
Intport = int32.parse (tbport. Text); file: // The default port is 25.
Tcpclient TCPC = new tcpclient ();
The server and other information come from user input and establish a connection with port 25 of the server.
Try
{
TCPC. Connect (strserver, intport );
Streamreader sr = new streamreader (TCPC. getstream (), encoding. Default );
Sr. Readline ();
...
}
Note the two points in the above Code: 1. In beta2, you cannot determine whether the established connection is successful by checking the return value. You can only identify the connection by capturing error exceptions. 2. Open the connection, when stream is used to read data, there must be an SR. readline. One row is the server's welcome information and version information.
Follow the steps above to complete the operation:
File: // write the HELO command
If (operastream (TCPC, "helohttp: // www.webjx.com ")! = "250 ")
{
Lblmsgshow. Text = "the HELO command cannot be completed. This port may not provide the SMTP service ";
Operastream (TCPC, "quit ");
Return;
}
File: // write the mail from command
If (operastream (TCPC, "mail from: web@webjx.com ")! = "250 ")
{
Lblmsgshow. Text = "The MAIL command cannot be completed, and the SMTP service needs to be verified ";
Operastream (TCPC, "quit ");
Return;
}
File: // write the RCPT command. This is a key step. The following parameter is the email address to be queried.
If (operastream (TCPC, "rcpt to:" + stremail )! = "250 ")
{
Lblmsgshow. Text = stremail + "this email address is not valid ";
Operastream (TCPC, "quit ");
Return;
}
Else
{
Lblmsgshow. Text = stremail + "is a valid Email Address ";
Operastream (TCPC, "quit ");
Return;
}
The host name after the HELO command does not need to be written on some email servers, such as IMail, but it is still well written. Of course, you can also cheat the server, but generally the server can check it out.
Operastrem is a custom function used to operate the connected stream:
Public String operastream (tcpclient TCPC, string strcmd)
{
Stream tcpstream;
Strcmd = strcmd + "/R/N"; file: // Add a line break
Tcpstream = TCPC. getstream ();
Byte [] bwrite = encoding. Default. getbytes (strcmd. tochararray ());
Tcpstream. Write (bwrite, 0, bwrite. Length );
Streamreader sr = new streamreader (TCPC. getstream (), encoding. Default );
String RL = Sr. Readline ();
String sp = RL. substring (0, 3 );
Showpro. controls. add (New literalcontrol ("Run the command: <font color = Red>" + strcmd + "</font> <br> return data: "+ RL +" <br> "));
Return sp;
}
The return value of this function is the stream information code used to determine whether the operation is successful. 250 indicates that the operation is successful, and 550 indicates that the operation can only be applied to local emails. That is to say, the sender must be a user on the server, for example, when you connect to smtp.163.net, you must have a valid 163.net account. In this way, the server prevents external users from sending spam through the service.