国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > HTML > 正文

支持HTML格式和優先級設置的郵件發送類

2024-08-26 00:15:34
字體:
來源:轉載
供稿:網友
using system;
using system.text;
using system.io;
using system.net;
using system.net.sockets;

namespace blood.com.classlib
{         
    /// <summary>
    /// tcpclient派生類,用來進行smtp服務器的連接工作
    /// </summary>
    public class smtpclient : tcpclient
    {
        public bool isconnected()
        {
            return active;
        }

        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(readbuffer);
                return  returnvalue;
            }         
        }

        public bool doesstringcontainsmtpcode(string s,string smtpcode)
        {
            return(s.indexof(smtpcode,0,10)==-1)?false:true;
        }

    } //結束類

    /// <summary>
    /// 發送郵件類
    /// </summary>
    public class smtpmail
    {
        /// <summary>
        /// 錯誤反饋信息
        /// </summary>
        private string strerrmessage = null;

        /// <summary>
        /// smtp服務器反饋的信息
        /// </summary>
        private string strresponse;

        /// <summary>
        /// 構造函數
        /// </summary>
        public smtpmail()
        {
            strerrmessage = "";
            strresponse = "";
        }

        /// <summary>
        /// 取得錯誤反饋信息
        /// </summary>
        public string errormessage
        {
            get
            {
                return strerrmessage ;
            }
        }

        /// <summary>
        /// 取得smtp服務器反饋的信息
        /// </summary>
        public string serverresponse
        {
            get
            {
                return strresponse;
            }
        }

        /// <summary>
        /// 郵件發送優先級
        /// </summary>
        public enum prioritys
        {
            /// <summary>
            /// 最高級別
            /// </summary>
            high = 1,
            /// <summary>
            /// 默認級別
            /// </summary>
            normal = 3,
            /// <summary>
            /// 最低級別
            /// </summary>
            low = 5
        }

        public void sendmail(string smtphost,int port,string from,string displayfromname,string to,string displaytoname,prioritys priority,bool html,string base,string subject,string message)
        {
            try
            {
                string strresponsenumber;
                smtpclient smtpcmail = new smtpclient();
                smtpcmail.connect(smtphost,port);
                bool bolconnect = smtpcmail.isconnected();
                
                //判斷是否進行了連接
                if (!bolconnect)
                {
                    strerrmessage = "smtp服務器連接失敗...";
                    return;
                }
                
                //讀取反饋信息
                strresponsenumber = smtpcmail.getserverresponse();
                if (smtpcmail.doesstringcontainsmtpcode(strresponsenumber,"220"))
                {
                    this.strresponse += strresponsenumber;
                }
                else
                {
                    this.strerrmessage = "連接失敗" + strresponsenumber;
                    return ;
                }
                
                string[] strsendbuffer = 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");
                strsendbuffer[0] = strdata  ;
                
                strdata = "";
                strdata = string.concat("mail from: ","<" + from + ">");
                strdata = string.concat(strdata,"/r/n");
                strsendbuffer[1] = strdata;
                
                strdata = "";
                strdata = string.concat("rcpt to: ","<" + to + ">");
                strdata = string.concat(strdata,"/r/n");
                strsendbuffer[2] = strdata;
                
                strdata = "" ;
                strdata = string.concat("data","/r/n");
                strsendbuffer[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,subject);
                strdata = string.concat(strdata,"/r/n");
                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)
                {
                    strdata = string.concat(strdata,"content-transfer-encoding: text/html;" );
                }
                else
                {
                    strdata = string.concat(strdata,"content-transfer-encoding: 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");
                strsendbuffer[4] = strdata;
                
                strdata = "" ;
                strdata = string.concat(strdata,"quit/r/n");
                strsendbuffer[5] = strdata;
                
                int i = 0 ;
                
                while(i < strsendbuffer.length)
                {
                    smtpcmail.sendcommandtoserver(strsendbuffer[i]);
                    strresponsenumber = smtpcmail.getserverresponse();
                    
                    for(int j=0;j<strresponsecode.length;j++)
                    {
                        if (smtpcmail.doesstringcontainsmtpcode(strresponsenumber,strresponsecode[j]))
                        {
                            this.strresponse += strresponsenumber;
                            this.strresponse += "<br>";
                            break;
                        }
                        else
                        {
                            if(j==strresponsecode.length-1)
                            {
                                this.strerrmessage += strresponsenumber;
                                this.strerrmessage += strsendbuffer[i];
                                return;
                            }
                        }
                    }
                    
                    i++ ;
                } // 結束循環
            }
            catch(socketexception err)
            {
                this.strerrmessage += err.message + " " + err.stacktrace;
            }
            catch(exception e)
            {
                this.strerrmessage += e.message + " " + e.stacktrace;
            }
        } //結束郵件發送方法
         
    } // 結束類

} // 結束namespace
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 双江| 布拖县| 昌黎县| 仪陇县| 昔阳县| 沙洋县| 南宫市| 长治县| 花莲县| 威远县| 廊坊市| 和田市| 通江县| 泰和县| 临猗县| 石阡县| 湾仔区| 沙洋县| 靖远县| 云安县| 万安县| 普宁市| 手游| 车险| 聂拉木县| 苏尼特左旗| 仪征市| 滕州市| 壤塘县| 临颍县| 双鸭山市| 凌海市| 武山县| 分宜县| 伊吾县| 徐州市| 莱州市| 永吉县| 杭州市| 探索| 蒙自县|