MailKit icon indicating copy to clipboard operation
MailKit copied to clipboard

MailKit fails to authenticate against shaw.ca mailserver

Open Saurbaum opened this issue 10 years ago • 47 comments

My apologies for the vagueness I'm waiting on some more information.

Shaw.ca (ISP in Canada) are running a mail server on mail.shaw.ca and one of my users is attempting to connect to it for their email needs. I THINK it's running Exchange 2010 but I can't be certain.

I've managed to get an account out of them to test on but to see the problem you actually wouldn't need one.

I can connect fine

client.Connect(this.ServerAddress, this.ServerPort, SecureSocketOptions.StartTls);

but no matter what I send here

client.Authenticate(this.UserName, this.Password);

no exception is thrown when a logon fails unless I have set the password to blank.

My code then continues having not seen an exception and attempts to send the mail which will return

5.1.0 Authentication required

Using the same details I threw together a test using System.Net.Mail.SmtpClient and that works.

Saurbaum avatar Sep 06 '16 10:09 Saurbaum

In case it helps the connection details are Server: mail.shaw.ca Port: 587

Saurbaum avatar Sep 06 '16 10:09 Saurbaum

Make sure that you are using MailKit 1.4.2.1 and not 1.4.2 (1.4.2 has a bug where a bad login did not throw an exception).

jstedfast avatar Sep 06 '16 12:09 jstedfast

I've tried with 1.4.2.1 and with 1.2.13 but they behave the same on authentication with this server.

Saurbaum avatar Sep 06 '16 12:09 Saurbaum

FWIW, I'm getting an AuthenticationException locally, but it looks like the 1.4.2 issue only affected IMAP and not SMTP (like I thought in my previous comment).

jstedfast avatar Sep 06 '16 12:09 jstedfast

I'll try it again with a bad username in case that makes a difference. It's definitely the strangest mail server I've had this displeasure of connecting to.

Saurbaum avatar Sep 06 '16 12:09 Saurbaum

Just to make sure I'm understanding the issue correctly:

There are 2 issues here:

  1. Using an invalid user/passwd combo, an exception is not being thrown
  2. When the correct user/passwd are being used, it throws an exception on Send() saying that authentication is required

Is that correct? Or is it only the first issue?

The reason I ask is because I think the second issue might be that MailKit sends an EHLO command after authenticating (because the specs say clients should do that in case the server offers different capabilities for authenticated users) but from my own experience, some SMTP servers are broken and when you send EHLO after successfully authenticating, it wipes out their authenticated state variable and they think the client is no longer authenticated.

jstedfast avatar Sep 06 '16 12:09 jstedfast

Correct both 1 and 2 are being seen.

I've double checked and any setting I put in for the username and password, except a blank password, result in no exception.

Then in all cases on this server the send throws the exception because it is no authenticated. It certainly sounds likely that the authenticated state is being dropped. Is there a way for me to prove that? More importantly is there a way for me to not send the second EHLO? No disrespect intended to your benevolent overlords but I doubt Microsoft is going to patch Exchange 2010 any time soon.

Saurbaum avatar Sep 06 '16 12:09 Saurbaum

No disrespect taken :)

Are you able to test MailKit built from source? If so, you can modify https://github.com/jstedfast/MailKit/blob/master/MailKit/Net/Smtp/SmtpClient.cs#L75 to add "smtp.shaw.ca" to that list and that should fix that issue (at least assuming the problem is the one I think it is).

As far as the invalid user/passwd not throwing an exception, that is weird. I'll have to dig some more to figure out why you aren't getting an exception and I am. This is with 1.4.2.1, right?

jstedfast avatar Sep 06 '16 12:09 jstedfast

Yes. I was on 1.2.13 but I figured that you'd really only care about the latest so I've pulled that in as well. I'll double check my build process and make sure the binaries are getting to the right place and I'm not still dealing with an old build by mistake.

I'll try building the source and see what it does for me there.

Saurbaum avatar Sep 06 '16 12:09 Saurbaum

ok, cool. If you are still seeing the problem after you make sure you get a fresh build, can you give me a sample invalid user/pass combo that you are trying?

I've been trying:

client.Authenticate ("invalid", "invalid");

That's what has been giving me the exception.

jstedfast avatar Sep 06 '16 13:09 jstedfast

I've just aded smtp.shaw.ca to that list since I'm fairly certain that will solve the second issue you are seeing.

Now we just need to figure out the invalid user/pass issue.

jstedfast avatar Sep 06 '16 13:09 jstedfast

I've checked it with a locally built dll and yes they are the 4th "honorable" entry into the non standard compliant club. Now I'm deeper in the code I see I can get an exception out of it in 1.4.2.x The SMTP server has unexpectedly disconnected.

Not exactly what I would have expected on an invalid password but it's something. When the full nuget is available I'll get that and pull it in do a full build and test it again.

Saurbaum avatar Sep 06 '16 14:09 Saurbaum

That's not the correct exception, you should be getting an AuthenticationException

jstedfast avatar Sep 06 '16 14:09 jstedfast

Note that it's mail.shaw.ca not smtp.shaw.ca that's being flagged. It appears that they have actually setup and alias on both of them but their own support recommend using mail.shaw.ca

Looks like the server has dropped offline at the moment so I'll have to wait for that to come back online before I investigate it further.

Saurbaum avatar Sep 06 '16 14:09 Saurbaum

Oh, okay. I could probably add both aliases to the list.

jstedfast avatar Sep 06 '16 14:09 jstedfast

Definitely only getting the exception The SMTP server has unexpectedly disconnected.

Using the built in .Net client and the same wrong credentials I get The server committed a protocol violation The server response was: so I don't think this is anything we are doing

I'll raise it with the ISP and try to get my external ip address off the blacklist I seem to have been added to for failing to log on too many times.

Can you push the new version via nuget and we can close this ticket off then?

Saurbaum avatar Sep 06 '16 14:09 Saurbaum

Unfortunately I am working remotely this week and so do not have access to my normal Windows/Visual Studio setup so can't publish any nugets until I get back this weekend.

jstedfast avatar Sep 06 '16 14:09 jstedfast

No worries I won't be rushing this fix out now I know that the mail server is at fault. I'll be arguing this with them for a few days anyway with the timezone.

Saurbaum avatar Sep 06 '16 15:09 Saurbaum

Any chance we can make a minor change to let a user specify that a connection is against a non compliant server? Either as a property on the Smtp client or as part of the connection method? I'm just conscious that this list will grow more and more and I'm sure you don't really want me to keep pestering you with an ever increasing list of dodgy servers.

Saurbaum avatar Sep 06 '16 15:09 Saurbaum

Yea, I've been thinking of how to better solve this. I might just have to give up on doing what I'm supposed to do (i.e. sending EHLO after authenticating) and just default to not sending it.

Either that or having a bool property like "QueryCapabilitiesAfterAuth" or something.

jstedfast avatar Sep 06 '16 15:09 jstedfast