Refer to my blog post for a souvenir, especially the hash code-use SMTP to send emails in. net.
Reference and sort out chinafcl
In smtpclient, only the email address, password, and SMTP server address are provided. How can I test whether the email address and password are correct? (Similar to the test connection function of the mail client)
Private bool checksmtp (string smtpserver, int port, string username, string password)
{
Tcpclient = new tcpclient (smtpserver, Port );
Networkstream stream = tcpclient. getstream ();
If (! Waitefor (stream, "220") return false;
Sendcommand (stream, "HELO 211.152.50.xxx \ r \ n ");
If (! Waitefor (stream, "250") return false;
Sendcommand (stream, "auth login \ r \ n ");
If (! Waitefor (stream, "334") return false;
Sendcommand (stream, base64encode (username) + "\ r \ n ");
If (! Waitefor (stream, "334") return false;
Sendcommand (stream, base64encode (password) + "\ r \ n ");
If (! Waitefor (stream, "235") return false;
Return true;
}
Private bool waitefor (networkstream stream, string strcode)
{
Int streamsize;
Byte [] readbuffer = new byte [1024];
Streamsize = stream. Read (readbuffer, 0, readbuffer. Length );
String returnvalue = encoding. Default. getstring (readbuffer). substring (0, streamsize );
Console. writeline (returnvalue );
Return returnvalue. substring (0, 3). Equals (strcode );
}
Private void sendcommand (networkstream stream, string strcmd)
{
Byte [] writebuffer;
Writebuffer = encoding. Default. getbytes (strcmd );
Stream. Write (writebuffer, 0, writebuffer. Length );
}
Private string base64encode (string Str)
{
Byte [] barray;
Barray = encoding. Default. getbytes (STR );
Return convert. tobase64string (barray );
}