SSL 인증서가있는 이메일을 보내는 데 문제가 있습니다. 제가 사용할 수있는 제 3자를 알고 있습니다. 그러나 논의하기를 원하지 않는 이유로 사용할 수 없습니다.SSL 인증서가있는 이메일을 보내려면 어떻게해야합니까?
현재 나는
System.Net.Sockets을 사용하여 이메일을 보내고 있습니다. 이해하는 바와 같이이 방법으로 SSL을 통해 전자 메일을 보낼 방법이 없습니다. 그래서 나는
System.Net.Security.SslStream을 건너 왔고 이것을 수행하는 방법을 보여주기위한 몇 가지 샘플 코드를 발견했습니다.
STARTTLS 명령 후 SslStream을 만듭니다. 이 작동하지만 핸드 셰이크를 할 때 오류 메시지가 나타납니다. 예기치 않은 패킷 형식으로 인해 핸드 셰이크가 실패했습니다.
다음은 SSLStream을 만들고 핸드 셰이크를 수행하는 코드입니다.
private SslStream _sslStream = null;
private NetworkStream _networkStream = null;
private TcpClient _client = null;
private bool _SSLActive = false;
public SmtpConnectorWithSSL(string smtpServerAddress, int port) : base(smtpServerAddress, port)
{
_client = new TcpClient(smtpServerAddress, port);
_networkStream = _client.GetStream();
}
public override void CreateSSLConnection()
{
_sslStream = new SslStream(
_networkStream,
true,
new RemoteCertificateValidationCallback(ValidateServerCertificate),
null
);
// The server name must match the name on the server certificate.
try
{
_sslStream.AuthenticateAsClient(SmtpServerAddress, GetX509CertificateCollection(), SslProtocols.Tls, false);
_SSLActive = true;
}
catch (AuthenticationException e)
{
_sslStream = null;
Console.WriteLine("Exception: {0}", e.Message);
if (e.InnerException != null)
{
Console.WriteLine("Inner exception: {0}", e.InnerException.Message);
}
Console.WriteLine("Authentication failed - closing the connection.");
_client.Close();
}
catch (Exception ex)
{
_sslStream = null;
Console.WriteLine("Error: {0}", ex.Message);
_client.Close();
}
}
오류는 "AuthenticateAsClient()"함수에서 발생합니다.
아마도 내가로드하는 인증서와 관련이 있습니까?
public static X509CertificateCollection GetX509CertificateCollection()
{
X509Certificate2 certificate1 = new X509Certificate2(@"C:\Users\leo\OneDrive - Cumulo9 Limited\Documents\Cumulo9\StartCom\mailprimer.com\IISServer\2_mailprimer.com.crt");
X509CertificateCollection collection1 = new X509CertificateCollection();
collection1.Add(certificate1);
return collection1;
}
아마도 인증서가 잘못되었지만이를 찾는 방법을 모르겠습니다. 나는 오류 메시지가 다를 것이라고 예상 할 것이다.
이것이 도움이된다면 나는 전체 샘플 프로젝트를 제공 할 수 있습니다.
도움 주셔서 감사합니다.
* "STARTTLS 명령 이후에 SslStream을 만들고 있습니다."* - STARTTLS 명령을 보내거나 명령에 대한 응답을받은 후에? –
죄송합니다. "220 TLS를 시작할 준비가되었습니다."라는 응답을 얻은 후에 말하십시오. – Leo