This is for [Original] Email Send System (C # Based on SMTP Certification) http://www.lionsky.net/mywebsite/Article/List.aspx?id=430's revision is available from the Internet I found the above address, I am very grateful to the original author's creative labor. The purpose of rewriting is to adapt to the requirements of service operation: 1. Adapt to multi-thread requirements, sending mail services can be run in the background, will be considered as exclusive resource with the SMTP server. 2 , Return the logo that is not successfully sent. 3, streamlined attributes, methods, information related to mail no longer act as an attribute, but as the parameters of Send; only an overloaded Send method is announced. In this class as the base class, the interface method of the notification service requires.
The following is the code rewritten: using System; using System.Text; using System.IO; using System.Net; using System.Net.Sockets; using System.Collections; using System.Threading; namespace Deep.SendEmail {#region AspNetPager Server control ///
/// summary> public string logs {rary m_logs.tostring ();}} ///
Such as "User: Pass@www.server.com" or "www.server.com" /// summary> /// Enter your username, password, mail server domain name, port number param> /// Return to the mail server domain name param> /// Return Username param> /// Return Password param> /// Return port number param> /// Return to whether SMTP verification param> /// < returns> returns> private bool SetMailDomain (string mailDomain, out string mailServer, out string mailServerUserName, out string password, out int mailserverport, out bool needSmtp) {bool isRight = false; // given initial MailServer = string as output .Empty; mailServerUserName = String.Empty; password = String.Empty; mailserverport = 25; needSmtp = false; mailServer = mailDomain.Trim (); int tempint; if (! mailServer = "") {tempint = mailServer.IndexOf ( " @ "); Isright = true; if (Tempint! = - 1) {string str = mailserver.substring (0, tempint); mailserverusername = str.substring (0, str .indexof (": ")); pass Word = str.substring (str.indexof (":") 1, str.Length-str.indexof ("::") - 1); Needsmtp =! (password == string.empty); mailserver = maildomain.substring (Tempint 1, Maildomain.Length-Tempint-1);} tempint = mailserver.indexof (":"); if (Tempint! = -1) {mailserverport = system.convert.toint32 (MailServer.Substring (Tempint 1 , MailServer.Length-Tempint-1)); MailServer = mailser.substring (0, TemPint);
}}}}} ///
Sendant Name param> /// Recipient address list param> /// Reconfite Name param> ///// Whether html mail param> /// Mail topic param> /// mail body param> / // attachment list Mail param> public bool Send (string smtpServer, string from, string fromName, string [] recipientADD, string recipientName, bool isHtml, string subject, priority priority, string body, String [] filepath) {// If the recipient is more transmitted at the same time, sending if (RecipientAdd.Length> RecipientMaxNum) {string [] RecipientAdd1 = new string [RecipientMaxNum]; string [] recipientADD2 = new string [recipientADD.Length - RecipientMaxNum]; for (int i = 0; i
) {M_logs.Append ( "must specify an SMTP server" ENTER); return false;} IList attachments = AddAttachment (filePath); // mail attachment list Hashtable recipients = AddRecipient (recipientADD); // recipient list if (recipients == NULL || Recipients.count == 0) {m_logs.Append ("must specify" Enter); return false;} if (Recipients.count> RecipientMaxNum) {m_logs.append ("One Send too many pieces " ENTER); return false;} bool isSuccessful = false; lock (this) {TcpClient tcpClientObject = null; // TcpClient objects, for connecting the server NetworkStream networkStreamObject = null; // NetworkStream objects DateTime dateTimeBegin = DateTime .Now; int useTime = 0; (! (useTime> RepeatTime || isSuccessful)) while {try {tcpClientObject = new TcpClient (mailServer, mailserverport); networkStreamObject = tcpClientObject.GetStream (); isSuccessful = SendEmail (networkStreamObject, needSmtp, mailServer , Username, Password, Recipients, from, fromname, RecipientName, Subject, Priority.toT7tring (), Attachments, Ishtml, body);} catch (Exception e) {m_logs.Append ( "Error:"!! e.Message ENTER);} finally {if (networkStreamObject = null) networkStreamObject.Close (); if (tcpClientObject = null) tcpClientObject .Close (); if {string n = Thread.CurrentThread.Name; Thread.Sleep (WaitTime);} (isSuccessful!). useTime = ((TimeSpan) (DateTime.Now - dateTimeBegin)) Seconds;}}} return isSuccessful } #Endregion #region private helper functions ///
Private void smtpcodeadd () {m_ERRCODEHT.ADD ("500", "Mailbox Address Error"); m_ERRCODEHT.ADD ("501", "Parameter Format Error"); m_ERRCODEHT.ADD ("502", "command is not implemented") M_ERRCODEHT.ADD ("503", "Server requires SMTP verification"); m_ERRCODEHT.ADD ("504", "command parameters are not implemented"); m_ERRCODEHT.ADD ("421", "service is not ready, turn off Transfer Channel" ); M_ERRCODEHT.ADD ("450", "requested by mail operations, mailboxes are not available (for example, mailbox busy)"); m_errcodeht.add ("550", "required mail operations are not completed, mailboxes are not available ( For example, mailbox is not found, or inseparable) "); m_ERRRCODEHT.ADD (" 451 "," Abandon the requirements of the requirements; error during processing "); M_ERRCODEHT.ADD (" 551 "," User Non-local, please try < Forward-path> "); m_ERRCODEHT.ADD (" 452 "," system storage is insufficient, the required operation is not executed "); m_ERRCODEHT.ADD (" 552 "," excess storage allocation, required operation is not executed "); M_ERRCODEHT.ADD ("553", "mail address is not available, the required operation is not executed (such as mailbox format error)"); m_errcodeht.add ("432", "require a password conversion"); m_ERRRCODEHT.ADD ("534 "" The "certification mechanism is too simple"); m_errcodeht.add ("538", "Current Request Certification Mechanism Requires"); M_ERRCODEHT.ADD ("454", "Temporary Certification Failure"); m_ERRCODEHT.Add ("530 "," Need to authenticate "); m_rightcodeht.add (" 220 "," service ready "); m_rightcodeht.add (" 250 "," required mail operation "); m_rightcodeht.add (" 251 "," users Local, will forward to "Str"> String param> private string base64encode (string str) {byte [] barray; barray = encoding.default.getbytes (str); return convert.tobase64string (barray);} /// < Summary> /// Decoding Base64 strings to normal strings /// //// String param> private string base64decode (string string string string string Str) {BYTE [] Barray; barray = convert.FromBase64String (STR); return encoding.default.getstring (barray);} /// } Return true;} /// /// summary> private bool dialog (String [] str, networkStream _networkstream) {for (int i = 0; i (! Dialog (SendBuffer, _NetworkStream))} if return false; SendBufferstr = "DATA" ENTER; if return false; SendBufferstrBuilder.Append ( "From:" (Dialog (SendBufferstr, _NetworkStream)!) FromName "<" From "> enter); sendbufferstrbuilder.Append (" to: =? " charset.toupper () "? b? " base64encode (recipientname) "? = " " <" Recipients [0] "> Enter); sendBufferstrBuilder.Append (" cc: "); for (int i = 0; i Boundary = / "======= (ISHTML?" 001_dragon520636771063 _ ":" 001_dragon303406132050 _ ") " ==== / " ENTER ENTER);}}}}}}} {i (attachments! = null && attachments.Count == 0) {SendBufferstrBuilder.Append ( "content-Type: multipart / alternative;" ENTER); // content format and separators SendBufferstrBuilder.Append ( "boundary = /" ===== 003_Dragon520636771063_ = ==== / "" Enter ENTER); sendBufferstrBuilder.Append ("this is a multi-part message in mime format." ENTER ENTER);} else {sendbufferstrbuilder.Append ("this is a multi-part message in MIME format " ENTER ENTER); SendBufferstrBuilder.Append (" - ===== 001_Dragon520636771063 _ ===== " ENTER); SendBufferstrBuilder.Append (" Content-Type:. multipart / alternative; " ENTER); // Content format and separator sendbuffrstrbuilder.Append ("boundary = /" ===== 003_dragon520636771063 _ ===== / "" "" enter enter);} sendbufferstrbuilder.Append ("- === == 003_dragon520636771063 _ ===== " enter); sendBufferstrBuilder.Append (" Content-Type: Text / Plain; " ENTER); sendbuff ErstrBuilder.Append ((Charset == ")? (" Charset = / "ISO-8859-1): (" Charset = / " " / "")) ENTER); sendbufferstrbuilder.append ("Content-Transfer-Encoding: Base64" Enter Enter); SendBufferstrBuilder.Append (Base64Encode ( "message content to HTML format, select the HTML view mode") ENTER ENTER); SendBufferstrBuilder.Append ( "- ===== 003_Dragon520636771063 _ =====" ENTER); SendBufferstrBuilder .Append ("Content-Type: Text / HTML;" ENTER); sendbufferstrbuilder.Append ((Charset == ")? (" Charset = / "ISO-8859-1) :(" "" " / "" "" "")) Enter); sendBufferstrBuilder.Append ("Content-Transfer-Encoding: Base64" Enter Enter); sendbufferstrbuilder.Append (Base64Encode (Body) ENTER ENTER); SendBufferstrBuilder.Append ( "- ===== 003_Dragon520636771063 _ ===== -" ENTER);} else {if (attachments = null && attachments.Count = 0) {SendBufferstrBuilder.Append (!! "- ===== 00132050 _ =====" ENTER);} sendbufferstrbuilder.Append ("Content-type: text / plain;" enter); sendbufferstrbuilder.Append ((charset == ") ("Charset = /" ISO-8859-1 / ""): ("Charset = /" "/" ")) Enter; sendbufferstrbuilder.Append (" Content-Transfer- EN coding: base64 " ENTER ENTER); SendBufferstrBuilder.Append (Base64Encode (body) ENTER);} // SendBufferstr =" Content-Transfer-Encoding:! base64 " ENTER; if (attachments = null && attachments.Count ! = 0) {for (int i = 0; i ) {String filepath = (string) attachments [i]; sendbufferstrbuilder.Append ("- =====" (Ishtml? "001_dragon520636771063 _": "001_dragon303406132050_") "=====" enter); ENTER); // SendBufferstr = "Content-Type: application / octet-stream" ENTER; SendBufferstrBuilder.Append? ( "Content-Type: text / plain;" ENTER); SendBufferstrBuilder.Append ( "name = /" = " Charset.toupper () "? B?" Base64encode (filepath.substring (filepath.lastindexof ("//") 1)) "? = /" " Enter); sendbufferstrbuilder.Append (" Content-Transfer -Encoding: Base64 " ENTER); SendBufferstrBuilder.Append (" Content-Disposition: attachment; " ENTER); sendBufferstrBuilder.Append (" filename = / "=?" Charset.toupper () "? B?" Base64Encode (filepath.Substring (filepath.LastIndexOf ( "//") 1)) " ENTER ENTER)" = /? "; SendBufferstrBuilder.Append (GetStream (filepath) ENTER ENTER);} SendBufferstrBuilder.Append ("- =====" (iSHTML? "001_dragon520636771063 _": "001_dragon303406132050_") "===== -" Enter Enter);} SendBufferstrBuilder.Append ( "." ENTER ENTER); SendBufferstr = SendBufferstrBuilder.ToString (); if (! Dialog (SendBufferstr, _NetworkStream)) return false; SendBufferstr = "QUIT" ENTER; if (Dialog (SendBufferstr, _NetworkStream)!) Return False; Return True;} #endregion #region / * ///