首页 > 编程 > 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
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表