Example #1
0
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="smtpHost">SMTP服务器</param>
        /// <param name="port">SMTP服务器端口</param>
        /// <param name="from">邮件发送者</param>
        /// <param name="displayFromName">显示的发送者名称</param>
        /// <param name="authentication">是否进行认证</param>
        /// <param name="userName">认证用户名</param>
        /// <param name="password">认证密码</param>
        /// <param name="To">邮件接收者</param>
        /// <param name="displayToName">显示的接收者名称</param>
        /// <param name="priority">优先级</param>
        /// <param name="html">是否为HTML</param>
        /// <param name="Base">URL</param>
        /// <param name="subject">邮件主题</param>
        /// <param name="message">邮件内容</param>
        /// <param name="errmsg">错误信息 </param>
        public int SendMail(string smtpHost, int port, string @from, string displayFromName, bool authentication,
                            string userName, string password, string To, string displayToName, Priority priority,
                            bool html, string Base, string subject, string message, out string errmsg)
        {
            try
            {
                @from = "<" + @from + ">";
                To = "<" + To + ">";
                ////if (To.IndexOf(";") > -1)
                ////    To = "<" + To.Replace(";", ">,<") + ">";
                ////else
                ////    To = "<" + To + ">";


                var Name = new[]
                               {
                                   "服务器地址", "端口", "发信邮箱", "发送者名称", "是否认证(Smtp登录)", "认证用户名", "认证密码", "邮件接受者", "接受者姓名",
                                   "优先级"
                                   , "是否HTML", "相对连接地址(在有图片或连接时)", "主题", "正文"
                               };

                var NameParameter = new[]
                                        {
                                            smtpHost, port.ToString(), @from, displayFromName, authentication.ToString(),
                                            userName, password, To, displayToName, priority.ToString(), html.ToString(),
                                            Base, subject, message
                                        };

                //以上为发送邮件失败时记录到网站根目录的文本文件中

                string strResponseNumber; //smtp服务器返回的信息

                var smtpcMail = new SMTPClient(); //实例化连接类

                smtpcMail.Connect(smtpHost, port); //连接SmtpHost服务器,端口Port

                bool boolConnect = smtpcMail.isConnected(); //返回连接是否成功

                //判断是否进行了连接

                if (!boolConnect)
                {
                    smtpcMail.Close();

                    //TextLog.ErrorLog(_Server, "ErrorText.txt", Name,  string.Join(",", NameParameter), "未能取得连接", "");


                    Dev.Log.Loger.Error(string.Join(",", NameParameter) + "=>未能取得连接");

                    errmsg = "smtpcMail.Connect error:未能取得连接";
                    return -1;
                }

                strResponseNumber = smtpcMail.GetServerResponse(); //得到服务器返回的信息

                if (!(smtpcMail.DoesStringContainSMTPCode(strResponseNumber, "220"))) //包含220表示成功连接
                {
                    smtpcMail.Close();

                    Dev.Log.Loger.Error(string.Join(",", NameParameter) + "=>请求连接失败");
                   
                    //TextLog.ErrorLog(_Server, "ErrorText.txt", Name, NameParameter, "请求连接失败",
                    //                 FormatString(strResponseNumber).Trim());
                    errmsg = "smtpcMail.GetServerResponse error:" + FormatString(strResponseNumber).Trim();
                    return -1;
                }

                smtpcMail.SendCommandToServer("HELO kaidongmei\r\n");
                Thread.Sleep(50);

                strResponseNumber = smtpcMail.GetServerResponse();

                if (!(smtpcMail.DoesStringContainSMTPCode(strResponseNumber, "250")))
                {
                    smtpcMail.Close();

                    //TextLog.ErrorLog(_Server, "ErrorText.txt", Name, NameParameter, "与服务器连接初期(打招呼出错)",
                    //                 FormatString(strResponseNumber).Trim());

                    Dev.Log.Loger.Error(string.Join(",", NameParameter) + "=>与服务器连接初期(打招呼出错)");
                  
                    errmsg = FormatString(strResponseNumber).Trim();
                    return 1;
                }

                //第一步结束

                if (authentication) //是否认证
                {
                    //与服务器通讯第二步:请求登录

                    smtpcMail.SendCommandToServer("AUTH LOGIN\r\n");
                    Thread.Sleep(50);
                    strResponseNumber = smtpcMail.GetServerResponse();

                    if (!(smtpcMail.DoesStringContainSMTPCode(strResponseNumber, "334")))
                    {
                        smtpcMail.Close();

                        //TextLog.ErrorLog(_Server, "ErrorText.txt", Name, NameParameter, "请求登录出现错误",
                        //                 FormatString(strResponseNumber).Trim());

                        Dev.Log.Loger.Error(string.Join(",", NameParameter) + "=>请求登录出现错误");
                  
                        errmsg = FormatString(strResponseNumber).Trim();
                        return 2;
                    }

                    //与服务器通讯第三步:输入用户帐号

                    smtpcMail.SendCommandToServer(Encode(userName) + "\r\n");
                    Thread.Sleep(50);
                    strResponseNumber = smtpcMail.GetServerResponse();

                    if (!(smtpcMail.DoesStringContainSMTPCode(strResponseNumber, "334")))
                    {
                        smtpcMail.Close();

                        //TextLog.ErrorLog(_Server, "ErrorText.txt", Name, NameParameter, "输入用户名称出错",
                        //                 FormatString(strResponseNumber).Trim());


                        Dev.Log.Loger.Error(string.Join(",", NameParameter) + "=>输入用户名称出错");

                        errmsg = FormatString(strResponseNumber).Trim();
                        return 3;
                    }

                    //第三步结束

                    //与服务器通讯第四步:输入用户密码

                    smtpcMail.SendCommandToServer(Encode(password) + "\r\n");
                    Thread.Sleep(50);
                    strResponseNumber = smtpcMail.GetServerResponse();

                    if (!(smtpcMail.DoesStringContainSMTPCode(strResponseNumber, "235")))
                    {
                        smtpcMail.Close();

                        //TextLog.ErrorLog(_Server, "ErrorText.txt", Name, NameParameter, "输入用户密码出错",
                        //                 FormatString(strResponseNumber).Trim());

                        Dev.Log.Loger.Error(string.Join(",", NameParameter) + "=>输入用户密码出错");

                        errmsg = FormatString(strResponseNumber).Trim();
                        return 4;
                    }

                    //第四步结束
                }

                //与服务器通讯第五步:输入发送邮件的信箱地址

                smtpcMail.SendCommandToServer("MAIL FROM: " + @from + /* +  " AUTH = " + From*/ "\r\n");
                Thread.Sleep(50);
                strResponseNumber = smtpcMail.GetServerResponse();

                if (!(smtpcMail.DoesStringContainSMTPCode(strResponseNumber, "250")))
                {
                    smtpcMail.Close();

                    //TextLog.ErrorLog(_Server, "ErrorText.txt", Name, NameParameter, "输入发信邮箱出错",
                    //                 FormatString(strResponseNumber).Trim());
                    Dev.Log.Loger.Error(string.Join(",", NameParameter) + "=>输入发信邮箱出错");


                    errmsg = FormatString(strResponseNumber).Trim();
                    return 5;
                }

                //第五步结束

                //与服务器通讯第六步:输入接收邮件的信箱地址

                smtpcMail.SendCommandToServer("RCPT TO: " + To + "\r\n");
                Thread.Sleep(50);
                strResponseNumber = smtpcMail.GetServerResponse();

                if (!(smtpcMail.DoesStringContainSMTPCode(strResponseNumber, "250")))
                {
                    smtpcMail.Close();

                    //TextLog.ErrorLog(_Server, "ErrorText.txt", Name, NameParameter, "输入接收邮箱出错",
                    //                 FormatString(strResponseNumber).Trim());
                    Dev.Log.Loger.Error(string.Join(",", NameParameter) + "=>输入接收邮箱出错");

                    errmsg = FormatString(strResponseNumber).Trim();
                    return 6;
                }

                //第六步结束

                //与服务器通讯第七步:输入邮箱主题内容				

                smtpcMail.SendCommandToServer("DATA\r\n");
                Thread.Sleep(50);
                strResponseNumber = smtpcMail.GetServerResponse();

                if (!(smtpcMail.DoesStringContainSMTPCode(strResponseNumber, "354")))
                {
                    smtpcMail.Close();

                    //TextLog.ErrorLog(_Server, "ErrorText.txt", Name, NameParameter, "打开输入邮件主内容出错",
                    //                 FormatString(strResponseNumber).Trim());


                    Dev.Log.Loger.Error(string.Join(",", NameParameter) + "=>打开输入邮件主内容出错");
                    errmsg = "smtpcMail.SendCommandToServer error:" + FormatString(strResponseNumber).Trim();
                    return 7;
                }

                /*邮件主要内容开始			               相关参考	 
				
                FROM:<姓名><邮件地址>                      格式:FROM:管理员 
				  
                TO:  <姓名><邮件地址>                      格式:TO:Name<*****@*****.**>  
				
                SUBJECT:<标题>                             格式:SUBJECT:今天的天气很不错! 
				
                DATE:<时间>                                格式:DATE: Thu, 29 Aug 2002 09:52:47 +0800 (CST)
				
                REPLY-TO:<收邮件地址>                      格式:REPLY-TO:[email protected] 
				
                Content-Type:<邮件类型>                    格式:Content-Type: multipart/mixed; boundary=unique-boundary-1 
				
                X-Priority:<邮件优先级>                    格式:X-Priority:3 
				
                MIME-Version:<版本>                       格式:MIME-Version:1.0 
				
                Content-Transfer-Encoding:<内容传输编码>   格式:Content-Transfer-Encoding:Base64 
				
                X-Mailer:<邮件发送者>                      格式:X-Mailer:FoxMail 4.0 beta 1 [cn]  */

                string 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");

                if (html)
                {
                    strData = string.Concat(strData, "Content-Type: text/html;charset=\"gb2312\"");
                }
                else
                {
                    strData = string.Concat(strData, "Content-Type: text/plain;charset=\"gb2312\"");
                }

                strData = string.Concat(strData, "\r\n");

                //strData = string.Concat(strData,"Content-Transfer-Encoding: base64;");

                //strData = string.Concat(strData,"\r\n");

                strData = string.Concat(strData, "X-Priority: " + priority);

                strData = string.Concat(strData, "\r\n");

                strData = string.Concat(strData, "MIME-Version: 1.0");

                strData = string.Concat(strData, "\r\n");

                //strData = string.Concat(strData,"Content-Type: text/html;" );			

                //strData = string.Concat(strData,"\r\n");							

                strData = string.Concat(strData, "X-Mailer: Email自动发送程序 1.0 ");

                strData = string.Concat(strData, "\r\n" + "\r\n");

                strData = string.Concat(strData, message);

                //执行.命令结束传输

                strData = string.Concat(strData, "\r\n.\r\n");

                smtpcMail.SendCommandToServer(strData);
                Thread.Sleep(50);
                strResponseNumber = smtpcMail.GetServerResponse();

                if (!(smtpcMail.DoesStringContainSMTPCode(strResponseNumber, "250")))
                {
                    smtpcMail.Close();

                    //TextLog.ErrorLog(_Server, "ErrorText.txt", Name, NameParameter, "添加-结束添加邮件内容出错",
                    //                 FormatString(strResponseNumber).Trim());

                    Dev.Log.Loger.Error(string.Join(",", NameParameter) + "=>添加-结束添加邮件内容出错");
                    errmsg = "smtpcMail.SendCommandToServer(strData) error:" + FormatString(strResponseNumber).Trim();
                    return 7;
                }

                //第七步完成

                //最后执行.QUIT命令断开连接

                smtpcMail.SendCommandToServer("QUIT\r\n");
                Thread.Sleep(50);
                smtpcMail.Close();
            }
            catch (SocketException err)
            {
                strErrMessage += err.Message + " " + err.StackTrace;
                errmsg = "SocketException err:" + strErrMessage;
                return -1;
            }
            catch (Exception e)
            {
                strErrMessage += e.Message + " " + e.StackTrace;
                errmsg = "Exception e:" + strErrMessage;
                return -1;
            }
            errmsg = "邮件发送成功";
            return 0;
        }
