无法从 windows 2008R2 上托管在 IIS 运行 上的 asp.net(框架版本 4)应用程序发送邮件?

Unable to send mail from asp.net (framework version 4) application hosted on IIS running on windows 2008R2?

我的应用程序托管在 IIS 运行 上 windows 2008R2.Code 发送邮件如下所述。

public bool SendMail(string to, string cc, string subject, string body)
    {
        bool abc = false;
        System.Net.Mail.SmtpClient smtpClient = null; 
        System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
        bool IsSSLfailed = false;
        bool allPortsFailed = false;
        bool SSLEnabled = false;
        bool CertificateByPassed = false;
        smtpClient = new System.Net.Mail.SmtpClient();
    again: try
        {

            //  SmtpClient smtpserver = new SmtpClient("smtp.gmail.com", 25);

            string from = ConfigurationSettings.AppSettings.Get("UserName");
            string Password = ConfigurationSettings.AppSettings.Get("Password");
            message.From = new MailAddress(from);
            message.To.Add(to);
            message.Subject = subject;
            message.IsBodyHtml = true;
            message.Body = body;
            message.Subject = WebUtility.HtmlEncode(subject);
            message.SubjectEncoding = Encoding.UTF8;
            message.IsBodyHtml = true;
            message.Body = WebUtility.HtmlDecode(body);
            message.BodyEncoding = Encoding.UTF8;



            smtpClient.Timeout = 10000;
            message.SubjectEncoding = Encoding.UTF8;
            if (IsSSLfailed)
            {
                if (smtpClient.Port == 587)
                {
                    smtpClient.Port = 465;
                }
                else if (smtpClient.Port == 465)
                {
                    smtpClient.Port = 25;
                }
                else if (smtpClient.Port == 25)
                {
                    smtpClient.Port = 2525;
                    allPortsFailed = true;
                }


            }
            else
            {
                smtpClient.Port = 587;
            }
            smtpClient.EnableSsl = SSLEnabled;
            if (!CertificateByPassed)
            {
                BypassCertificateError();    
            }

            System.Net.NetworkCredential nc = new System.Net.NetworkCredential(from, Password);
            smtpClient.Credentials = nc;
            smtpClient.Send(message);
            abc = true;
        }
        catch (System.Net.Mail.SmtpFailedRecipientException ex)
        {
            IsSSLfailed = true;
            logger.ErrorFormat("Error occured in SendMail() method,CertificateBypassed:" + CertificateByPassed.ToString() + "  SSLEnabled: " + SSLEnabled.ToString() + " smtp User:" + ((NetworkCredential)smtpClient.Credentials).UserName + " pwd:" + ((NetworkCredential)smtpClient.Credentials).Password + " smtpClient.DeliveryMethod:" + smtpClient.DeliveryMethod.ToString() + " smtpClient.EnableSsl:" + smtpClient.EnableSsl.ToString() + " smtpClient.Host:" + smtpClient.Host.ToString() + " smtpClient.Port:" + smtpClient.Port.ToString()+ " smtpClient.UseDefaultCredentials:" + smtpClient.UseDefaultCredentials.ToString() + " Detailed error:{0}{1}", Environment.NewLine, ex.Message.ToString());
            logger.ErrorFormat("Inner Exception", Environment.NewLine, ex.InnerException != null ? ex.InnerException.Message.ToString() : string.Empty);

            if (!allPortsFailed)
            {
                goto again;
            }
            else if (!SSLEnabled)
            {
                SSLEnabled = true;
                IsSSLfailed = false;
                allPortsFailed = false;
                goto again;
            }
            else if (!CertificateByPassed)
            {
                CertificateByPassed = true;
                SSLEnabled = false;
                IsSSLfailed = false;
                allPortsFailed = false;
                goto again;

            }
            this.upForm.Update();
            throw ex;
        }
        catch (System.Net.Mail.SmtpException ex)
        {
            IsSSLfailed = true;
            logger.ErrorFormat("SendMail SmtpException,CertificateBypassed:" + CertificateByPassed.ToString() + "  SSLEnabled: " + SSLEnabled.ToString() + " smtp User:" + ((NetworkCredential)smtpClient.Credentials).UserName + " pwd:" + ((NetworkCredential)smtpClient.Credentials).Password + " smtpClient.DeliveryMethod:" + smtpClient.DeliveryMethod.ToString() + " smtpClient.EnableSsl:" + smtpClient.EnableSsl.ToString() + " smtpClient.Host:" + smtpClient.Host.ToString() + " smtpClient.Port:" + smtpClient.Port.ToString() + " smtpClient.UseDefaultCredentials:" + smtpClient.UseDefaultCredentials.ToString() + "  StatusCode:" + ex.StatusCode.ToString() + " " + smtpClient.Port.ToString(), Environment.NewLine, ex.Message.ToString());
            logger.ErrorFormat("SendMail Inner ExceptionSmtpException StatusCode:" + ex.StatusCode.ToString() + " " + smtpClient.Port.ToString(), Environment.NewLine, ex.InnerException != null ? ex.InnerException.Message.ToString() : string.Empty);

            if (!allPortsFailed)
            {
                goto again;
            }
            else if (!SSLEnabled)
            {
                SSLEnabled = true;
                IsSSLfailed = false;
                allPortsFailed = false;
                goto again;
            }
            else if (!CertificateByPassed)
            {
                CertificateByPassed = true;
                SSLEnabled = false;
                IsSSLfailed = false;
                allPortsFailed = false;
                goto again;

            }
            this.upForm.Update();
            throw ex;
        }
        catch (Exception ex)
        {
            IsSSLfailed = true;
            logger.ErrorFormat("SendMail error sending mail,CertificateBypassed:" + CertificateByPassed.ToString() + "  SSLEnabled: " + SSLEnabled.ToString() + " smtp User:" + ((NetworkCredential)smtpClient.Credentials).UserName + " pwd:" + ((NetworkCredential)smtpClient.Credentials).Password + " smtpClient.DeliveryMethod:" + smtpClient.DeliveryMethod.ToString() + " smtpClient.EnableSsl:" + smtpClient.EnableSsl.ToString() + " smtpClient.Host:" + smtpClient.Host.ToString() + " smtpClient.Port:" + smtpClient.Port.ToString() +" smtpClient.UseDefaultCredentials:" + smtpClient.UseDefaultCredentials.ToString() + " " + " " + smtpClient.Port.ToString(), Environment.NewLine, ex.Message.ToString());
            logger.ErrorFormat("SendMail error sending mail,CertificateBypassed:" + CertificateByPassed.ToString() + "  SSLEnabled: " + SSLEnabled.ToString() + " smtp User:" + ((NetworkCredential)smtpClient.Credentials).UserName + " pwd:" + ((NetworkCredential)smtpClient.Credentials).Password + " smtpClient.DeliveryMethod:" + smtpClient.DeliveryMethod.ToString() + " smtpClient.EnableSsl:" + smtpClient.EnableSsl.ToString() + " smtpClient.Host:" + smtpClient.Host.ToString() + " smtpClient.Port:" + smtpClient.Port.ToString() + " smtpClient.UseDefaultCredentials:" + smtpClient.UseDefaultCredentials.ToString() + " " + " " + smtpClient.Port.ToString(), Environment.NewLine, ex.InnerException != null ? ex.InnerException.Message.ToString() : string.Empty);

            if (!allPortsFailed)
            {
                goto again;
            }
            else if (!SSLEnabled)
            {
                SSLEnabled = true;
                IsSSLfailed = false;
                allPortsFailed = false;
                goto again;
            }
            else if (!CertificateByPassed)
            {
                CertificateByPassed = true;
                SSLEnabled = false;
                IsSSLfailed = false;
                allPortsFailed = false;
                goto again;

            }
            this.upForm.Update();
            throw ex;
        }

        return abc;

    }
    public static void BypassCertificateError()
    {
        try
        {
            ServicePointManager.ServerCertificateValidationCallback +=

                    delegate(
                        Object sender1,
                        X509Certificate certificate,
                        X509Chain chain,
                        SslPolicyErrors sslPolicyErrors)
                    {
                        return true;
                    };
        }
        catch (Exception ex)
        {

            throw ex;
        }
    }

正如您从代码中看到的,我已经检查了所有用于发送邮件的端口。 此相同代码适用于 windows 7/8 上托管的应用程序,但不适用于 windows 2008R2。 以下是我收到的错误消息

SendMail SmtpException,CertificateBypassed:False SSLEnabled: False smtpClient.DeliveryMethod:Network smtpClient.EnableSsl:False smtpClient.Host:SMTP.GMAIL.COM smtpClient.Port:587 smtpClient.UseDefaultCredentials:False StatusCode:GeneralFailure

innerexception.This 中没有任何内容重复所有 ports.I 我通过 gmail.I 发送此邮件已在具有相同 gmail 帐户的服务器上使用 MS Outlook 进行了测试,它是工作正常,所以我猜防火墙限制不适用于我的 case.I 我还在 gmail 设置中启用了 "Access for less secure app"。

McAfee 正在阻止邮件 attemps.Never 对此表示怀疑,因为我可以从 outlook 发送邮件,但无法从我的 code.Just 发送邮件,包括 w3wp.exe 以排除在群发邮件中