简体   繁体   中英

C# Get SSL-Certificate expiration date of SMTP-Server as return value from ServerCertificateValidationCallback

At the moment I've got this code to get the expiration date of a SMTP-Server:

namespace SMTPCert
{
    public class SMTPCert
    {
        public static void GetSMTPCert(string ServerName)
        {
            ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(RemoteServerCertificateValidationCallback);
            using (System.Net.Mail.SmtpClient S = new System.Net.Mail.SmtpClient(ServerName))
            {
                S.EnableSsl = true;
                using (System.Net.Mail.MailMessage M = new System.Net.Mail.MailMessage("test@example.com", "test@example.com", "Test", "Test"))
                {
                    try
                    {
                        S.Send(M);
                    }
                    catch (Exception)
                    {
                        return;
                    }
                }
            }
        }

private static bool RemoteServerCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { Console.WriteLine(certificate); return true; } }

}

My problem is that I want to change the GetSMTPCert method from void to string that I can return the certificate expiration date to my main method. But at the moment I'm only able to get the expiration date in the RemoteServerCertificateValidationCallback method and can't return it from there. Is there any possible way to get the certificate expiration date to my GetSMTPCert method and then return it to my main method?

Suggestions about other ways to get the SSL-Certificate expiration date of a SMTP-Server are also welcome.

Ok I solved the problem by adding the public static field of type string "CertificateDaysLeft" to my SMTPCert class.

namespace SMTPCert
{
    public static string CertificateDaysLeft;

    public static string GetSMTPCert(string ServerName)
    {
        ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(RemoteServerCertificateValidationCallback);
        using (System.Net.Mail.SmtpClient S = new System.Net.Mail.SmtpClient(ServerName))
        {
            S.EnableSsl = true;
            using (System.Net.Mail.MailMessage M = new System.Net.Mail.MailMessage("test@example.com", "test@example.com", "Test", "Test"))
            {
                try
                {
                    S.Send(M);
                    string daysLeft = CertificateDaysLeft;
                    return daysLeft;
                }
                catch (Exception)
                {
                    string daysLeft = CertificateDaysLeft;
                    return daysLeft;
                }
            }
        }
    }

    private static bool RemoteServerCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
    {
        DateTime ExpirDate = Convert.ToDateTime(certificate.GetExpirationDateString());
        string DaysLeft = Convert.ToString((ExpirDate - DateTime.Today).Days);
        CertificateDaysLeft = DaysLeft;
        Console.WriteLine(certificate);
        return true;
    }
}

}

I guess I thought a bit too complicated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM