/ ********************* CREATED by chen ********************************* **
* If you feel that I have a good article, please refer to the work of the work, explanation
* Source and original author, Thank you !!! email: Aishen944-sohu.com
*********************************************************** ***************** / using system; use system.io; use system.net; using system.net.sockets; using system.collections; using system.collections; using system.collections; using system.collections; using system.collections; .Collections.Specialized; using ksn.exceptions; use ksn.validate; namespace ksn.web.mail {///
} Set {this.mailType = value;}} ///
private string logPath = null; private string userid = null; private string password = null; private string mailEncodingName = "GB2312"; private bool sendIsComplete = false; private SmtpValidateTypes smtpValidateType = SmtpValidateTypes.Login; #endregion #region "propertys" ///
NEW IPENDPOINT (IP, Port); tc = new tcpclient (endpoint); ns = tc.getStream (); this.servername = IP.TOString (); this.initialfields ();} #endregion #region "Methods" Private Void Initialfields () // Initialization connection {logs = "=================" DATETIME.NOW.TOLONGDATESTRING () " " DATETIME.NOW.TOLONGTIMESTRING () "= = =============== " CRLF; // ****************************** ******************************************************************************** *********************************************************** ******** ErrorCodes.add ("421", "Service is not ready, shut down"); ErrorCodes.Add ("432", "need a password conversion"); errorcodes.add ("450" "The required mail operation is not completed, the mailbox is unavailable (eg mailbox busy)"); ErrorCodes.add ("451", "Abandon the required operation, the required operation is not implemented"); erroorcodes.add ("452" "System storage is insufficient, the required operation is not completed"); errorcodes.add ("454", "temporary authentication failed"); ErrorCodes.Add ("500", "Email address error"); erroorcodes.add (" 501 "," Parameter Format Error "); ErrorCodes.Add (" 502 "," command is not implemented "); errorcodes.add (" 503 "," The order is incorrect "); ErrorCodes.Add (" 504 ", "Command parameters are not implemented"); ErrorCodes.Add ("530", "Requirements"); ErrorCodes.Add ("534", "Certification Mechanism is too simple"); ErrorCodes.Add ("538", "current request The authentication mechanism needs to be encrypted "); ErrorCodes.Add (" 550 "," The current mail operation is not completed, the mailbox is unavailable (eg mailbox not found or mailbox cannot be used) "); errores.add (" 551 "," user Non-local, please try
); Erroorcodes.add ("535", "user authentication failed"); // ****************************************** *********************************************************************************** *********************************************************** ********* Rightcodes.add ("220", "Service Ready"); RightCodes.add ("221", "Service Turn Transfer Transfer Channel"); RightCodes.Add ("235", "successful "); RightCodes.Add (" 250 "," required mail operation "); RightCodes.add (" 251 "," non-local user, will forward to
Logs = cmd; // Start Write Mail Data IF (! Ismails) {CMD_B = Encoding.ASCII.GETBYTES (CMD); ns.write (cmd_b, 0, cmd_b.length);} else {cmd_b = encoding.getencoding this.mailEncodingName) .GetBytes (cmd); ns.BeginWrite (cmd_b, 0, cmd_b.Length, new AsyncCallback (this.asyncCallBack), null);} // read the server response StreamReader reader = new StreamReader (ns); string response = reader.ReadLine (); logs = response CRLF; // check the status code string statusCode = response.Substring (0,3); bool isExist = false; bool isRightCode = true; foreach (string err in this.errorCodes. Keys) {if (statusCode == err) {isExist = true; isRightCode = false; break;}} foreach (string right in this.rightCodes.Keys) {if (statusCode == right) {isExist = true; break;} } // According to the status code, the next action IF (! ISexist) // is not a legitimate SMTP host {this.setd ("Not a legitimate SMTP host, or server deny service");} else if (! Isrightcode) / / The command has not been successfully executed {this.seterror (statuscode ":" this.errorcodes [statu SCODE // The else // command successfully executed {this.ErrorMessage = "";} Return Response;}}} ///
/// Login Server /// summary> private void landingByplain () {string null = (); string loginstr = null this.userid null this.password null this.password String Base64loginstr = this.convertbase64String (Loginstr, "ASCII"); // Handshake this.sendcommand ("Helo" this.servername, false); // Login this.sendCommand (Base64loginstr, false);} /// < Summary> /// Log in /// summary> private vid landingBycrammd5 () {// Handshake ThisndCommand ("Helo" this.servername, false; // Login String Response = THIS .sendcommand ("Auth CRAM-MD5", FALSE); // Get the logo returned by the server String Identifier = response.remove (0, 4); // encrypted with MD5 KSN_MACTRIPLEDES MAC = New KSN_MACTRIPLEDES (); mac.key = This.Password; mac.data = encoding.ascii.getbytes; string hash = mac.getHashValue (); // Send User account information this.sendcommand (this.userid " hash, false);} // /
break; case SmtpValidateTypes.Plain: this.landingByPlain (); break; case SmtpValidateTypes.CRAMMD5: this.landingByCRAMMD5 (); break; default: break;} // initialize mail session (corresponding to SMTP command mail) this.sendCommand ( "mail From: <" mail.sender "> ", false); // Identify the recipient (corresponding SMTP command RCPT) Foreach (String receive in mail.receivers) {this.sendcommand (" rcpt to: <" receive " > ", false);} // Logo Start Entering Mail Content (DATA) This.sendCommand (" DATA ", FALSE); // Start writing mail content string message =" Subject: " mail.subject crlf; message = "X-mailer:" mail.xmailer crlf; message = "mime-version: 1.0" CRLF; if (mail.ttachments.count == 0) // No attachment {if (mail.mailtype == mailtypes. TEXT) // Text Format {Message = "Content-Type: Text / Plain;" CRLF "" .padright (8, ') "Charset = /" mail.mailencoding.toString () "/" " CRLF; message =" Content-Transfer-Encoding: base64 " CRLF CRLF; if (mail.MailBody = null!) message = Convert.ToBase64String (mail.MailBody, 0, mail.MailBody.Length) CRLF CRL F CRLF "." CRLF;} else // HTML format {Message = "Content-Type: Multipart / Alertnative;" CRLF "" .padright (8, ') "Boundary" "= /" = ==== 003_Dragon310083331177 _ ===== / "" CRLF CRLF CRLF; Message = "this is a multi-part message in mime format" CRLF CRLF; Message = "
- ===== 003_dragon310083331177 _ ===== " CRLF; Message =" Content-type: text / html; " CRLF " ".padright (8, ') " charset = / " mail .MailEncoding.ToString () ToLower () "/" " CRLF; message =." Content-Transfer-Encoding: base64 " CRLF CRLF; if (mail.MailBody = null!) message = Convert.ToBase64String (mail .Mailbody, 0, mail.mailbody.length) CRLF CRLF; Message = "- ===== 003_Dragon310083331177 _ ===== -" CRLF CRLF CRLF "." CRLF;}} Else // With an attachment {// Processing data stringcollection attatchmentDataS = new stringcollection () {if (! File.exists (path)) {if (! File.exists (path)) {.. .. {ix. Seterror ("The specified attachment is not found;} else {// gets the byte stream fileInfo file = new fileinfo (path); filestream fs = new filestream (path, filemode.open, fileaccess.read); IF (fs.length>) {this.seEterror ("The size of the attachment beyond the maximum limit");} Byte [] file_b = new byte [(int) fs.length]; fs.read File_B, 0, File_B.LENGT h); fs.close (); string attatchmentmailstr = "Content-type: Application / OCTET-STREAM;" CRLF "" .padright (8, ') "Name =" "/"" file.name "/" " Crlf; attatchmentmailstr =" Content-Transfer-Encoding: base64 " crlf; attatchmentmailstr =" Content-disposition: attachment; " CRLF " "
.Padright (8, '') "filename =" "/"" file.name "/"" crlf crlf; attatchmentmailstr = convert.tobase64string (file_b, 0, file_b.length CRLF CRLF; AttatchMentDataS.Add (AttatchMAilStr);}} // Set mail information if (mail.mailtype == mailtypes.text) // text format {Message = "Content-type: multipart / mix;" CRLF "" .padright (8 , '') "Boundary = /" ===== 001_dragon320037612222 _ ===== / "" CRLF CRLF; Message = "this is a multi-part message in mime format." CRLF CRLF; Message = "- ======001_dragon3200376122222_ =====" CRLF; Message = "Content-Type: text / plain;" CRLF "" .padright (8, ') "charset = /" " mail.MailEncoding.ToString () ToLower () "/" " CRLF; message =." Content-Transfer-Encoding: base64 " CRLF CRLF; if (! mail.MailBody = null) message = Convert.ToBase64String (mail.mailbody, 0, mail.mailbody.length) CRLF; Foreach (String s in attatchmentdata) {message = "- ===== 001_dragon320037612222 _ =====" CRLF S CRLF CRLF; } Message = "- ===== 001_d Ragon320037612222_ ===== - " CRLF CRLF CRLF ". " CRLF;} else {Message =" Content-Type: Multipart / Mixed; " CRLF " ".padright (8, ''
) "boundary = /" ===== 001_dragon255511664284 _ ===== / "" CRLF CRLF; Message = "this is a multi-part message in mime format." CRLF CRLF; Message = "- - ===== 001_Dragon255511664284 _ ===== " CRLF; Message =" Content-type: text / html; " CRLF " ".padright (8, ') " charset = / "" mail. . MailEncoding.ToString () ToLower () "/" " CRLF; message =" Content-Transfer-Encoding: base64 " CRLF CRLF; if (mail.MailBody = null!) message = Convert.ToBase64String (mail. Mailbody, 0, mail.mailbody.Length) CRLF CRLF; for (int i = 0; i