首页 > 开发 > 综合 > 正文

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

2024-07-21 02:24:20
字体:
来源:转载
供稿:网友

这是我学c#后写的第一个比较拿得出手的东西,肯定有很多错误之处,还望各位高手指正。


写它的原因就是.net自带的smtpmail不支持smtp认证(说句题外话,我真很奇怪, 从asp到asp.net,m$的邮件组件老是慢半拍,asp中的cdonts难得能有正常使用的, 到了.net时代,smtp服务器基本都得验证才能用,.net的smtpmail又不支持验证, 真是~~~),我费了好几天功夫才弄明白怎样把原来的jmail怎么弄到.net下用, 可用起来还是比较麻烦,正好买了本《c#网络程序设计》,作为实践项目,也是为了 让自己以后轻松一点,我写了这个东西。


程序架构可能有些乱,都是原来写asp时留下的毛病,如果那位能帮我弄得整齐一点,小弟感激不尽。


欢迎大家提出修改建议,或者自行修改,最好能把修改稿mail给我一份,让我也学习学习 ^_^。

完整的源代码和编译好的dll可以从下面的url下载。
http://huolx.t2008.com/class.asp?classlevel=3&classname=.net技术&l2classname=esmtpmail&articleid=-486856519

下面开始贴源代码。


using system;
using system.text;
using system.io;
using system.net;
using system.net.sockets;
using system.collections;

namespace huolx.pubclass
{
    /// <summary>
    /// <para>支持smtp服务器认证的邮件发送类</para>
    /// <para></para>
    /// <para>ver 1.0b</para>
    /// <para>属性和方法设置类似于jmail。</para>
    /// <para></para>
    /// <para>huolx([email protected])版权所有,转载请保留。</para>
    /// </summary>
    /// <remarks>
    /// <para>这是我学c#后写的第一个比较拿得出手的东西,肯定有很多错误之处,还望各位高手指正。</para>
    /// <para></para>
    /// <para>写它的原因就是.net自带的smtpmail不支持smtp认证(说句题外话,我真很奇怪,
    /// 从asp到asp.net,m$的邮件组件老是慢半拍,asp中的cdonts难得能有正常使用的,
    /// 到了.net时代,smtp服务器基本都得验证才能用,.net的smtpmail又不支持验证,
    /// 真是~~~),我费了好几天功夫才弄明白怎样把原来的jmail怎么弄到.net下用,
    /// 可用起来还是比较麻烦,正好买了本《c#网络程序设计》,作为实践项目,也是为了
    /// 让自己以后轻松一点,我写了这个东西。</para>
    /// <para></para>
    /// <para>程序架构可能有些乱,都是原来写asp时留下的毛病,如果那位能帮我弄得整齐一点,小弟感激不尽。</para>
    /// <para></para>
    /// <para>欢迎大家提出修改建议,或者自行修改,最好能把修改稿mail给我一份,让我也学习学习 ^_^。</para>
    /// </remarks>
    public class esmtpmail
    {

        /// <summary>
        /// 设定语言代码,默认设定为gb2312,如不需要可设置为""
        /// </summary>
        public string charset="gb2312";

        /// <summary>
        /// 发件人地址
        /// </summary>
        public string from="";

        /// <summary>
        /// 发件人姓名
        /// </summary>
        public string fromname="";

        /// <summary>
        /// 回复邮件地址
        /// </summary>
        public string replyto="";

        /// <summary>
        /// 邮件服务器域名
        /// </summary>    
        private string mailserver="";

        /// <summary>
        /// 邮件服务器域名和验证信息
        /// 形如:"user:[email protected]:25",也可省略次要信息。如"user:[email protected]"或"www.server.com"
        /// </summary>    
        public string maildomain
        {
            set
            {
                string maidomain=value.trim();
                int tempint;

                if(maidomain!="")
                {
                    tempint=maidomain.indexof("@");
                    if(tempint!=-1)
                    {
                        string up=maidomain.substring(0,tempint);
                        mailserverusername=up.substring(0,up.indexof(":"));
                        mailserverpassword=up.substring(up.indexof(":")+1,up.length-up.indexof(":")-1);
                        string ms=maidomain.substring(tempint+1,maidomain.length-tempint-1);
                        tempint=ms.indexof(":");
                        if(tempint!=-1)
                        {
                            mailserver=ms.substring(0,tempint);
                            mailserverport=system.convert.toint32(ms.substring(tempint+1,ms.length-tempint-1));
                        }
                    }
                    else
                    {
                        tempint=maidomain.indexof(":");
                        if(tempint!=-1)
                        {
                            mailserver=maidomain.substring(0,tempint);
                            mailserverport=system.convert.toint32(maidomain.substring(tempint+1,maidomain.length-tempint-1));
                        }
                        else
                        {
                            mailserver=maidomain;

                        }

                    }
                }

            }
        }

