首页 > 开发 > 综合 > 正文

C#写的支持SMTP验证的发送邮件组件(2)

2024-07-21 02:24:26
字体:
来源:转载
供稿:网友
        /// <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();
            
        }
    }
}
 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表