Copy the codeThe code is as follows:
/// <summary>
/// Detect whether the entered email address strEmail is legal, and returns true if it is not legal.
/// </summary>
public bool CheckEmail(string strEmail)
{
int i, j;
string strTmp, strResult;
string strWords = "abcdefghijklmnopqrstuvwxyz_-.0123456789"; //Define the legal character range
bool blResult = false;
strTmp = ();
//Execute the code only if the input string is empty and is not empty.
if (!(strTmp == "" || == 0))
{
//Judge whether there is a "@" number in the email address
if ((("@") < 0))
{
blResult = true;
return blResult;
}
//Use the "@" symbol as the splitter, divide the address into two parts and verify them separately.
string[] strChars = (new char[] { '@' });
foreach (string strChar in strChars)
{
i = ;
//When the front or back part of the “@” sign is empty.
if (i == 0)
{
blResult = true;
return blResult;
}
// Verify word by word. If the defined character range strWords is exceeded, it means that the address is illegal.
for (j = 0; j < i; j++)
{
strResult = (j, 1).ToLower();//Fetch out and compare characters one by one
if ((strResult) < 0)
{
blResult = true;
return blResult;
}
}
}
}
return blResult;
}
C# Verify whether the email actually exists, not whether the email format or whether the email address exists
In previous programming, for example, when writing user information, sometimes we need to confirm whether the email entered by the user is real and valid. In the past, we could only verify whether the email contained some special characters, such as "@", ".", ".com", etc. All we did was to judge the legality of the email and prove that the format of the email filled in was correct, but there was no way to whether the email really exists on the network.
First of all, we need to understand the SMTP protocol.
It works in two situations: one is to transmit email from the client to the server; the other is to transmit email from one server to another
server
It is a request/response protocol, and the command and response are based on ASCII text and end with CR and LF characters. The response includes a representation return
Three-digit code to return to state
Listen to connection requests on TCP protocol port 25
4. Connection and sending process
The SMTP protocol is not complicated (it clearly contains the word "simple"), but it is simple if you know Sock. But now we are just using what we said in the first article: when we transmit emails from the client to the server, when we send emails to a server, the email server will first verify whether the email sending address really exists on this server.
The steps of the operation are as follows:
Connect to the 25th port of the server (if there is no mail service, it will be a waste of connection)
Send helo greetings
Send the mail from command. If return 250 means it is correct, it is OK to connect to this server. Otherwise, it means that the server needs to verify by the sender.
Send rcpt to command, if return 250, the email exists
Send quit command to exit the connection
Let's operate this process:
First, let’s take a look at the page structure:
Copy the codeThe code is as follows:
<b>Ordinary Email Verification</b>
<form runat="server">
<table boder="#6699FF">
<tr><td colspan="2" align="center" ><asp:Label ForeColor="red" runat="server" /></td></tr>
<tr><td>Email address that needs to be verified:</td><td><asp:TextBox runat="server" /></td></tr>
<tr><td>Mail SMTP server: </td><td><asp:TextBox runat="server" /></td></tr>
<tr><td>SMTP port: </td><td><asp:TextBox Text="25" runat="server" /></td></tr>
<tr><td colspan="2" ><asp:Button Text="Validate" OnClick="Validate_Email" runat="server" /></td></tr>
</table>
<b>Expression process display:</b>
<asp:Panel runat="server" />
</form>
The click of the Button control will invoke the Valiate_Email event. All main program operations are completed in this event. Let’s explain the code in this event in detail.
I don't want to repeat the things about TCP connections. . Please read my previous articles by yourself:
string strEmail,strServer;
int intPort;
strEmail = ;
strServer = ;
intPort = (); file://default port is 25
TcpClient tcpc = new TcpClient();
Information such as server comes from user input to establish a connection with port 25 of server.
Copy the codeThe code is as follows:
try
{
(strServer,intPort);
StreamReader sr = new StreamReader((),);
();
...
}
Please note two points in the above code: First, in beta2, you can no longer judge whether the established connection is successful by judging the return value method, and you can only judge by catching error exceptions; second, when opening the connection and reading using Stream, there must be one line, and one line is the server's welcome information plus version information.
Next, follow the steps mentioned above to complete the operation:
Copy the codeThe code is as follows:
file://write HELO command
if(OperaStream(tcpc,"HELOhttp://") != "250")
{
= "The HELO command cannot be completed, this port may not provide SMTP service";
OperaStream(tcpc,"QUIT");
return;
}
file://write Mail From command
if(OperaStream(tcpc,"MAIL FROM: web@") != "250")
{
= "The MAIL command cannot be completed, the SMTP service needs to be verified";
OperaStream(tcpc,"QUIT");
return;
}
file://write RCPT command, this is a key step. The subsequent parameter is the address of the query email.
if(OperaStream(tcpc,"RCPT TO: "+strEmail) != "250")
{
= strEmail + "This email address is not valid";
OperaStream(tcpc,"QUIT");
return;
}
else
{
= strEmail + "is a legal and valid email address";
OperaStream(tcpc,"QUIT");
return;
}
The host name after the Helo command does not require Imail in some email servers, but it is better to write it. Of course, you can also write it randomly, but generally the server can check it out.
Where OperaStrem is our custom function, used to operate connection flow:
Copy the codeThe code is as follows:
public string OperaStream(TcpClient tcpc,string strCmd)
{
Stream TcpStream;
strCmd = strCmd + "\r\n"; file://add line break
TcpStream = ();
byte[] bWrite = (());
(bWrite,0,);
StreamReader sr = new StreamReader((),);
string rl = ();
string sp = (0,3);
(new LiteralControl("Execute the command: <font color=red>"+strCmd+"</font><br/>Return data: "+rl+"<br/>"));
return sp;
}
The return value of this function is the information code of the stream, used to determine whether the operation is successful. 250 means successful, and 550 means that it can only be used for local emails. That is to say, the sender must be a user on the server. For example, when connecting to smtp., you must have a real and valid account. This practice is to prevent outsiders from using the service to send spam emails.