I had given solution in C# and VB.NET for Asp.Net C# the-remote-certificate-is-invalid-according-to-the-validation-procedure
copy text which in bold letter is solution command.
C# Solution:
public static bool SendEmail(string toEmail, string subject, string msgBody, string FileName = "")
{
bool flag = true;
string FromAddress = ConfigurationManager.AppSettings("FromAddress");
System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
System.Net.Configuration.MailSettingsSectionGroup settings = (System.Net.Configuration.MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");
SmtpClient client = new SmtpClient();
// If Not (client.Host = "localhost" Or client.Host = "127.0.0.1") Then
System.Net.NetworkCredential credential = new System.Net.NetworkCredential(settings.Smtp.Network.UserName, settings.Smtp.Network.Password);
client.Host = settings.Smtp.Network.Host;
client.Port = settings.Smtp.Network.Port;
client.UseDefaultCredentials = false;
client.Credentials = credential;
client.EnableSsl = ConfigurationManager.AppSettings("IsSSL");
// Else
// client = New SmtpClient("localhost")
// End If
MailMessage mailMsg = new MailMessage();
mailMsg.From = new MailAddress(FromAddress);
var listoftos = Regex.Split(toEmail, ",|;");
foreach (var toeid in listoftos)
mailMsg.To.Add(new MailAddress(toeid));
string bcc = ConfigurationManager.AppSettings("ToBCC");
var listofbcc = Regex.Split(bcc, ",|;");
foreach (var Item in listofbcc)
mailMsg.Bcc.Add(new MailAddress(Item));
mailMsg.IsBodyHtml = true;
mailMsg.Subject = subject;
if (Strings.Trim(FileName) != "" && System.IO.File.Exists(FileName) == true)
mailMsg.Attachments.Add(new Attachment(FileName));
msgBody = msgBody.Replace("[siteurl]", ConfigurationManager.AppSettings("MainSite"));
mailMsg.Body = msgBody;
mailMsg.Priority = MailPriority.Normal;
//Add this line to bypass the certificate validation
System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate(object s,
System.Security.Cryptography.X509Certificates.X509Certificate certificate,
System.Security.Cryptography.X509Certificates.X509Chain chain,
System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
return true;
};
client.Send(mailMsg);
return flag;
}
VB.NET Solution:
Public Shared Function SendEmail(ByVal toEmail As String, ByVal subject As String, ByVal msgBody As String, Optional ByVal FileName As String = "") As Boolean
Dim flag As Boolean = True
Dim FromAddress As String = ConfigurationManager.AppSettings("FromAddress")
Dim config As System.Configuration.Configuration = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath)
Dim settings As System.Net.Configuration.MailSettingsSectionGroup = CType(config.GetSectionGroup("system.net/mailSettings"), System.Net.Configuration.MailSettingsSectionGroup)
Dim client As New SmtpClient()
'If Not (client.Host = "localhost" Or client.Host = "127.0.0.1") Then
Dim credential As New System.Net.NetworkCredential(settings.Smtp.Network.UserName, settings.Smtp.Network.Password)
client.Host = settings.Smtp.Network.Host
client.Port = settings.Smtp.Network.Port
client.UseDefaultCredentials = False
client.Credentials = credential
client.EnableSsl = ConfigurationManager.AppSettings("IsSSL")
'Else
'client = New SmtpClient("localhost")
'End If
Dim mailMsg As New MailMessage
mailMsg.From = New MailAddress(FromAddress)
Dim listoftos = Regex.Split(toEmail, ",|;")
For Each toeid In listoftos
mailMsg.To.Add(New MailAddress(toeid))
Next
Dim bcc As String = ConfigurationManager.AppSettings("ToBCC")
Dim listofbcc = Regex.Split(bcc, ",|;")
For Each Item In listofbcc
mailMsg.Bcc.Add(New MailAddress(Item))
Next
mailMsg.IsBodyHtml = True
mailMsg.Subject = subject
If Trim(FileName) <> "" AndAlso System.IO.File.Exists(FileName) = True Then mailMsg.Attachments.Add(New Attachment(FileName))
msgBody = msgBody.Replace("[siteurl]", ConfigurationManager.AppSettings("MainSite"))
mailMsg.Body = msgBody
mailMsg.Priority = MailPriority.Normal
System.Net.ServicePointManager.ServerCertificateValidationCallback = Function(ByVal s As Object, ByVal certificate As System.Security.Cryptography.X509Certificates.X509Certificate, ByVal chain As System.Security.Cryptography.X509Certificates.X509Chain, ByVal sslPolicyErrors As System.Net.Security.SslPolicyErrors) True
client.Send(mailMsg)
Return flag
End Function
No comments:
Post a Comment