Www.chinacs.net 2002-5-9 Chinese C # Technology Station
Mail Send Class, support HTML format, support priority settings. Implemented through the Socket class
Using system;
Using system.text;
Using system.io;
Using system.net;
Using system.net.sockets;
Namespace blood.com.classlib {///
public void SendCommandToServer (string Command) {NetworkStream ns = this.GetStream (); byte [] WriteBuffer; WriteBuffer = new byte [1024]; WriteBuffer = Encoding.Default.GetBytes (Command); ns.Write (WriteBuffer, 0, WriteBuffer .Length); return;} public string GetServerResponse () {int streamSize; string ReturnValue = ""; byte [] ReadBuffer; NetworkStream ns = this.GetStream (); ReadBuffer = new byte [1024]; streamSize = ns.Read ( Readbuffer, 0, Readbuffer.Length; IF (streamsize == 0) {Return ReturnValue;} else {returnvalue = encoding.default.getstring (Return ReturnValue;}}
Public Bool DoesStringContainsmtpcode (String S, String SMTPCode) {Return (S.indexof (SMTPCode, 0, 10) == - 1)? False: true;
} // End class
///
///
///
///
///
///
/ / Judgment Whether the connection IF (! Bolconnect) {strerrMessage = "SMTP server connection failed ..."; Return;}
// Read feedback strResponseNumber = smtpcMail.GetServerResponse (); if (smtpcMail.DoesStringContainSMTPCode (strResponseNumber, "220")) {this.strResponse = strResponseNumber;} else {this.strErrMessage = "connection failed" strResponseNumber; return }
String [] stringbuffer = new string [6]; string [] strresponsecode = {"220", "250", "251", "354", "221"}; // Success Codes from SMTP Server
String strdata = ""; strdata = string.concat ("helo", smtphost); strdata = string.concat (strdata, "/ r / n"); strsndbuffer [0] = strdata;
StrData = ""; strdata = string.concat ("Mail from:", "<" from ">); strdata = string.concat (strdata," / r / n "); strsndbuffer [1] = strdata ;
strdata = ""; strdata = string.concat ("RCPT TO:", "<" to ">"); strdata = string.concat (strdata, "/ r / n"); strsndbuffer [2] = strdata ;
StrData = ""; strdata = string.concat ("data", "/ r / n"); strsndbuffer [3] = strdata;
Strdata = ""; strdata = string.concat ("from:", DisplayFromName "<" from ">"); strdata = string.concat (strdata, "/ r / n"); strdata = string.concat (strData, "to:"); strdata = string.concat (strdata, displaytoname <" to "> "); strdata = string.concat (strdata," / r / n "); strdata = string. Concat (strdata, "Subject:"); strdata = string.concat (strdata = string.concat); strdata = string.concat; strdata = string.concat (strdata, "MIME-VERSION: 1.0 "); strdata = string.concat (strdata," / r / n "); strdata = string.concat (strdata," x-priority: " priority); strdata = string.concat (strdata," / r / n "); strdata = string.concat (strdata," x-msmail-priority: " priority); strdata = string.concat (strdata," / r / n "); if (html == true) {strData = String.concat (strdata, "content-type: text / html;");} else {strdata = String.concat (strdata, "content-type: text / plain;");} strdata = string.concat (strdata, "/ r / n"); strdata = string.concat (strdata, "charSet = /" ISO-8859-1 / "); strdata = string.concat (strdata," / r / n "); if (HTML == True) {s Trdata = string.concat (strdata, "content-transfer-encoding: text / html;");} else {strdata = string.concat (strdata, "content-transfer-enconcoding: text / plain;");} strdata = String.concat (strdata, "/ r / n"); strdata = string.concat (strdata, "content-base: /" " base " / "); strdata = string.concat (strdata," / r / N " " / r / n ");
STRDATA = String.concat (strdata, message); strdata = string.concat (strdata, "/ r / n. / r / n"); strsndbuffer [4] = strdata; strdata = ""; strdata = string.concat Strdata, "quit / r / n"); strsndbuffer [5] = strdata;
INT i = 0;
While (i for (int j = 0; j i ;} // End cycle} catch (socketexception err) {this.strerrMessage = err.stack " Err.stackTrace;} catch (exception e) {this.strerrMessage = E.MESSAGE " E .Stacktrace;}} // End mail sending method } // End class } // End Namespace Mail Send a front page <% @ Page language = "c #" codebehind = "Webform1.aspx.cs" autoeventwireup = "false" inherits = "test.sendmailsample"%>