Send the class (reproduced)

xiaoxiao2021-03-06  105

Using system; using system.net; using system.net.sockets; using system.collections;

Namespace e_mail {public class esmtpmail {private string enter = "/ r / n"; ///

/// Setting language code, the default setting is GB2312, if it is not required to be set to "" /// < / summary> public string charset = "gb2312"; /// /// sender address /// public string from = ""; /// /// sender Name /// public string fromname = ""; /// /// Reply email address /// // public string replyto = "; /// / // Recipient name /// public string recipientname = ""; /// /// recipient list /// private haShtable recipient = new hashtable (); / // /// mail server domain name /// private string mailserver = ""; /// /// mail server port number /// private int mailserverport = 25 ; /// /// SMTP authentication username /// private string username = "; /// /// SMTP authentication) /// < / summary> private string password = ""; /// /// Do not need SMTP verification /// private bool esmtp = false; /// /// HTML mail /// public bool html = false; /// /// message attachment list /// private system.collections.arraylist attachments; /// / /// message Send priority, can be set to "High", "Normal", "Low" or "1", "3", "5" /// priz, priority = " Normal "; /// /// mail topic ///

Public String Subject = ""; ///

/// Mail body /// public string body = ""; /// /// recipient number /// private int recipientnum = 0; /// /// Up to the number of recipients /// private Int RecipientMaxNum = 1; /// /// Distinguetors / // // private int recipientbccnum = 0; /// /// Error message feedback /// private string errmsg; /// /// TCPClient object, Connected Server /// Private TCPClient Tc; /// /// NetWorkStream Object /// private networkStream ns; /// /// SMTP error code hash Table /// private hashtable errcodeht = new hashtable (); /// /// SMTP correct code hash table /// private haShtable rightcodeht = new hashtable (); public esmtpmail () {Attachments = new system.collections.arraylist ();} /// /// signal server domain name and verification information ///-shaped, "User: pASWW.SERVER.com: 25", Subject subsistence information. Such as "User: paswww.server.com" www.server.com "/// public string maildomain {set {string maidomain = value.trim (); int Tempint

IF (Maidomain! = "") {tempint = maidomain.indexof ("@"); if (Tempint! = - 1) {string str = maidomain.substring (0, tempint); mailserverusername = str.substring (0, STR) .Indexof (":")); mailserverpassword = str.substring (str.indexof (":") 1, str.length-str.indexof (":") - 1); maidomain = maidomain.substring (Tempint 1, maidomain.Length-Tempint-1);} tempint = maidomain.indexof (":"); if (Tempint! = - 1) {mailserver = maidomain.substring (0, tempint); mailserverport = system.convert.toint32 (Maidomain.substring (Tempint 1, Maidomain.Length-Tempint-1));} else {mailserver = maidomain

}

}

}

///

/// Mail server port number /// public int maildomainport {set {mailserverport = value;}}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////> {● = value = "") {username = value.trim (); Else {username = ""; esmtp = false;}}}

/////////////////////////////////////////////////////////////////////////////////////////////bit = value;}}

///

/// mail Send priority, can be set to "High", "Normal", "Low" or "1", "3", "5" /// public string priority {Set {switch (value.tolower ()) {copy "high": priority = "high"; break;

Case "1": priority = "high"; Break;

Case "Normal": priority = "normal"; Break; Case "3": priority = "normal"; Break;

Case "low": priority = "low"; Break;

Case "5": priority = "low"; Break;

Default: priority = "normal"; Break;}}}

///

/// error message feedback /// public string errorMessage {get {return errmsg;}}

///

/// server interaction record /// private string logs = "";

///

// / server interacts records, if found the SMTP server that this component cannot be used, please send me the logs when the error (lion-a@sohu.com), I will find out the reason as soon as possible. /// public string logs {get {return logs;}}

///

