Reputation: 303
I hope someone can help clear something up for me as this is another situation where something is working but I don't fully understand WHY it's working:
I have standard SMTP settings in web.config and I have a custom mail section also because I need to send from different email addresses depending on which method is called in code.
I figured because I had defined both my different mail from addresses in the custom sections I could remove the standard mailSettings section but the code falls over at the from address, I believe because I'm defining a new System.Net.Mail.MailMessage instance and this automatically looks to the standard mailSettings section.
If I have the standard mailSettings section in place, both my methods work, sending emails from different addresses.
I had a look at Scott Guthrie's blog post on this and he too has standard settings in mailSettings but is also defining the from address - in his example the from address in the method is different from the from address in the mailSettings section in web.config
So, my questions are:
It seems to me that in Scott Guthrie's example, he's overriding the from address in mailSettings with that in his code and if he didn't define a from address in code it would default to the one in mailSettings but I'm hoping someone can just clarify this for me.
Many thanks!
Upvotes: 0
Views: 223
Reputation: 124686
1.Does the MailMessage class automatically look to the mailSettings section?
Yes
2.Why when I'm defining the from address in the custom section is this not picked up, and why in that case can I not remove the standard mailSettings section?
I don't know what you mean by a "custom section". If it's a custom section you've defined, why should it be picked up automatically?
3.If I have to use the standard settings because I'm using the MailMessage class, how can the from address be defined as something different than in the mailSettings section, and why do we have a from address in there?
The From address in the smtp configuration will be used if you don't explicitly set a From address in code. You can set a different From address in code if your smtp server allows it. Typically an smtp server that requires authentication (*) won't allow an arbitrary From address to be used, in which case it makes more sense for the From address to be set in configuration.
(*) though I've worked in organizations where the smtp server did not require authentication on the intranet. It usually only takes one spoof email purporting to come from the CEO to get them to mend their ways.
Upvotes: 2