DEH
DEH

Reputation: 1747

C# Read <system.net><mailSettings> in web.config from external dll

My web app calls an external dll. Within the dll I want to access the specifiedPickupDirectory pickupDirectoryLocation value within the system.net/mailSettings/smtp section. How can I grab it from within the dll code?

Something like

System.Configuration.ConfigurationSettings.GetConfig("configuration/system.net/mailSettings/smtp/specifiedPickupDirectory/pickupDirectoryLocation")

but that doesn't work

Upvotes: 7

Views: 7701

Answers (3)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039130

I guess you could simply use the PickupDirectoryLocation property.

// if .NET 4.0 don't forget that SmtpClient is IDisposable
SmtpClient client = new SmtpClient();
string pickupLocation = client.PickupDirectoryLocation;

This way you are not using magic strings in your code and it makes one less thing to worry about if in future versions of the framework this attribute changes name or location in the configuration file.

Upvotes: 6

matt-dot-net
matt-dot-net

Reputation: 4244

use this:

using System.Configuration;
using System.Web.Configuration;
using System.Net.Configuration;

then:

Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
MailSettingsSectionGroup settings = (MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");

then you'll have access to

//settings.Smtp.SpecifiedPickupDirectory;

Of course this should also be found in the System.Net.Mail.SmtpClient.PickupDirectoryLocation property

Upvotes: 0

Matthew Abbott
Matthew Abbott

Reputation: 61599

You can use:

public string GetPickupDirectory()
{
    var config = ConfigurationManager.GetSection("system.net/mailSettings/smtp") as SmtpSection;

    return (config != null) ? config.SpecifiedPickupDirectory : null;
}

Upvotes: 17

Related Questions