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

时间:2022-01-18 18:16:15

My application is hosted on IIS running on windows 2008R2.Code to send mail is mentioned below.

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

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;
        }
    }

As you can see from the code,i've checked all ports that are used to send mail. This same code works on application hosted on windows 7/8 but doesn't work on windows 2008R2. Below is the error message i get

从代码中可以看出,我已经检查了用于发送邮件的所有端口。这个相同的代码适用于在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

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

Nothing in innerexception.This same message is repeated for all ports.I am sending this mail via gmail.I've tested with MS Outlook on the server with same gmail account,and it was working properly so i guess firewall ristriction are not applicable in my case.I've also enabled "Access for less secure app" in gmail settings.

在innerexception中没有任何内容。所有端口都重复了同样的消息。我通过gmail发送此邮件。我已经在服务器上使用相同的Gmail帐户测试了MS Outlook,并且它运行正常所以我认为防火墙的限制不适用于我的情况。我还在gmail设置中启用了“访问安全性较低的应用”。

1 个解决方案

#1


0  

McAfee was blocking mail attemps.Never doubted it as i could send the mail from outlook but couldn't do it from my code.Just included w3wp.exe to exclusion in mass mailing

迈克菲正在阻止邮件尝试。没有人怀疑它,因为我可以从Outlook发送邮件,但无法从我的代码中执行。只是包含w3wp.exe以排除群发邮件

#1


0  

McAfee was blocking mail attemps.Never doubted it as i could send the mail from outlook but couldn't do it from my code.Just included w3wp.exe to exclusion in mass mailing

迈克菲正在阻止邮件尝试。没有人怀疑它,因为我可以从Outlook发送邮件,但无法从我的代码中执行。只是包含w3wp.exe以排除群发邮件