        /// <summary>
        /// 邮件服务器端口号
        /// </summary>    
        private int mailserverport=25;

        /// <summary>
        /// 邮件服务器端口号
        /// </summary>    
        public int maildomainport
        {
            set
            {
                mailserverport=value;
            }
        }


        /// <summary>
        /// 是否需要smtp验证
        /// </summary>        
        private bool esmtp=false;

        /// <summary>
        /// smtp认证时使用的用户名
        /// </summary>
        private string username="";

        /// <summary>
        /// smtp认证时使用的用户名
        /// </summary>
        public string mailserverusername
        {
            set
            {
                if(value.trim()!="")
                {
                    username=value.trim();
                    esmtp=true;
                }
                else
                {
                    username="";
                    esmtp=false;
                }
            }
        }

        /// <summary>
        /// smtp认证时使用的密码
        /// </summary>
        private string password="";

        /// <summary>
        /// smtp认证时使用的密码
        /// </summary>
        public string mailserverpassword
        {
            set
            {
                password=value;
            }
        }    

        /// <summary>
        /// 邮件主题
        /// </summary>        
        public string subject="";
        
        /// <summary>
        /// 是否html邮件
        /// </summary>        
        public bool html=false;

        /// <summary>
        /// 邮件正文
        /// </summary>        
        public string body="";

        /// <summary>
        /// 收件人列表
        /// </summary>
        private hashtable recipient=new hashtable();

        /// <summary>
        /// 密送收件人列表
        /// </summary>
        private hashtable recipientbcc=new hashtable();

        /// <summary>
        /// 邮件发送优先级,可设置为"high","normal","low"或"1","3","5"
        /// </summary>
        private string priority="normal";

        /// <summary>
        /// 邮件发送优先级,可设置为"high","normal","low"或"1","3","5"
        /// </summary>
        public string priority
        {
            set
            {
                switch(value.tolower())
                {
                    case "high":
                        priority="high";
                        break;

                    case "1":
                        priority="high";
                        break;

                    case "normal":
                        priority="normal";
                        break;

                    case "3":
                        priority="normal";
                        break;

                    case "low":
                        priority="low";
                        break;

                    case "5":
                        priority="low";
                        break;

                    default:
                        priority="normal";
                        break;
                }
            }
        }


        /// <summary>
        /// 错误消息反馈
        /// </summary>
        private string errmsg;

        /// <summary>
        /// 错误消息反馈
        /// </summary>        
        public string errormessage
        {
            get
            {
                return errmsg;
            }
        }



        /// <summary>
        /// 服务器交互记录
        /// </summary>
        private string logs="";

        /// <summary>
        /// 服务器交互记录,如发现本组件不能使用的smtp服务器,请将出错时的logs发给我([email protected]),我将尽快查明原因。
        /// </summary>
        public string logs
        {
            get
            {
                return logs;
            }
        }
        

        private string enter="/r/n";
        /// <summary>
        /// tcpclient对象,用于连接服务器
        /// </summary>    
        private tcpclient tc;

        /// <summary>
        /// networkstream对象
        /// </summary>    
        private networkstream ns;

        /// <summary>
        /// smtp错误代码哈希表
        /// </summary>
        private hashtable errcodeht = new hashtable();

        /// <summary>
        /// smtp正确代码哈希表
        /// </summary>
        private hashtable rightcodeht = new hashtable();