Example #2
0
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="smtpHost">SMTP服务器</param>
        /// <param name="port">SMTP服务器端口</param>
        /// <param name="from">邮件发送者</param>
        /// <param name="displayFromName">显示的发送者名称</param>
        /// <param name="authentication">是否进行认证</param>
        /// <param name="userName">认证用户名</param>
        /// <param name="password">认证密码</param>
        /// <param name="To">邮件接收者</param>
        /// <param name="displayToName">显示的接收者名称</param>
        /// <param name="priority">优先级</param>
        /// <param name="html">是否为HTML</param>
        /// <param name="Base">URL</param>
        /// <param name="subject">邮件主题</param>
        /// <param name="message">邮件内容</param>
        /// <param name="errmsg">错误信息 </param>
        public int SendMail(string smtpHost, int port, string @from, string displayFromName, bool authentication,
                            string userName, string password, string To, string displayToName, Priority priority,
                            bool html, string Base, string subject, string message, out string errmsg)
        {
            try
            {
                @from = "<" + @from + ">";
                To    = "<" + To + ">";
                ////if (To.IndexOf(";") > -1)
                ////    To = "<" + To.Replace(";", ">,<") + ">";
                ////else
                ////    To = "<" + To + ">";


                var Name = new[]
                {
                    "服务器地址", "端口", "发信邮箱", "发送者名称", "是否认证(Smtp登录)", "认证用户名", "认证密码", "邮件接受者", "接受者姓名",
                    "优先级"
                    , "是否HTML", "相对连接地址(在有图片或连接时)", "主题", "正文"
                };

                var NameParameter = new[]
                {
                    smtpHost, port.ToString(), @from, displayFromName, authentication.ToString(),
                    userName, password, To, displayToName, priority.ToString(), html.ToString(),
                    Base, subject, message
                };

                //以上为发送邮件失败时记录到网站根目录的文本文件中

                string strResponseNumber;                   //smtp服务器返回的信息

                var smtpcMail = new SMTPClient();           //实例化连接类

                smtpcMail.Connect(smtpHost, port);          //连接SmtpHost服务器,端口Port

                bool boolConnect = smtpcMail.isConnected(); //返回连接是否成功

                //判断是否进行了连接

                if (!boolConnect)
                {
                    smtpcMail.Close();

                    //TextLog.ErrorLog(_Server, "ErrorText.txt", Name,  string.Join(",", NameParameter), "未能取得连接", "");


                    Dev.Log.Loger.Error(string.Join(",", NameParameter) + "=>未能取得连接");

                    errmsg = "smtpcMail.Connect error:未能取得连接";
                    return(-1);
                }

                strResponseNumber = smtpcMail.GetServerResponse();                    //得到服务器返回的信息

                if (!(smtpcMail.DoesStringContainSMTPCode(strResponseNumber, "220"))) //包含220表示成功连接
                {
                    smtpcMail.Close();

                    Dev.Log.Loger.Error(string.Join(",", NameParameter) + "=>请求连接失败");

                    //TextLog.ErrorLog(_Server, "ErrorText.txt", Name, NameParameter, "请求连接失败",
                    //                 FormatString(strResponseNumber).Trim());
                    errmsg = "smtpcMail.GetServerResponse error:" + FormatString(strResponseNumber).Trim();
                    return(-1);
                }

                smtpcMail.SendCommandToServer("HELO kaidongmei\r\n");
                Thread.Sleep(50);

                strResponseNumber = smtpcMail.GetServerResponse();

                if (!(smtpcMail.DoesStringContainSMTPCode(strResponseNumber, "250")))
                {
                    smtpcMail.Close();

                    //TextLog.ErrorLog(_Server, "ErrorText.txt", Name, NameParameter, "与服务器连接初期(打招呼出错)",
                    //                 FormatString(strResponseNumber).Trim());

                    Dev.Log.Loger.Error(string.Join(",", NameParameter) + "=>与服务器连接初期(打招呼出错)");

                    errmsg = FormatString(strResponseNumber).Trim();
                    return(1);
                }

                //第一步结束

                if (authentication) //是否认证
                {
                    //与服务器通讯第二步:请求登录

                    smtpcMail.SendCommandToServer("AUTH LOGIN\r\n");
                    Thread.Sleep(50);
                    strResponseNumber = smtpcMail.GetServerResponse();

                    if (!(smtpcMail.DoesStringContainSMTPCode(strResponseNumber, "334")))
                    {
                        smtpcMail.Close();

                        //TextLog.ErrorLog(_Server, "ErrorText.txt", Name, NameParameter, "请求登录出现错误",
                        //                 FormatString(strResponseNumber).Trim());

                        Dev.Log.Loger.Error(string.Join(",", NameParameter) + "=>请求登录出现错误");

                        errmsg = FormatString(strResponseNumber).Trim();
                        return(2);
                    }

                    //与服务器通讯第三步:输入用户帐号

                    smtpcMail.SendCommandToServer(Encode(userName) + "\r\n");
                    Thread.Sleep(50);
                    strResponseNumber = smtpcMail.GetServerResponse();

                    if (!(smtpcMail.DoesStringContainSMTPCode(strResponseNumber, "334")))
                    {
                        smtpcMail.Close();

                        //TextLog.ErrorLog(_Server, "ErrorText.txt", Name, NameParameter, "输入用户名称出错",
                        //                 FormatString(strResponseNumber).Trim());


                        Dev.Log.Loger.Error(string.Join(",", NameParameter) + "=>输入用户名称出错");

                        errmsg = FormatString(strResponseNumber).Trim();
                        return(3);
                    }

                    //第三步结束

                    //与服务器通讯第四步:输入用户密码

                    smtpcMail.SendCommandToServer(Encode(password) + "\r\n");
                    Thread.Sleep(50);
                    strResponseNumber = smtpcMail.GetServerResponse();

                    if (!(smtpcMail.DoesStringContainSMTPCode(strResponseNumber, "235")))
                    {
                        smtpcMail.Close();

                        //TextLog.ErrorLog(_Server, "ErrorText.txt", Name, NameParameter, "输入用户密码出错",
                        //                 FormatString(strResponseNumber).Trim());

                        Dev.Log.Loger.Error(string.Join(",", NameParameter) + "=>输入用户密码出错");

                        errmsg = FormatString(strResponseNumber).Trim();
                        return(4);
                    }

                    //第四步结束
                }

                //与服务器通讯第五步:输入发送邮件的信箱地址

                smtpcMail.SendCommandToServer("MAIL FROM: " + @from + /* +  " AUTH = " + From*/ "\r\n");
                Thread.Sleep(50);
                strResponseNumber = smtpcMail.GetServerResponse();

                if (!(smtpcMail.DoesStringContainSMTPCode(strResponseNumber, "250")))
                {
                    smtpcMail.Close();

                    //TextLog.ErrorLog(_Server, "ErrorText.txt", Name, NameParameter, "输入发信邮箱出错",
                    //                 FormatString(strResponseNumber).Trim());
                    Dev.Log.Loger.Error(string.Join(",", NameParameter) + "=>输入发信邮箱出错");


                    errmsg = FormatString(strResponseNumber).Trim();
                    return(5);
                }

                //第五步结束

                //与服务器通讯第六步:输入接收邮件的信箱地址

                smtpcMail.SendCommandToServer("RCPT TO: " + To + "\r\n");
                Thread.Sleep(50);
                strResponseNumber = smtpcMail.GetServerResponse();

                if (!(smtpcMail.DoesStringContainSMTPCode(strResponseNumber, "250")))
                {
                    smtpcMail.Close();

                    //TextLog.ErrorLog(_Server, "ErrorText.txt", Name, NameParameter, "输入接收邮箱出错",
                    //                 FormatString(strResponseNumber).Trim());
                    Dev.Log.Loger.Error(string.Join(",", NameParameter) + "=>输入接收邮箱出错");

                    errmsg = FormatString(strResponseNumber).Trim();
                    return(6);
                }

                //第六步结束

                //与服务器通讯第七步:输入邮箱主题内容

                smtpcMail.SendCommandToServer("DATA\r\n");
                Thread.Sleep(50);
                strResponseNumber = smtpcMail.GetServerResponse();

                if (!(smtpcMail.DoesStringContainSMTPCode(strResponseNumber, "354")))
                {
                    smtpcMail.Close();

                    //TextLog.ErrorLog(_Server, "ErrorText.txt", Name, NameParameter, "打开输入邮件主内容出错",
                    //                 FormatString(strResponseNumber).Trim());


                    Dev.Log.Loger.Error(string.Join(",", NameParameter) + "=>打开输入邮件主内容出错");
                    errmsg = "smtpcMail.SendCommandToServer error:" + FormatString(strResponseNumber).Trim();
                    return(7);
                }

                /*邮件主要内容开始			               相关参考
                 *
                 * FROM:<姓名><邮件地址>                      格式:FROM:管理员
                 *
                 * TO:  <姓名><邮件地址>                      格式:TO:Name<*****@*****.**>
                 *
                 * SUBJECT:<标题>                             格式:SUBJECT:今天的天气很不错!
                 *
                 * DATE:<时间>                                格式:DATE: Thu, 29 Aug 2002 09:52:47 +0800 (CST)
                 *
                 * REPLY-TO:<收邮件地址>                      格式:REPLY-TO:[email protected]
                 *
                 * Content-Type:<邮件类型>                    格式:Content-Type: multipart/mixed; boundary=unique-boundary-1
                 *
                 * X-Priority:<邮件优先级>                    格式:X-Priority:3
                 *
                 * MIME-Version:<版本>                       格式:MIME-Version:1.0
                 *
                 * Content-Transfer-Encoding:<内容传输编码>   格式:Content-Transfer-Encoding:Base64
                 *
                 * X-Mailer:<邮件发送者>                      格式:X-Mailer:FoxMail 4.0 beta 1 [cn]  */

                string 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");

                if (html)
                {
                    strData = string.Concat(strData, "Content-Type: text/html;charset=\"gb2312\"");
                }
                else
                {
                    strData = string.Concat(strData, "Content-Type: text/plain;charset=\"gb2312\"");
                }

                strData = string.Concat(strData, "\r\n");

                //strData = string.Concat(strData,"Content-Transfer-Encoding: base64;");

                //strData = string.Concat(strData,"\r\n");

                strData = string.Concat(strData, "X-Priority: " + priority);

                strData = string.Concat(strData, "\r\n");

                strData = string.Concat(strData, "MIME-Version: 1.0");

                strData = string.Concat(strData, "\r\n");

                //strData = string.Concat(strData,"Content-Type: text/html;" );

                //strData = string.Concat(strData,"\r\n");

                strData = string.Concat(strData, "X-Mailer: Email自动发送程序 1.0 ");

                strData = string.Concat(strData, "\r\n" + "\r\n");

                strData = string.Concat(strData, message);

                //执行.命令结束传输

                strData = string.Concat(strData, "\r\n.\r\n");

                smtpcMail.SendCommandToServer(strData);
                Thread.Sleep(50);
                strResponseNumber = smtpcMail.GetServerResponse();

                if (!(smtpcMail.DoesStringContainSMTPCode(strResponseNumber, "250")))
                {
                    smtpcMail.Close();

                    //TextLog.ErrorLog(_Server, "ErrorText.txt", Name, NameParameter, "添加-结束添加邮件内容出错",
                    //                 FormatString(strResponseNumber).Trim());

                    Dev.Log.Loger.Error(string.Join(",", NameParameter) + "=>添加-结束添加邮件内容出错");
                    errmsg = "smtpcMail.SendCommandToServer(strData) error:" + FormatString(strResponseNumber).Trim();
                    return(7);
                }

                //第七步完成

                //最后执行.QUIT命令断开连接

                smtpcMail.SendCommandToServer("QUIT\r\n");
                Thread.Sleep(50);
                smtpcMail.Close();
            }
            catch (SocketException err)
            {
                strErrMessage += err.Message + " " + err.StackTrace;
                errmsg         = "SocketException err:" + strErrMessage;
                return(-1);
            }
            catch (Exception e)
            {
                strErrMessage += e.Message + " " + e.StackTrace;
                errmsg         = "Exception e:" + strErrMessage;
                return(-1);
            }
            errmsg = "邮件发送成功";
            return(0);
        }
