程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C#寫的支持SMTP驗證的發送郵件組件(2)

C#寫的支持SMTP驗證的發送郵件組件(2)

編輯:關於C語言
        /// <summary>
        /// 接收SMTP服務器回應
        /// </summary>
        private string RecvResponse()
        {
            int StreamSize;
            string ReturnValue = "";
            byte[]  ReadBuffer = new byte[1024] ;
            try
            {
                StreamSize=ns.Read(ReadBuffer,0,ReadBuffer.Length);
            }
            catch
            {
                errmsg="網絡連接錯誤";
                return "false";
            }

            if (StreamSize==0)
            {
                return ReturnValue ;
            }
            else
            {
                ReturnValue = Encoding.Default.GetString(ReadBuffer).Substring(0,StreamSize);
                logs+=ReturnValue;
                return  ReturnValue;
            }
        }


        /// <summary>
        /// 與服務器交互,發送一條命令並接收回應。
        /// </summary>
        /// <param name="Command">一個要發送的命令</param>
        /// <param name="errstr">如果錯誤,要反饋的信息</param>
        private bool Dialog(string Command,string errstr)
        {
            if(Command==null||Command.Trim()=="")
            {
                return true;
            }
            if(SendCommand(Command))
            {
                string RR=RecvResponse();
                if(RR=="false")
                {
                    return false;
                }
                string RRCode=RR.Substring(0,3);
                if(RightCodeHT[RRCode]!=null)
                {
                    return true;
                }
                else
                {
                    errmsg+=(RRCode+ErrCodeHT[RRCode].ToString());
                    errmsg+=enter;
                    errmsg+=errstr;
                    return false;
                }
            }
            else
            {
                return false;
            }

        }


        /// <summary>
        /// 與服務器交互,發送一組命令並接收回應。
        /// </summary>

        private bool Dialog(string[] Command,string errstr)
        {
            for(int i=0;i<Command.Length;i++)
            {
                if(!Dialog(Command[i],""))
                {
                    errmsg+=enter;
                    errmsg+=errstr;
                    return false;
                }
            }

            return true;
        }



        private bool SendEmail()
        {
            //連接網絡
            try
            {
                tc=new TcpClIEnt(mailserver,mailserverport);
            }
            catch(Exception e)
            {
                errmsg=e.ToString();
                return false;
            }

            ns = tc.GetStream();
            SMTPCodeAdd();

            //驗證網絡連接是否正確
            if(RightCodeHT[RecvResponse().Substring(0,3)]==null)
            {
                errmsg="網絡連接失敗";
                return false;
            }


            string[] SendBuffer;
            string SendBufferstr;

            //進行SMTP驗證
            if(ESmtp)
            {
                SendBuffer=new String[4];
                SendBuffer[0]="EHLO " + mailserver + enter;
                SendBuffer[1]="AUTH LOGIN" + enter;
                SendBuffer[2]=Base64Encode(username) + enter;
                SendBuffer[3]=Base64Encode(passWord) + enter;
                if(!Dialog(SendBuffer,"SMTP服務器驗證失敗,請核對用戶名和密碼。"))
                    return false;
            }
            else
            {
                SendBufferstr="HELO " + mailserver + enter;
                if(!Dialog(SendBufferstr,""))
                    return false;
            }

            //
            SendBufferstr="MAIL FROM:<" + From + ">" + enter;
            if(!Dialog(SendBufferstr,"發件人地址錯誤"))
                return false;

            //
            SendBuffer=new string[10];
            for(int i=0;i<RecipIEnt.Count;i++)
            {

                SendBuffer[i]="RCPT TO:<" + RecipIEnt[i].ToString() +">" + enter;

            }
            if(!Dialog(SendBuffer,"收件人地址有誤"))
                return false;

            SendBuffer=new string[10];
            for(int i=0;i<RecipIEntBCC.Count;i++)
            {

                SendBuffer[i]="RCPT TO:<" + RecipIEntBCC[i].ToString() +">" + enter;

            }
            if(!Dialog(SendBuffer,"密件收件人地址有誤"))
                return false;

            SendBufferstr="DATA" + enter;
            if(!Dialog(SendBufferstr,""))
                return false;

            SendBufferstr="From:" + FromName + "<" + From +">" +enter;
            SendBufferstr+="To:" + RecipientName + "<" + RecipIEnt[0] +">" +enter;
            SendBufferstr+="CC:";
            for(int i=1;i<RecipIEnt.Count;i++)
            {
                SendBufferstr+=Recipient[i].ToString() + "<" + RecipIEnt[i].ToString() +">,";
            }
            SendBufferstr+=enter;


            if(ReplyTo.Trim()!="")
            {
                SendBufferstr+="Reply-To: " + ReplyTo + enter;
            }

            if(Charset=="")
            {
                SendBufferstr+="Subject:" + Subject + enter;
            }
            else
            {
                SendBufferstr+="Subject:" + "=?" + Charset.ToUpper() + "?B?" + Base64Encode(Subject) +"?=" +enter;
            }

            SendBufferstr+="MIME-Version: 1.0" + 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+="X-Priority:" + priority + enter;
            SendBufferstr+="X-MSMail-Priority:" + priority + enter;
            SendBufferstr+="Importance:" + priority + enter;
            SendBufferstr+="X-Mailer: Huolx.Pubclass" + enter;
            SendBufferstr+= enter + enter;
            SendBufferstr+= Base64Encode(Body) + enter;
            SendBufferstr+=enter + "." + enter;

            if(!Dialog(SendBufferstr,"錯誤信件信息"))
                return false;


            SendBufferstr="QUIT" + enter;
            if(!Dialog(SendBufferstr,"斷開連接時錯誤"))
                return false;


            ns.Close();
            tc.Close();
            return true;
        }


        /// <summary>
        /// 發送郵件方法,所有參數均通過屬性設置。
        /// </summary>
        public bool Send()
        {
            if(RecipIEnt.Count==0)
            {
                errmsg="收件人列表不能為空";
                return false;
            }

            if(RecipIEntName=="")
                RecipientName=RecipIEnt[0].ToString();

            if(mailserver.Trim()=="")
            {
                errmsg="必須指定SMTP服務器";
                return false;
            }

            return SendEmail();
            
        }


        /// <summary>
        /// 發送郵件方法
        /// </summary>
        /// <param name="smtpserver">smtp服務器信息,如"username:[email protected]:25",也可去掉部分次要信息,如"www.smtpserver.com"</param>
        public bool Send(string smtpserver)
        {
            
            MailDomain=smtpserver;
            return Send();
        }


        /// <summary>
        /// 發送郵件方法
        /// </summary>
        /// <param name="smtpserver">smtp服務器信息,如"username:[email protected]:25",也可去掉部分次要信息,如"www.smtpserver.com"</param>
        /// <param name="from">發件人mail地址</param>
        /// <param name="fromname">發件人姓名</param>
        /// <param name="replyto">回復郵件地址</param>
        /// <param name="to">收件人地址</param>
        /// <param name="toname">收件人姓名</param>
        /// <param name="html">是否Html郵件</param>
        /// <param name="subject">郵件主題</param>
        /// <param name="body">郵件正文</param>
        public bool Send(string smtpserver,string from,string fromname,string replyto,string to,string toname,bool Html,string subject,string body)
        {
            MailDomain=smtpserver;
            From=from;
            FromName=fromname;
            ReplyTo=replyto;
            AddRecipIEnt(to);
            RecipIEntName=toname;
            Html=Html;
            Subject=subject;
            Body=body;
            return Send();
            
        }
    }
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved