11

I'm having an issue with a .NET 3.5 library I'm developing to send emails. I put the system.net configuration into app.config:

<system.net>
  <mailSettings>
    <smtp from="mail@domain.com">
      <network host="myserver.com" port="25" defaultCredentials="true" />
    </smtp>
  </mailSettings>
</system.net>

And I instantiate the SmtpClient without params:

SmtpClient client = new SmtpClient();

But the configuration is not read (I'm trying to test the library with NUnit) and I get a System.InvalidOperationException, because the configuration is not read and thus the host is null.

Shouldn't the configuration be read automatically?

Yuck
  • 49,664
  • 13
  • 105
  • 135
mamoo
  • 8,156
  • 2
  • 28
  • 38
  • 1
    Had a similar problem over 2 years ago: http://stackoverflow.com/questions/737946/net-configuration-section-designer-where-is-my-collection/738501#738501 – John Rasch Aug 30 '11 at 14:28
  • Thank you John. I had just came to the same conclusion... ;) I should have thought more about it definitely! – mamoo Aug 30 '11 at 14:33

2 Answers2

14

Make sure you add your configuration block (as shown above) to the {appName}.exe.config or web.config - the configuration for the class library is taken from one of those files at runtime, not from the app.config of the class library.

Chris B. Behrens
  • 6,255
  • 8
  • 45
  • 71
Davide Piras
  • 43,984
  • 10
  • 98
  • 147
  • Thanks for your answer Davide. It is a class library and I'm trying to test it with NUnit (does it make any difference?), anyway the section is present in the .config file so I'm still stuck... – mamoo Aug 30 '11 at 14:17
  • you are the most welcome, I gave similar answer to same question some weeks ago, it's a common error I also did in the past :) – Davide Piras Aug 30 '11 at 14:18
0

I've just realized I'm definitely doing it the wrong way (there's also a similar post from John about it and the solution is there...).

The mailsettings is available only at the application level, so the test project should know about your settings to take them into account.

Thanks to John and Davide for pointing me to the right direction!

Community
  • 1
  • 1
mamoo
  • 8,156
  • 2
  • 28
  • 38