Example #3
0
        /// <summary>
        /// 返回错误描述
        /// </summary>
        /// <param name="str">服务器返回的信息</param>
        /// <returns>错误描述</returns>
        private string FormatString(string str)
        {
            var smtpcMail = new SMTPClient();

            string s = str.Substring(0, 3);

            str = str.Substring(0, str.IndexOf("\0"));

            switch (s)
            {
                case "500":

                    return "邮箱地址错误" + ",服务器原始反馈信息:" + str;

                case "501":

                    return "参数格式错误" + ",服务器原始反馈信息:" + str;

                case "502":

                    return "命令不可实现" + ",服务器原始反馈信息:" + str;

                case "503":

                    return "服务器需要SMTP验证" + ",服务器原始反馈信息:" + str;

                case "504":

                    return "命令参数不可实现" + ",服务器原始反馈信息:" + str;

                case "421":

                    return "服务未就绪,关闭传输信道" + ",服务器原始反馈信息:" + str;

                case "450":

                    return "要求的邮件操作未完成,邮箱不可用(例如,邮箱忙)" + ",服务器原始反馈信息:" + str;

                case "550":

                    return "要求的邮件操作未完成,邮箱不可用(例如,邮箱未找到,或不可访问)" + ",服务器原始反馈信息:" + str;

                case "451":

                    return "放弃要求的操作;处理过程中出错" + ",服务器原始反馈信息:" + str;

                case "551":

                    return "用户非本地,请尝试<forward-path>" + ",服务器原始反馈信息:" + str;

                case "452":

                    return "系统存储不足,要求的操作未执行" + ",服务器原始反馈信息:" + str;

                case "552":

                    return "过量的存储分配,要求的操作未执行" + ",服务器原始反馈信息:" + str;

                case "553":

                    return "邮箱名不可用,要求的操作未执行(例如邮箱格式错误)" + ",服务器原始反馈信息:" + str;

                case "432":

                    return "需要一个密码转换" + ",服务器原始反馈信息:" + str;

                case "534":

                    return "认证机制过于简单" + ",服务器原始反馈信息:" + str;

                case "538":

                    return "当前请求的认证机制需要加密" + ",服务器原始反馈信息:" + str;

                case "454":

                    return "临时认证失败" + ",服务器原始反馈信息:" + str;

                case "530":

                    return "需要认证" + ",服务器原始反馈信息:" + str;

                default:

                    return "没有匹配的错误处理,服务器返回参数:" + str;
            }
        }
