title: Reading system.net/mailSettings/smtp from Web.config in Medium trust environment
body: <p>I have some inherited code which stores SMTP server, username, password in the <code>system.net/mailSettings/smtp</code> section of the Web.config.</p>
  
  <p>It used to read them like so:</p>
  
  <pre><code>Configuration c = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
  MailSettingsSectionGroup settings = (MailSettingsSectionGroup)c.GetSectionGroup("system.net/mailSettings");
  return settings.Smtp.Network.Host;
  </code></pre>
  
  <p>But this was failing when I had to deploy to a medium trust environment.</p>
  
  <p>So following the answer from <a href="https://stackoverflow.com/questions/3927343/asp-net-security-exception-with-openwebconfiguration-on-shared-host">this question</a>, I rewrote it to use <code>GetSection()</code> like so:</p>
  
  <pre><code>SmtpSection settings = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
  return settings.Network.Host;
  </code></pre>
  
  <p>But it's still giving me a SecurityException on Medium trust, with the following message:</p>
  
  <blockquote>
    <p>Request for ConfigurationPermission
    failed while attempting to access
    configuration section
    'system.net/mailSettings/smtp'. To
    allow all callers to access the data
    for this section, set section
    attribute 'requirePermission' equal
    'false' in the configuration file
    where this section is declared.</p>
  </blockquote>
  
  <p>So I tried this <code>requirePermission</code> attribute, but can't figure out where to put it.</p>
  
  <p>If I apply it to the &lt;smtp&gt; node, I get a ConfigurationError: "Unrecognized attribute 'requirePermission'. Note that attribute names are case-sensitive."</p>
  
  <p>If I apply it to the &lt;mailSettings&gt; node, I still get the SecurityException.</p>
  
  <p>Is there any way to get at this config section programatically under medium trust? Or should I just give up on it and move the setting into &lt;appSettings&gt;?</p>