Reading the configuration settings in ASP.NET

This does not seem to work in ASP.NET:

SmtpClient client = new SmtpClient(ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None));

Why don't you just get the configuration out of System.Net.Mail.SmtpClient?

Cheers

Remy

Hi Remy,


Thank you for considering Aspose.

As you may know that there are multiple ways to extract information from configuration files (app.config/web.config). The idea is to minimize the source code lines to access the Mail Settings from configuration file and pass it to the SmptClient.

Regarding your original query, I am working on it to reproduce the problem. Meanwhile, can you please share your operating system specifications and what error are you receiving. Also, it would be of great value if you provide us your sample ASP.Net application for our review.

Ref Article

Regards,

System.ArgumentException: exePath must be specified when not running inside a stand alone exe.
at System.Configuration.ConfigurationManager.OpenExeConfigurationImpl(ConfigurationFileMap fileMap, Boolean isMachine, ConfigurationUserLevel userLevel, String exePath)
at System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel userLevel)

I figured there might be another settings manager for asp.net? Like the WebConfigurationManager?

I've got Win7 with the normal IIS and ASP.NET 4.0

Hi,

Thank you for further details.

I am able to reproduce exactly the same error on my end [Windows 7 64bit]. But I have also noticed, this error has nothing to do with Aspose.Email for .NET assembly. If you execute only the below source code line without utilizing any object from Aspose.Email Namespace, still you get the same error. I am looking into it further for better answers.

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

Meanwhile, I have a workaround for this problem. Please check the below source code and attached sample application for your reference. I have removed the Aspose.Email.dll file to decrease the archive size.

Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);

MailSettingsSectionGroup settings = (MailSettingsSectionGroup)config.GetSectionGroup(“system.net/mailSettings 1”);

SmtpSection smtp = settings.Smtp;

SmtpNetworkElement network = smtp.Network;


SmtpClient client = new SmtpClient(network.Host, network.Port, network.UserName, network.Password);

Hi,


Thank you for further details.

I am able to reproduce exactly the same error on my end [Windows 7 64bit]. But I have also noticed, this error has nothing to do with Aspose.Email for .NET assembly. If you execute only the below source code line without utilizing any object from Aspose.Email Namespace, still you get the same error. I am looking into it further for better answers.

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

Meanwhile, I have a workaround for this problem. Please check the below source code and attached sample application for your reference. I have removed the Aspose.Email.dll file to decrease the archive size.


Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);

MailSettingsSectionGroup settings = (MailSettingsSectionGroup)config.GetSectionGroup(“system.net/mailSettings”);

SmtpSection smtp = settings.Smtp;

SmtpNetworkElement network = smtp.Network;


SmtpClient client = new SmtpClient(network.Host, network.Port, network.UserName, network.Password);


Thanks, we’ve used something similar with your old Network Library. We also use the SpecifiedPickupDirectory settings. I just figured it would be nice to automatically set all the settings at once.


Has this been addressed yet? Your workaround is certainly a lot more verbose than necessary, since in Asp.Net you can normally use System.Net.Mail.SmtpClient.

Hi Charles,


You are right. I will check back with the development team and post the latest updates here for your kind reference.

Sorry for your inconvenience.

Regards,

Hi Charles,


Just to notify you that I am unable to observe the issue discussed in this post by using the latest version of Aspose.Email for .NET v1.9.0. Please modify the code provided in sample application with the below one and try it at your end,

C#

Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
SmtpClient client = new SmtpClient(config);
MailMessage message = new MailMessage();
message.From = “sender@domain.com”;
message.To = “receiver@aspose.com”;
message.HtmlBody = “Hello there”;
client.EnableSsl = true;
client.Send(message);

Regards,

Thanks, I’ll try that as soon as I get a chance. However, I believe we are using 1.9, and when I tried that I got an exception on the “OpenWebConfiguration” method. I’ll try it again just to be sure.