SMTP - The handshake failed due to an unexpected packet format.

Dec 8, 2013 at 7:45 PM
Edited Dec 8, 2013 at 7:47 PM
i'm trying to send an email via smtp.live.com but i keep getting
The handshake failed due to an unexpected packet format.
Console.WriteLine("Connecting...");
            //ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(AcceptAllCertifications);
            SmtpClient client = new SmtpClient();


            client.ServerName = "smtp.live.com";
            client.Port = 587;
            client.UserName = "xxxxx@outlook.com";
            client.Password = "xxxxx";

            client.RemoteCertificateValidationCallback = new RemoteCertificateValidationCallback(AcceptAllCertifications);
            client.Ssl = true;
         

            SmtpMessage mail = new SmtpMessage();
            string From = "xxxxx@outlook.com";
            mail.From = new HigLabo.Net.Mail.MailAddress(From);
            mail.To.Add(new HigLabo.Net.Mail.MailAddress("xxxxx@gmail.com"));
            mail.Subject = "xxxxx";
            mail.BodyText = "xxxxx<br>xxxxx!";
            mail.IsHtml = true;
        

            client.AuthenticateMode = SmtpAuthenticateMode.Auto;
            string laststat = "";
            client.Open();
            if (client.Authenticate())
            {
                Console.WriteLine("Sending Email...");
                var rs = client.SendMail(mail);

                if (rs.SendSuccessful == true)
                {
                    laststat = "success";
                }
                else
                {
                    laststat = rs.State;
                }
            }
            else
            {
                laststat = client.State;
            }

            Console.Write(laststat);
            Console.ReadLine();
i'd really appreciate if anyone that can point me to the right direction !

PS: after some digging around i heard that in order for the smtp request to go through we need to send these commands in order :
  1. Connect
  2. HELO - will not accept the STARTTLS until this has been sent
  3. STARTTLS - apparently this sets up the server to accept an encrypted connection
Coordinator
Dec 9, 2013 at 12:54 AM
Hi.

What is your "an unexpected packet format"?
Can you describe more detail?
What Exception raise when you execute your code?

regards.
Dec 9, 2013 at 11:35 AM
this is the whole exeption i was getting
System.IO.IOException was unhandled
  HResult=-2146232800
  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)
       at HigLabo.Net.SocketClient.Connect() in C:\Users\HP\Documents\Visual Studio 2010\Projects\higlabo-105132\HigLaboNet\HigLabo.Net\Socket\SocketClient.Net4.cs:line 274
       at HigLabo.Net.Smtp.SmtpClient.Open() in C:\Users\HP\Documents\Visual Studio 2010\Projects\higlabo-105132\HigLaboNet\HigLabo.Mail\Smtp\SmtpClient.cs:line 127
       at Sendemail.Program.Main(String[] args) in C:\Users\HP\Documents\Visual Studio 2010\Projects\Sendemail\Sendemail\Program.cs:line 65
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
Its failing on Ssl.AuthenticateAsClient() verification
i'm guessing this is because a HELO command is not sent and the handshake failed because smtp.live.com requires this command to be executed before the STARTTLS command
Coordinator
Dec 10, 2013 at 5:40 AM
There are two way sending mail by secure connection.
One is SSL, second is TLS.

Please try to set Tls property to true and Ssl property to false.


ragards
Dec 11, 2013 at 3:59 PM
Edited Dec 11, 2013 at 4:02 PM
Both doesn't work with outlook.com and live.com smtp servers :(
tried all smtp hosts and all smtp ports with switching between tls and ssl , but no luck !

can you add support to hotmail and outlook servers ??
Coordinator
Dec 11, 2013 at 11:18 PM
Hi.

I can add support for hotmail,outlook support.
Perhaps, hotmail & Certificate may be something wrong.

I can send a mail from smtp.live.com by below code without Certificate.
        var cl = new SmtpClient("smtp.live.com", 587, "xxxx@hotmail.com", "xxxxx");
        cl.Tls = true;
        cl.Ssl = false;
        SmtpMessage mg = new SmtpMessage();
        mg.From = new MailAddress("xxxx@hotmail.com");
        mg.To.Add(new MailAddress("yyyy@gmail.com"));
        mg.Subject = "Hotmail Live mail";
        mg.BodyText = "This is a mail from hotmail smtp server!";
        var text = mg.GetDataText();

        var result = cl.SendMail(mg);

Can you send a mail without Certificate from smtp.live.com?
Dec 12, 2013 at 10:27 AM
wow , thanks so much , that did the trick :D
Coordinator
Jan 7, 2014 at 1:05 AM
Hi Aymen99.
I modify that trick by adding EncryptedCommunication property to SmtpClient object.
If you use TLS
cl.EncryptedCommunication = SmtpEncryptedCommunication.Tls;
When Ssl
cl.EncryptedCommunication = SmtpEncryptedCommunication.Ssl;

More intuitive for developer.

Thank you for your comment.
That make me this idea.

regards