Hi
When I am trying to connect using imap, I am getting "The handshake failed due to an unexpected packet format " error. Is this something to do with SSL negotiation?
I am using the following line of code when connecting. Is this the problem?
client.AuthenticationMechanisms.Remove("XOAUTH");
That exception does not come from MailKit code, so it must be coming from .NET's BCL (Socket.Connect perhaps?).
client.AuthenticationMechanisms.Remove("XOAUTH");
That code is unrelated to the error.
From a quick Google of the error, it looks like your server requires an SSL-wrapped connection.
If you are using the ImapClient.Connect(Uri uri) method, you need to make sure to use the "imaps://" protocol.
If you are using the ImapClient.Connect(string host, int port, bool useSssl) method, you need to use true for the 'useSsl' parameter.
I had this same problem, for me when using port 587 with SSL it failed, but when using port 465 with SSL it worked.
Which is weird because that's the opposite behaviour of System.Net.Mail.SmtpClient (which works for 587 with SSL but fails with 465 with SSL).
TL;DR: Try using port 465 instead of 587.
Port 587 is a clear-text port for SMTP which is why you got the error when using an SSL-wrapped connection.
The way to get SSL support on port 587 is to use STARTTLS (which MailKit will do automatically for you once it connects unless you explicitly disable the feature).
Ah right, yes using 587 with SSL=false does work. Thanks!
Btw I love MailKit, it's fantastic!
Using 587 with SSL=false does work for me too using office 365.
Thanks!
ran into the same issue with smtp.office365.com, ssl needs to be set to _false_ to make a connection over port 587 (client.Connect(hostname....))
Port 587 always needs use false as the useSsl parameter to Connect().
A value of true should only ever be used for port 465 unless you know for sure that the port is ssl-wrapped. In general, port 465 is the only port that does.
Most helpful comment
I had this same problem, for me when using port 587 with SSL it failed, but when using port 465 with SSL it worked.
Which is weird because that's the opposite behaviour of System.Net.Mail.SmtpClient (which works for 587 with SSL but fails with 465 with SSL).
TL;DR: Try using port 465 instead of 587.