C # Check whether the email address is valid

Source: Internet
Author: User
Tags getstream valid email address

Copy codeThe Code is as follows:
/// <Summary>
/// Check whether the entered email address strEmail is valid. If it is invalid, true is returned.
/// </Summary>
Public bool CheckEmail (string strEmail)
{
Int I, j;
String strTmp, strResult;
String strWords = "abcdefghijklmnopqrstuvwxyz _-. 0123456789"; // define valid character ranges
Bool blResult = false;
StrTmp = strEmail. Trim ();
// Check whether the input string is empty. The code is executed only when it is not empty.
If (! (StrTmp = "" | strTmp. Length = 0 ))
{
// Determine whether the email address contains the "@" Number
If (strTmp. IndexOf ("@") <0 ))
{
BlResult = true;
Return blResult;
}
// Use the separator "@" to split the address into two parts for verification.
String [] strChars = strTmp. Split (new char [] {'@'});
Foreach (string strChar in strChars)
{
I = strChar. Length;
// When the front part or back part of "@" is empty.
If (I = 0)
{
BlResult = true;
Return blResult;
}
// Verify each character one by one. If it exceeds the defined character range of strWords, the address is invalid.
For (j = 0; j <I; j ++)
{
StrResult = strChar. Substring (j, 1). ToLower (); // retrieve and compare characters one by one
If (strWords. IndexOf (strResult) <0)
{
BlResult = true;
Return blResult;
}
}
}
}
Return blResult;
}

C # verify whether an Email exists. It is not the verification Email format, but the Email address.

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:
Copy codeThe Code is as follows:
<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.
Copy codeThe Code is as follows:
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:
Copy codeThe Code is as follows:
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:
Copy codeThe Code is as follows:
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.