2

I recently moved my code to MailKit library because I was using S22.Imap but I saw the maintenance wasn't that great.

I previously connected with my own mailserver through 143 and 993, but there is no way to do it through 993 and MailKit.

It just says "The IMAP server has unexpectedly disconnected". It doesn't even reach the Authenticate method, and the log uniquely says "Connected to imap://mail.xxx.com:993/".

I'm currently using the

imap.Connect(hostname, 993, SecureSocketOptions.None);

I've even tried with different SecureSocketOptions it has with no luck. Any idea what could be wrong? With works fine if I'm connecting through 143 and it worked through 993 when I used S22.Imap.

P.S: The Exception it throws is a MailKit.Net.Imap.ImapProtocolException.

Thanks.

Gonzo345
  • 1,133
  • 3
  • 20
  • 42

2 Answers2

5

Port 993 is an SSL-wrapped port, so you need to use:

imap.Connect(hostname, 993, SecureSocketOptions.SslOnConnect);

For port 143, you probably want to use:

imap.Connect(hostname, 143, SecureSocketOptions.StartTlsWhenAvailable);
Bridge
  • 29,818
  • 9
  • 60
  • 82
jstedfast
  • 35,744
  • 5
  • 97
  • 110
  • Thanks for your reply, @jstedfast . If I use the StartTlsWhenAvailable property it throws "remote certificate is not valid by the certification procedure", that's why I kept .None. And same error when I use the SslOnConnect. Any ideas? It worked once through that port using another library, so... what the hell! Thanks! – Gonzo345 Feb 17 '17 at 20:45
  • You need to set the `client.ServerCertificateValidationCallback` to something that can validate your server's certificate. Your machine does not trust your IMAP server because it's certificate is probably not signed by a trusted CA. – jstedfast Feb 17 '17 at 21:24
  • 1
    For demo purposes, you can do this: `client.ServerCertificateValidationCallback = (s,c,h,e) => true;` – jstedfast Feb 17 '17 at 21:27
  • Thanks for this reply. Wow! – Gonzo345 Feb 20 '17 at 06:52
0

I got it to work with this:

this._imap = new ImapClient();
this._imap.Connect(configuration.GetSection("MailSettings")["ImapServer"], 993, true);
this._imap.AuthenticationMechanisms.Remove("XOAUTH2");
this._imap.Authenticate(["ImapUser"], ["ImapPassword"]);
Vortex852456
  • 721
  • 6
  • 23