        /// <summary>
        /// smtp回应代码哈希表
        /// </summary>
        private void smtpcodeadd()
        {
            errcodeht.add("500","邮箱地址错误");
            errcodeht.add("501","参数格式错误");
            errcodeht.add("502","命令不可实现");
            errcodeht.add("503","服务器需要smtp验证");
            errcodeht.add("504","命令参数不可实现");
            errcodeht.add("421","服务未就绪,关闭传输信道");
            errcodeht.add("450","要求的邮件操作未完成,邮箱不可用(例如,邮箱忙)");
            errcodeht.add("550","要求的邮件操作未完成,邮箱不可用(例如,邮箱未找到,或不可访问)");
            errcodeht.add("451","放弃要求的操作;处理过程中出错");
            errcodeht.add("551","用户非本地,请尝试<forward-path>");
            errcodeht.add("452","系统存储不足,要求的操作未执行");
            errcodeht.add("552","过量的存储分配,要求的操作未执行");
            errcodeht.add("553","邮箱名不可用,要求的操作未执行(例如邮箱格式错误)");
            errcodeht.add("432","需要一个密码转换");
            errcodeht.add("534","认证机制过于简单");
            errcodeht.add("538","当前请求的认证机制需要加密");
            errcodeht.add("454","临时认证失败");
            errcodeht.add("530","需要认证");

            rightcodeht.add("220","服务就绪");
            rightcodeht.add("250","要求的邮件操作完成");
            rightcodeht.add("251","用户非本地,将转发向<forward-path>");
            rightcodeht.add("354","开始邮件输入,以<crlf>.<crlf>结束");
            rightcodeht.add("221","服务关闭传输信道");
            rightcodeht.add("334","服务器响应验证base64字符串");
            rightcodeht.add("235","验证成功");
        }


        /// <summary>
        /// 将字符串编码为base64字符串
        /// </summary>
        /// <param name="estr">要编码的字符串</param>
        private string base64encode(string estr)
        {
            byte[] barray;
            barray=encoding.default.getbytes(estr);
            return convert.tobase64string(barray);
        }


        /// <summary>
        /// 将base64字符串解码为普通字符串
        /// </summary>
        /// <param name="dstr">要解码的字符串</param>
        private string base64decode(string dstr)
        {
            byte[] barray;
            barray=convert.frombase64string(dstr);
            return encoding.default.getstring(barray);
        }


        public esmtpmail()
        {
        }

        ~esmtpmail()
        {
        }


        /// <summary>
        /// 收件人姓名
        /// </summary>    
        public string recipientname="";


        private int recipientnum=0;//收件人数量
        private int recipientbccnum=0;//密件收件人数量

        /// <summary>
        /// 添加一个收件人
        /// </summary>    
        /// <param name="str">收件人地址</param>
        public bool addrecipient(string str)
        {
            str=str.trim();
            if(str==null||str==""||str.indexof("@")==-1)
                return true;
            if(recipientnum<10)
            {
                recipient.add(recipientnum,str);
                recipientnum++;
                return true;
            }
            else
            {
                errmsg+="收件人过多";
                return false;
            }
        }


        /// <summary>
        /// 添加一组收件人(不超过10个),参数为字符串数组
        /// </summary>
        /// <param name="str">保存有收件人地址的字符串数组(不超过10个)</param>    
        public bool addrecipient(string[] str)
        {
            for(int i=0;i<str.length;i++)
            {
                if(!addrecipient(str[i]))
                {
                    return false;
                }
            }
            return true;
        }


        /// <summary>
        /// 添加一个密件收件人
        /// </summary>
        /// <param name="str">收件人地址</param>
        public bool addrecipientbcc(string str)
        {
            if(str==null||str.trim()=="")
                return true;
            if(recipientbccnum<10)
            {
                recipientbcc.add(recipientbccnum,str);
                recipientbccnum++;
                return true;
            }
            else
            {
                errmsg+="收件人过多";
                return false;
            }
        }


        /// <summary>
        /// 添加一组密件收件人(不超过10个),参数为字符串数组
        /// </summary>    
        /// <param name="str">保存有收件人地址的字符串数组(不超过10个)</param>
        public bool addrecipientbcc(string[] str)
        {
            for(int i=0;i<str.length;i++)
            {
                if(!addrecipientbcc(str[i]))
                {
                    return false;
                }
            }
            return true;
        }


        /// <summary>
        /// 发送smtp命令
        /// </summary>    
        private bool sendcommand(string command)
        {
            byte[]  writebuffer;
            if(command==null||command.trim()=="")
            {
                return true;
            }
            logs+=command;
            writebuffer = encoding.default.getbytes(command);
            try
            {
                ns.write(writebuffer,0,writebuffer.length);
            }
            catch
            {
                errmsg="网络连接错误";
                return false;
            }
            return true;
        }

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