sslstream won't work with SMTP protocol

1

The code i am using is simply for testing, here i test with 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);
    }

But this errors with this error:

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)

When i try to do this on port 443, it works fine, however port 25, just errors all the time. I've tried everything but i can't find a solution. The certificate is valid, also when i try different servers it doesn't work.

packet
handshake
sslstream
asked on Stack Overflow May 26, 2017 by Void_public • edited May 26, 2017 by Svenmarim

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0