runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Possible Bug in "System.Net.Mail.SmtpClient" when using "UseDefaultCredentials" property

Open mbodm opened this issue 9 years ago • 11 comments

Hi,

it seems weird, and after looking a few minutes at your source code, i can not really say why, but it plays a role if the property "UseDefaultCredentials" is set before or after the property "Credentials" of class "SmtpClient" in the Namespace "System.Net.Mail". At least with our mail provider.

Framework: asp.net core Version: 2.0

Example1:

var client = new SmtpClient("asmtp.mail.hostpoint.ch", 587);
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("our_usr", "our_pwd");
client.EnableSsl = true;
//... Create MailMessage in variable "message"
client.Send(message);

Example2:

var client = new SmtpClient("asmtp.mail.hostpoint.ch", 587);
client.Credentials = new NetworkCredential("our_usr", "our_pwd");
client.UseDefaultCredentials = false;
client.EnableSsl = true;
//... Create MailMessage in variable "message"
client.Send(message);

Result: Example1 works like a charm, while Example2 fails with this Exception:

"System.Net.Mail.SmtpException: 'Mailbox unavailable. The server response was: You are not authenticated. Try to activate SMTP Authentication in your mail'"

There is really no difference, just the switch of that 2 lines of code. We tested this in a clean new asp.net core test project. We simply could reproduce it as often as we want, just by switching this 2 lines. VERY weird ! :)

Our provider for the mail is "Hostpoint" from Swizzerland (see host above in SmtpClient() constructor). Its not possible for me (security reasons) to offer you more information, like users, passwords, etc. so you can test. Sorry.

But maybe the problem exists with other Mail providers like GMail etc. and you can reproduce it there. I didnt tested that. Maybe it is some really special thing with our Mail provider. But it seems definitelly worth for you to look at, i think. Or at least for me, to inform you about that :)

Just want let you know about that strange issue.

Best regards, Marcel

mbodm avatar Oct 09 '17 14:10 mbodm

@MBODM, presumably you're seeing this behavior not just with .NET Core but also with the .NET Framework / desktop? They have the same implementation for this: UseDefaultCredentials is implemented effectively as just a shorthand for storing a specific credential object, so it's mutually exclusive with Credentials: http://referencesource.microsoft.com/#System/net/System/Net/mail/SmtpClient.cs,267 http://referencesource.microsoft.com/#System/net/System/Net/mail/SmtpClient.cs,281 http://source.dot.net/#System.Net.Mail/System/Net/Mail/SmtpClient.cs,240 http://source.dot.net/#System.Net.Mail/System/Net/Mail/SmtpClient.cs,257

That said, I agree it's a little confusing, and the docs don't make this clear. cc: @Priya91, @mairaw

stephentoub avatar Oct 09 '17 14:10 stephentoub

I think looking at the implementation makes it clear what the problem is:

https://github.com/dotnet/corefx/blob/4979e8b57b4d926a3cebd101c1b6cd6cf7ada58d/src/System.Net.Mail/src/System/Net/Mail/SmtpClient.cs#L227-L259

When you set client.UseDefaultCredentials to false, it resets Credentials to null.

What's confusing is that I think the docs contradict this behavior:

If the UseDefaultCredentials property is set to false, then the value set in the Credentials property will be used for the credentials when connecting to the server.

svick avatar Oct 09 '17 14:10 svick

Absolutely. UseDefaultCredentials.set needs if (UseDefaultCredentials == value) return; semantics.

The weird part is I'm almost certain there was come kind of opposite behavior where it was necessary to set UseDefaultCredentials = false if you wanted to be able to set Credentials to custom credentials, so that's been boilerplate I've used in the past. Looking at the implementation, it's obvious that this is not the case now. Maybe never has been?

jnm2 avatar Oct 09 '17 15:10 jnm2

If this is just a documentation issue, feel free to move it to our repo @stephentoub!

mairaw avatar Oct 10 '17 22:10 mairaw

Hey, sorry for the late reply. I have been ill and read the answers only now.

@svick You are totally right. I just missed that ternary operator part, setting the Credentials back to null.

@stephentoub Thx for your answer. I think @jnm2 is right and the class/properties just need a slightly different design. Or at least there should be some short info in the property quick tip, showed with Intellisense.


@stephentoub P.S.: Totally aside from the above issue (maybe i will never get the chance again to talk to you ^^): Let me say THX for all the great work you did Stephen, over all the years. All that stuff, especially in the TPL/async/await corner, is fantastic and absolutelly top notch. That level of high quality, of work like yours, is what makes .NET/C# this great platform it is. Great job. So, THX for all of it!

mbodm avatar Oct 13 '17 07:10 mbodm

Thanks for the kind words, @MBODM! :smile: My pleasure.

stephentoub avatar Oct 13 '17 11:10 stephentoub

OMG, Office 365.... UseDefaultCredentials = false was set BEHIND NetworkCredentials and we were getting SMTP 5.7.57. After hours of debugging, we found out just by switching the two lines, so that UseDefaultCredentials = false was set before NetworkCredentials it worked. 🆘 😭 😞 :sob:

That is no 'documentation' bug, it is super-annoying-lame-code-behaviour-bug-stuff

(.NET Framework 4.6.2)

wstaelens avatar Oct 01 '18 12:10 wstaelens

This bug is still there and it is very dumb and annoying...

Sebastian1989101 avatar Dec 07 '18 01:12 Sebastian1989101

I'd like to work on this, still experiencing this issue

nabeelvalley avatar Sep 19 '19 16:09 nabeelvalley

Hi @nabeelvalley you'd be welcome. There is documentation in this repo that should be sufficient to get you building, editing and debugging. Please let us know if you get stuck.

danmoseley avatar Sep 21 '19 02:09 danmoseley

Fixed with PR dotnet/corefx#41271

davidsh avatar Oct 02 '19 01:10 davidsh