/// SMTP response code hash table /// private void smtpcodeadd () {Errcodeht.add ("500", "Email Address Error"); errcodeht.add ("501" , "Parameter Format Error"); errcodeht.add ("502", "command is not implemented"); errcodeht.add ("503", "Server requires SMTP verification"); errcodeht.add ("504", "command parameters Not achievable "); errcodeht.add (" 421 "," Service is not ready, shut down the channel "); errcodeht.add (" 450 ", the email operation requiring the mail operation is not completed, the mailbox is not available (for example, mailbox busy)" ); Errcodeht.add ("550", "requested by mail operations, mailboxes are unavailable (for example, mailboxes are not found, or irresisible)"); errcodeht.add ("451", "abandon the required operation; In the process, "); errcodeht.add (" 551 "," users are not local, please try "); errcodeht.add (" 452 "," system storage is insufficient, requiring operations "); Errcodeht.Add ("552", "excess storage allocation, required operation is not executed"); errcodeht.add ("553", "mailbox name is not available, the required operation is not executed (such as a mailbox format error)"); Errcodeht.Add ("432", "need a password conversion"); errcodeht.add ("534", "authentication mechanism is too simple"); errcodeht.add ("538", "Current Request Certification Mechanism Requires")) Errcodeht.Add ("454", "Temporary Certification Failure"); Errcodeht.Add ("530", "Requirements"); RightCodeht.Add ("220", "Service Ready"); RightCodeht.Add ("250 "," Required Mail Operation "); RightCodeht.Add (" 251 "," User is non-local, forward to "); RightCodeht.add (" 354 "," starting mail input, ENTER>. End ") RightCodeht.add ("221", "Service Close Transfer Channel"); RightCodeht.Add ("334", "Server Response Verification Base64 String"); RightCodeht.Add ("235", "Verification Success");}

///

/// The string is encoded as Base64 string /// /// String Private String Base64Encode (String STR) ) {Byte [] barray; barray = encoding.default.getBytes (str); return convert.tobase64string (barray);} /// /// Decoding the base64 string is normal string /// /// String private string base64decode (string str) {byte [] barray; barray = convert.default.getstring (STR) Barray;}

///

///> File stream /// /// Accessories's absolute path private string getStream (String FilePath) {/ / Establish file flow object system.io.filestream filestr = new system.io.filestream (filepath, system.io.filemode.open); Byte [] by = new byte [system.convert.toint32 (fileStr.Length)]; FILESTR.READ (By, 0, By.Length); filestr.close (); return (system.convert.tobase64string (by));}

///

/// Add email attachment /// /// Accessories absolute path public void addattachment (string path) {attachments.add (Path) }

///

/// Add a recipient /// /// Recipient address public bool addrecipient (string str) {str = Str.trim (); if (str == null || str .indexof ("@") == - 1) Return True; if (RecipientNum

///

/// Most recipients /// public int RecipientMaxnum {set {recipientmaxnum = value;}}

///

/// Add a set of recipients (not more than RecipientMaxNum), parameter is a string number / /// /// Save a recipient Address string array (not more than RecipientMaxNum) public bool addrecipient (String [] str) {for (int i = 0; i

///

/// Send SMTP command /// private bool sendcommand (string str) {byte [] WriteBuffer; if (str == null || Str.trim () == "") {RETURN TRUE;} logs = Str; WriteBuffer = Encoding.default.getbytes (STR); trybuffer, 0, writebuffer.Length);} catch {errmsg = "network connection error"; Return False; Return True;}

///

/// Receive SMTP server response to /// prince recnusponse () {int StreetSize; string returnvalue = "; byte [] readbuffer = new byte [1024]; try {streamsize = NS.Read (readbuffer, 0, readbuffer.Length);} catCH {errmsg = "Network connection error"; return "false";

IF (streamsize == 0) {Return ReturnValue;} else {returnvalue = encoding.default.getstring (readbuffer) .substring (0, streamsize); logs = returnvalue; return returnvalue;}}

///

/// interacts with the server, send a command and receives the response. /// /// A command to send /// If the error, the information you want to feedback private Bool Dialog (String Str, String Errstr) {if (Str == Null || Str.trim () == "" "{Return true;}} (sendcommand (STR)) {string rr = recvResponse (); if ( RR == "false") {return false;} string rrcode = r.substring (0, 3); if (rightcodeht [rcode]! = Null) {ket true;} else {if (errcodeht [rrcode]! = Null ) {Errmsg = (rrcode errcodeht [rrcode] .tostring ()); errmsg = Enter;} else {errmsg = rr;} errmsg = errstr; return false;}}}

///

/// interacts with the server, sends a set of commands and receives the response. ///