Example #4
0
        /// <summary>
        /// 返回错误描述
        /// </summary>
        /// <param name="str">服务器返回的信息</param>
        /// <returns>错误描述</returns>
        private string FormatString(string str)
        {
            var smtpcMail = new SMTPClient();

            string s = str.Substring(0, 3);

            str = str.Substring(0, str.IndexOf("\0"));

            switch (s)
            {
            case "500":

                return("邮箱地址错误" + ",服务器原始反馈信息:" + str);

            case "501":

                return("参数格式错误" + ",服务器原始反馈信息:" + str);

            case "502":

                return("命令不可实现" + ",服务器原始反馈信息:" + str);

            case "503":

                return("服务器需要SMTP验证" + ",服务器原始反馈信息:" + str);

            case "504":

                return("命令参数不可实现" + ",服务器原始反馈信息:" + str);

            case "421":

                return("服务未就绪,关闭传输信道" + ",服务器原始反馈信息:" + str);

            case "450":

                return("要求的邮件操作未完成,邮箱不可用(例如,邮箱忙)" + ",服务器原始反馈信息:" + str);

            case "550":

                return("要求的邮件操作未完成,邮箱不可用(例如,邮箱未找到,或不可访问)" + ",服务器原始反馈信息:" + str);

            case "451":

                return("放弃要求的操作;处理过程中出错" + ",服务器原始反馈信息:" + str);

            case "551":

                return("用户非本地,请尝试<forward-path>" + ",服务器原始反馈信息:" + str);

            case "452":

                return("系统存储不足,要求的操作未执行" + ",服务器原始反馈信息:" + str);

            case "552":

                return("过量的存储分配,要求的操作未执行" + ",服务器原始反馈信息:" + str);

            case "553":

                return("邮箱名不可用,要求的操作未执行(例如邮箱格式错误)" + ",服务器原始反馈信息:" + str);

            case "432":

                return("需要一个密码转换" + ",服务器原始反馈信息:" + str);

            case "534":

                return("认证机制过于简单" + ",服务器原始反馈信息:" + str);

            case "538":

                return("当前请求的认证机制需要加密" + ",服务器原始反馈信息:" + str);

            case "454":

                return("临时认证失败" + ",服务器原始反馈信息:" + str);

            case "530":

                return("需要认证" + ",服务器原始反馈信息:" + str);

            default:

                return("没有匹配的错误处理,服务器返回参数:" + str);
            }
        }