22

I have some inherited code which stores SMTP server, username, password in the system.net/mailSettings/smtp section of the Web.config.

It used to read them like so:

Configuration c = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
MailSettingsSectionGroup settings = (MailSettingsSectionGroup)c.GetSectionGroup("system.net/mailSettings");
return settings.Smtp.Network.Host;

But this was failing when I had to deploy to a medium trust environment.

So following the answer from this question, I rewrote it to use GetSection() like so:

SmtpSection settings = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
return settings.Network.Host;

But it's still giving me a SecurityException on Medium trust, with the following message:

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.

So I tried this requirePermission attribute, but can't figure out where to put it.

If I apply it to the <smtp> node, I get a ConfigurationError: "Unrecognized attribute 'requirePermission'. Note that attribute names are case-sensitive."

If I apply it to the <mailSettings> node, I still get the SecurityException.

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 <appSettings>?

Community
  • 1
  • 1
Carson63000
  • 4,215
  • 2
  • 24
  • 38
  • 4
    Whenever I had that setting I didn't need to "read it". Just creating a new SmtpClient() would use the settings on the web.config. – turtlepick Jan 05 '11 at 02:39

5 Answers5

27

The requirePemission attribute goes on the <configSections> grouping that matches the part fo the web.config you are having the security issue with.

additionally, you don't have to actually read the settings using code to send mail - you can simply use a blank SmtpClient:

 new SmtpClient.Send(MyMailMessage);

it will send using the settings from the config sections by default.

Doug
  • 6,460
  • 5
  • 59
  • 83
  • 8
    Heh, just using `new SmtpClient()` rather than trying to manually extract the host and port and username and password works perfectly! – Carson63000 Jan 05 '11 at 03:30
26

You can create a SmtpClient as some suggested, but that is a bit overkill - just read the sections directly.

var section = ConfigurationManager.GetSection("system.net/mailSettings/smtp") as SmtpSection;
var host=section.Network.Host
Wayne Brantley
  • 694
  • 6
  • 11
  • I've followed this whole process, but getting null values all time? What am I missing? – ruud Oct 18 '16 at 14:29
4

This works very well to me.

var smtp = new System.Net.Mail.SmtpClient();
var host = smtp.Host;
var ssl = smtp.EnableSsl;
var port = smtp.Port;

var credential = new System.Net.Configuration.SmtpSection().Network;
var username = credential.UserName;
var password = credential.Password;
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Gerald
  • 41
  • 2
2

To get the settings from the mail sections just create the mail objects.

var client = new SmtpClient();
var messageSettings = new MailMessage();

var host=client.Host;
//etc...

var fromAddress=messageSettings.From.Address;
//etc..

Config:

  <system.net>
    <mailSettings>
      <smtp from="xxxx@yahoo.com" deliveryMethod="Network" >
        <network host="smtp.mail.yahoo.com" port="587" enableSsl="true"
            userName="xxxx@yahoo.com" password="xxxxxxx"/>
      </smtp>     
    </mailSettings>
  </system.net>
Jeroen Doppenberg
  • 1,558
  • 1
  • 10
  • 13
2

Joys of coding eh... always 1000 ways to skin a fish

System.Net.Configuration.SmtpSection smtp = new System.Net.Configuration.SmtpSection();
string from = smtp.From;
//etc
System.Net.Configuration.SmtpNetworkElement nt = new System.Net.Configuration.SmtpNetworkElement();
string host = nt.Host;
//etc