Private bool dialog (str, str, string errstr) {for (int i = 0; i

Return True;}

Private Bool Sendemail () {// Connection Network TRY {TC = New TcpClient (MailServer, MailServerport);} catch (exception e) {errmsg = e.tostring (); return false;}

Ns = tc.getStream (); SMTPCodeAdd ();

/ / Verify that the network connection is correct if (rightcodeht [recject "(). Substring (0, 3)] == null) {errmsg =" Network connection failed "; return false;}

String [] SendBuffer; String SendBuffersTr;

// Perform SMTP verification if (esmtp) {sendbuffer = new string [4]; sendbuffler [0] = "ehlo" mailserver enter; sendbuffer [1] = "auth login" enter; sendbuffer [2] = base64encode Username) Enter; SendBuffer [3] = base64encode (password) Enter; if (! Dialog (SendBuffer, "SMTP server verification failed, check the username and password.")) Return false;} else {sendbufferstr = "Helo" MailServer Enter; if (! Dialog (SendBufferstr, ")) Return False;} // sendbufferstr =" Mail from: <" from "> enter; if (! Dialog (SendBufferstr, "sender Address error, or no empty ")) Return False;

// sendbuffer = new string [RecipientMaxNum]; for (int i = 0; i

Sendbuffer [I] = "RCPT TO: <" Recipient [i] .tostring () "> Enter;

} IF (! Dialog (SendBuffer, "Incomplete")) Return False;

SendBufferstr = "DATA" Enter; if (! Dialog (SendBufferstr, ")) Return False;

Sendbufferstr = "from:" fromname "<" from "> enter;

SendBufferstr = "to: =?" "?" " " <" "? = " " " "? = "> Enter; sendbufferstr =" "? =" "<" "? =" " =" Cc: "; for (int i = 0; i , "; SendBufferstr = Enter;

IF (charset == ") {sendbufferstr =" Subject: " Subject Enter;} else {sendbufferstr =" Subject: " " =? " CHARSET.TOUPPER () "? b? " base64encode (Subject) "? =") enter;} SendBufferstr = "X-priority:" priority enter; SendBufferstr = "X-MSMail-priority:" priority enter; SendBufferstr = "Importance:" priority enter; SendBufferstr = "X-mailer: huolx.pubclass" Enter; sendbufferstr = "MIME-VERSION: 1.0" ENTER;

SendBufferstr = "Content-Type: multipart / mixed;" enter; // content format and delimiters SendBufferstr = "boundary = /" ---- = _ NextPart_000_00D6_01C29593.AAB31770 / "" enter; SendBufferstr = "- ----- = _ nextpart_000_00d6_01c29593.aab31770 " enter;

IF (html) {sendbufferstr = "Content-type: text / html;" ENTER;} else {sendbufferstr = "Content-Type: text / plain;" ENTER;

IF (charset == ") {sendbufferstr =" charset = / "ISO-8859-1 /" " enter;} else {sendbufferstr =" charset = / " charset.tolower () " / "" ENTER;} // sendbufferstr = "Content-Transfer-Encoding: Base64" Enter;

SendBufferstr = "Content-Transfer-Encoding: Base64" ENTER ENTER;

SendBufferstr = Base64Encode (Body) enter; if (! Attachments.Count = 0) {foreach (string filepath in Attachments) {SendBufferstr = "------ = _ NextPart_000_00D6_01C29593.AAB31770" enter; SendBufferstr = "Content -Type: Application / OCTET-Stream " Enter; SendBufferstr =" Name = / "=?" CHARSET.TOUPPER () "? B?" Base64encode (filepath.substring (filepath.lastindexof ("//" ) 1)) "? = /" " Enter; sendbufferstr =" content-transfer-encoding: base64 " enter; sendbuffrstr =" content-disposition: attachment; " enter; sendbufferstr =" filename = /"=?" charset.toupper () "?b?" base64encode(filepath.substring("//") 1) "?=/"" enter enter; sendbufferstr = GetStream (FilePath) Enter Enter;}} SendBufferstr = "------ = _ nextpart_000_00d6_01c29593.aab31770 -" ENTER ENTER; SendBufferstr = Enter "." Enter; if (! Dialog) (SendBufferstr, "Error Letter Information")) Return False;

SendBufferstr = "quit" enter; if (! Dialog (SendBufferstr, "Disconnecting When Connection Error")) Return False;

ns.close (); tc.close (); return true;

///

/// Send an email method, all parameters are set by attribute. /// public bool send () {if (Recipient.count == 0) {Errmsg = "The recipient list cannot be empty"; Return False;}

IF (mailserver.trim () == "") {errmsg = "must specify SMTP server"; Return sentemail ();}

///

/// Send email method /// /// public bool send (string smtpserver) {maildomain = SMTPSERVER; RETURN Send ();

///

/// Send email method /// /// /// sender mail address /// sender Name /// Recipient address /// Recipient Name /// Whether html mail Mail topic /// mail body public bool send (String smtpserver, string from, string fromname, string to, string toname, bool html, string subject, string body) {MailDomain = smtpserver; from = from; fromName = fromname; AddRecipient (to); RecipientName = toname; Html = html; Subject = SUBJECT; BODY = body; return seund ();}}}

转载请注明原文地址:https://www.9cbs.com/read-102551.html

New Post(0)