Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mbodm opened this issue Oct 9, 2017 · 11 comments
Assignees
Milestone

Comments

@mbodm
Copy link

mbodm commented Oct 9, 2017

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

@stephentoub
Copy link
Member

@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

@svick
Copy link
Contributor

svick commented Oct 9, 2017

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.

@jnm2
Copy link
Contributor

jnm2 commented Oct 9, 2017

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?

@mairaw
Copy link
Contributor

mairaw commented Oct 10, 2017

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

@mbodm
Copy link
Author

mbodm commented Oct 13, 2017

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!

@stephentoub
Copy link
Member

Thanks for the kind words, @mbodm! 😄 My pleasure.

@wstaelens
Copy link

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.
🆘 😭 😞 😭

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

(.NET Framework 4.6.2)

@Sebastian1989101
Copy link

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

@nabeelvalley
Copy link
Contributor

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

@danmoseley
Copy link
Member

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.

@davidsh
Copy link
Contributor

davidsh commented Oct 2, 2019

Fixed with PR dotnet/corefx#41271

@davidsh davidsh closed this as completed Oct 2, 2019
@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 5.0 milestone Jan 31, 2020
@dotnet dotnet locked as resolved and limited conversation to collaborators Dec 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests