2011-02-24 5 views
1

C# 사용 STARTTLS를 지원하는 SMTP 서버에 연결하고 SSL 인증서를 얻으려면 어떻게해야합니까? 나는 이런 식으로 openssl을 사용할 수 있다는 것을 알고있다.C#을 사용하여 SMTP 서버 인증서 받기

openssl s_client -starttls smtp -crlf -connect 192.168.0.1:25 

나는 openssl을 호출하고 그 결과를 분석하고 싶지 않다. C#으로하는 방법에 대한 예제는 대단히 감사하겠습니다.

답변

1

한 가지 방법은 ServerCertificateValidationCallback 콜백에 바인딩하여 서버에 테스트 메시지를 보내는 것입니다. 메일 메시지가 실패 할 수도 있지만 인증서는 여전히 유효합니다.

private void Form1_Load(System.Object sender, System.EventArgs e) 
    { 
     System.Net.ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(RemoteServerCertificateValidationCallback); 
     using (System.Net.Mail.SmtpClient S = new System.Net.Mail.SmtpClient("smtp.gmail.com")) 
     { 
      S.EnableSsl = true; 
      using (System.Net.Mail.MailMessage M = new System.Net.Mail.MailMessage("[email protected]", "[email protected]", "Test", "Test")) 
      { 
       try 
       { 
        S.Send(M); 
       } 
       catch (Exception) 
       { 
        return; 
       } 
      } 
     } 
    } 
    private bool RemoteServerCertificateValidationCallback(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors) 
    { 
     Console.WriteLine(certificate); 

     return true; 
    } 
+0

크리스. 이것은 매우 유용하게 보입니다. – mozza

+0

'RemoteServerCertificateValidationCallback'은 실제로 인증서의 사용자 지정 유효성 검사를 수행하기 위해 사용하기위한 것입니다. 나는 cert가 실제로 유효한지 상관하지 않기 때문에 여기에서 사실로 돌아가고있다. –

+0

예, 이해합니다. – mozza