示例#1
0
        public static void sendMail()
        {
            try {
                while (SmtpMailerThreadPool.ThreadPool == SmtpMailerThreadPool.ThreadPoolMax)
                {
                    Thread.Sleep(10);
                }

                SmtpMailerThread sm = new SmtpMailerThread();
                sm.MAILTO       = MAILTO;
                sm.MAILFROM     = MAILFROM;
                sm.SUBJECT      = SUBJECT;
                sm.BODYTEXT     = BODYTEXT;
                sm.HTMLBODYTYPE = HTMLBODYTYPE;
                sm.SMTPSERVER   = SMTPSERVER;
                Thread t = new Thread(new ThreadStart(sm.send));
                t.Name = "SmtpMailerThread2";
                SmtpMailerThreadPool.incrementThreadPool();
                t.Start();
            } catch (Exception e) {
                throw(e);
            }
        }
        public void send()
        {
            try {
                // ATTEMPT TO CONNECT TO SERVER
                IPHostEntry ipHost = Dns.Resolve(SMTPSERVER);
                IPEndPoint  endPt  = new IPEndPoint(ipHost.AddressList[0], 25);
                Socket      s      = new Socket(endPt.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, RECEIVE_TIMEOUT);
                s.Connect(endPt);
                if (!(checkResponse(s, SMTPResponse.CONNECT_SUCCESS)))
                {
                    s.Close();
                }

                // INITIAL SMTP COMMANDS
                sendData(s, string.Format("HELO {0}\r\n", Dns.GetHostName()));
                if (!(checkResponse(s, SMTPResponse.GENERIC_SUCCESS)))
                {
                    s.Close();
                }
                sendData(s, string.Format("MAIL FROM: <{0}>\r\n", MAILFROM));
                if (!(checkResponse(s, SMTPResponse.GENERIC_SUCCESS)))
                {
                    s.Close();
                }
                sendData(s, string.Format("RCPT TO: <{0}>\r\n", MAILTO));
                if (!(checkResponse(s, SMTPResponse.GENERIC_SUCCESS)))
                {
                    s.Close();
                }

                // BUILDING BODY OF EMAIL
                StringBuilder Header = new StringBuilder();
                Header.Append("From: ");
                Header.Append(MAILFROM);
                Header.Append("\r\n");
                Header.Append("To: ");
                Header.Append(MAILTO);
                Header.Append("\r\n");
                Header.Append("Subject: ");
                Header.Append(SUBJECT);
                Header.Append("\r\n");

                // CHECK FOR HTML BODY
                if (HTMLBODYTYPE)
                {
                    Header.Append("Mime-Version: 1.0\r\n");
                    Header.Append("Content-Type: text/html; charset=\"ISO-8859-1\"\r\n");
                }

                // IF BODY DOESN'T END WITH CR LF - THEN ADD IT
                if (!BODYTEXT.EndsWith("\r\n"))
                {
                    BODYTEXT += "\r\n";
                }

                // DATA COMMAND & BODY
                sendData(s, ("DATA\r\n"));
                if (!(checkResponse(s, SMTPResponse.DATA_SUCCESS)))
                {
                    s.Close();
                }

                Header.Append("\r\n");
                Header.Append(BODYTEXT);
                Header.Append(".\r\n");
                Header.Append("\r\n");
                Header.Append("\r\n");
                sendData(s, Header.ToString());
                s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, PAYLOAD_RECEIVE_TIMEOUT);
                if (!(checkResponse(s, SMTPResponse.GENERIC_SUCCESS)))
                {
                    s.Close();
                }

                // QUIT
                sendData(s, "QUIT\r\n");
                s.Close();

                myCounter.Increment();
            } catch (Exception e) {
                myCounter.Close();
                throw(e);
            } finally {
                try {
                    SmtpMailerThreadPool.decrementThreadPool();
                    myCounter.Close();
                } catch (Exception e) {
                    throw(e);
                }
            }
        }