0

I am working on Forgot Password Functionality. In my web.config file I have done the mail settings:

<system.net>
    <mailSettings>
      <smtp from="email">
        <network host="host" port="25" userName="" password="=" enableSsl="true" />
      </smtp>
    </mailSettings>
</system.net>

In my SendAsync method I am trying to read setting from web.config:

SmtpClient client = new SmtpClient();
return client.SendMailAsync(ConfigurationManager.AppSettings["SupportEmailAddr"],
                                    message.Destination,
                                    message.Subject,
                                    message.Body);

I have no idea what is this: AppSettings["SupportEmailAddr"]

I took this from here.

It is giving me following exception:

Value cannot be null. Parameter name: from

M Armaan
  • 39
  • 8

1 Answers1

1

In your web.config file you have a section called: <appSettings>.

That is what ConfigurationManager.AppSettings is referring too.

["SupportEmailAddr"] is looking at a specific setting called SupportEmailAddr.

In your web.config it would look something like this:

<appSettings>
    <add key="SupportEmailAddr" value="someone@example.com" />
</appSettings>

You are getting the value cannot be null message because you will not have the setting in your web.config as above.

So to fix the error message find your <appSettings> and add:

<add key="SupportEmailAddr" value="someone@example.com" />

Alternatively, if you have the current value in your AppSettings already then just change the key that you are looking for in the C# code.

ConfigurationManager.AppSettings["CorrectAppSettingKey"]

Note: if you plan on using any of the web.config inheritance features you should WebConfiguratonManger.AppSettings instead of ConfigurationManager.AppSettings. See the difference between the two here: What's the difference between the WebConfigurationManager and the ConfigurationManager?

Ashley Medway
  • 7,151
  • 7
  • 49
  • 71
  • So I should add new key in my config file with SupportEmailAddr. Leave other setting as they are. – M Armaan Dec 01 '17 at 11:57
  • @MHussain Yes, unless you have the correct setting already there. If you already have a setting you want to use. Change `SupportEmailAddr` in `ConfigurationManager.AppSettings["SupportEmailAddr"]` to the key you want to use. e.g. `ConfigurationManager.AppSettings["MyOtherKey"]` – Ashley Medway Dec 01 '17 at 11:59
  • I am getting reset email now. But when I am clicking on link getting following error: `A potentially dangerous Request.QueryString value was detected from the client (code="...QQ==">here – M Armaan Dec 01 '17 at 12:07
  • @MHussain Best to create a new question for that one :) – Ashley Medway Dec 01 '17 at 12:13
  • I found solution. My Bad. – M Armaan Dec 01 '17 at 12:15