My application is hosted on IIS running on windows 2008R2.Code to send mail is mentioned below.
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
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.