我正在使用的代码仅用于测试,这里我用gmail测试:
C# code:
private void button1_Click(object sender, EventArgs e)
{
string server = "smtp.gmail.com";
int port = 25;
client = new TcpClient();
client.Connect(server, port);
var stream = client.GetStream();
sslStream = new SslStream(stream);
sslStream.AuthenticateAsClient(server);
clearTextReader = new StreamReader(sslStream);
clearTextWriter = new StreamWriter(sslStream);
clearTextWriter.WriteLine("EHLO " + server);
clearTextWriter.Flush();
var response = clearTextReader.ReadLine();
Error(response);
}
但是这个错误出现了这个错误:
System.IO.IOException occurred
HResult=0x80131620
Message=The handshake failed due to an unexpected packet format.
Source=System
StackTrace:
at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
at System.Net.Security.SslStream.AuthenticateAsClient(String targetHost, X509CertificateCollection clientCertificates, SslProtocols enabledSslProtocols, Boolean checkCertificateRevocation)
at System.Net.Security.SslStream.AuthenticateAsClient(String targetHost)
当我尝试在端口443上执行此操作时,它工作正常,但端口25,只是一直出错。我已经尝试了一切,但我找不到解决方案。证书是有效的,当我尝试不同的服务器时它也不起作用。