Thursday, 19 November 2015

How to remove empty spaces from string using dotnet regex?





Use below method to remove empty spaces:


public static string RemoveEmptySpace(string inputString)
        {
            string ouputString = string.Empty;
            if (!string.IsNullOrEmpty(inputString))
            {
                ouputString = Regex.Replace(inputString, "\\s+", " ");
                ouputString = Regex.Replace(ouputString, "^\\s+|\\s+$", "");
            }
            return ouputString;

        }

How to ignore certificate validation when sending an email using SmtpClient in Dotnet


Issue:
If there is some Issue with Server certificate below error will occur,
The remote certificate is invalid according to the validation procedure


Solution:
1)      Use valid server certificates.
2)      Right code to ignore invalid certificate (refer below code lines).


Code lines:
1.      ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertificate;
2.      smtpClient.Send(mailMessage);

Add Callback handler (ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertificate) before sending email (before smtpClient.Send() method)


ValidateServerCertificate() definition:

private static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
        {
            if (sslPolicyErrors == SslPolicyErrors.None)
            {
                return true;
            }
            //The server certificate is not valid. Continuing with sending email.;
            return true;
        }