usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.IO;usingSystem.Linq;usingSystem.Net.Mail;usingSystem.Net.Sockets;usingSystem.Security.Cryptography;usingSystem.Text;usingSystem.Threading;usingSystem.Windows.Forms;namespaceSMTP Test { Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); } BOOLb =false; Private voidForm1_Load (Objectsender, EventArgs e) { } Private voidButton1_Click (Objectsender, EventArgs e) { for(inti =0; I <5; i++) {Thread th=NewThread (NewThreadStart (Delegate{Console.WriteLine (CHECKSMTP ("smtp.126.com", -,"User","pswd")); })); Th. IsBackground=true; Th. Start (); } } Private BOOLCHECKSMTP (stringSmtpServer,intPortstringUsernamestringpassword) {TcpClient TcpClient=NewTcpClient (SmtpServer, Port); NetworkStream Stream=Tcpclient.getstream (); if(! Waitefor (Stream," -"))return false; SendCommand (Stream,"HELO 211.152.50.xxx\r\n"); if(! Waitefor (Stream," -"))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 BOOLWaitefor (NetworkStream Stream,stringstrcode) { intstreamsize; byte[] Readbuffer =New byte[1024x768]; Streamsize= Stream. Read (Readbuffer,0, readbuffer.length); stringreturnvalue = Encoding.Default.GetString (Readbuffer). Substring (0, streamsize); //Console.WriteLine (returnvalue); returnReturnvalue.substring (0,3). Equals (Strcode); } Private voidSendCommand (NetworkStream Stream,stringstrcmd) { byte[] writebuffer; WriteBuffer=Encoding.Default.GetBytes (Strcmd); Stream. Write (WriteBuffer,0, writebuffer.length); } Private stringBase64Encode (stringstr) { byte[] barray; Barray=Encoding.Default.GetBytes (str); returnconvert.tobase64string (Barray); } }}
C#SMTP Multithreading