Unfortunately it is not true in this case. When I take the following code out -
ConfigurationManager.AppSettings["network"] -
I get this error - The SMTP host was not specified.
when I use your 2 lines of code. There is no good example on the web on how to read Mail Settings out of an app.config file rather then a web.config file.
I’m relatively new to this. Please forgive me for asking you one more time. I have a Class Library (Email Component) and therefore no web.config file.
I guess I could use the <appSettings> in the app.config file and add key and value pairs for smtp host, username, password etc. and try and read the data like that. I would like to use the Microsoft <system.net><mailSettings>
approach with a app.config file. I have tried so many ways. The latest which is your sample but it does not like the following line:
Bishi
Member
310 Points
62 Posts
How to call settings from app.config in C# code..............................
Mar 25, 2006 03:22 AM|LINK
<configuration>
<system.net>
<mailSettings>
<smtp from="user@user.com">
<network host="smtp.server.com" password="password" userName="username" />
</smtp>
</mailSettings>
</system.net>
</configuration>
How can I call the "host"setting from the App.config file in my C# code?
I have tried this but it does not work.
SmtpClient mailClient = new SmtpClient(ConfigurationManager.AppSettings["network"]);
StrongTypes
All-Star
30801 Points
6013 Posts
ASPInsiders
Re: How to call settings from app.config in C# code..............................
Mar 25, 2006 04:55 AM|LINK
I know that if you set it in Web.config, there is no need to call it from your code. I assume the same is true for app.config. Is this not the case?
HTH,
Ryan
Bishi
Member
310 Points
62 Posts
Re: How to call settings from app.config in C# code..............................
Mar 25, 2006 04:24 PM|LINK
Hi Ryan,
Unfortunately it is not true in this case. When I take the following code out - ConfigurationManager.AppSettings["network"] - I get this error - The SMTP host was not specified.
Any ideas?
Patrick
Bishi
Member
310 Points
62 Posts
Re: How to call settings from app.config in C# code..............................
Mar 25, 2006 04:42 PM|LINK
Do I have to write a custom config section handler? Isn't there an easier way to read a value out of an app.config file using C#?
Patrick
StrongTypes
All-Star
30801 Points
6013 Posts
ASPInsiders
Re: How to call settings from app.config in C# code..............................
Mar 25, 2006 04:48 PM|LINK
With .NET 2.0, you can get the value using a FCL API.
System.Net.Configuration.MailSettingsSectionGroup mMailSettings;
string mMailHost = mMailSettings.Smtp.Network.Host;
Let me know if this works.
Ryan
Bishi
Member
310 Points
62 Posts
Re: How to call settings from app.config in C# code..............................
Mar 27, 2006 05:58 PM|LINK
Hi Ryan,
I can’t get it to work. I get the following error message
Error 3 Use of unassigned local variable 'mMailSettings
System.Net.Configuration.MailSettingsSectionGroup mMailSettings;
string mMailHost = mMailSettings.Smtp.Network.Host;
when I use your 2 lines of code. There is no good example on the web on how to read Mail Settings out of an app.config file rather then a web.config file.
Any more ideas?
Thanks,
Patrick
StrongTypes
All-Star
30801 Points
6013 Posts
ASPInsiders
Re: How to call settings from app.config in C# code..............................
Mar 27, 2006 06:05 PM|LINK
You'll need to assign a value to mMailSettings when you declare it.
System.Net.Configuration.MailSettingsSectionGroup mMailSettings = null;
HTH,
Ryan
Bishi
Member
310 Points
62 Posts
Re: How to call settings from app.config in C# code..............................
Mar 27, 2006 09:14 PM|LINK
I have tried that but it still does not work. I get the following error message:
Object reference not set to an instance of an object.
The code failes on line 2.
System.Net.Configuration.MailSettingsSectionGroup mMailSettings = null;
string mMailHost = mMailSettings.Smtp.Network.Host;
//Allows applications to send e-mail by using the Simple Mail Transfer Protocol (SMTP).
SmtpClient mailClient = new SmtpClient();
//Sends an e-mail message to an SMTP server for delivery.
mailClient.Send(EmailMessage.CreateEmailMessage(sender, recipient, subject, body));
Thanks,
Patrick
StrongTypes
All-Star
30801 Points
6013 Posts
ASPInsiders
Re: How to call settings from app.config in C# code..............................
Mar 27, 2006 09:35 PM|LINK
I was using null as a placeholder. This is along the lines of what you need to do: Accessing Configuration Mail Settings Programatically.
HTH,
Ryan
Bishi
Member
310 Points
62 Posts
Re: How to call settings from app.config in C# code..............................
Mar 29, 2006 07:28 PM|LINK
I’m relatively new to this. Please forgive me for asking you one more time. I have a Class Library (Email Component) and therefore no web.config file.
I guess I could use the <appSettings> in the app.config file and add key and value pairs for smtp host, username, password etc. and try and read the data like that. I would like to use the Microsoft <system.net><mailSettings> approach with a app.config file. I have tried so many ways. The latest which is your sample but it does not like the following line:
System.Web.Configuration mConfigurationFile = WebConfigurationManager.OpenWebConfiguration("D:\\Projects\\EmailSolution\\Email\\App.config");
Here is the code for the mailer class:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Web;
using System.Web.Configuration;
using System.Net.Configuration;
namespace Email
{
/// <summary>
/// Mailer is used to send emails.
/// </summary>
public class Mailer
{
/// <summary>
/// Sends email message.
/// </summary>
/// <param name="sender"></param>
/// <param name="recipient"></param>
/// <param name="subject"></param>
/// <param name="body"></param>
/// <returns>true or false</returns>
public static bool SendEmail(string sender, string recipient, string subject, string body)
{
try
{
System.Web.Configuration mConfigurationFile = WebConfigurationManager.OpenWebConfiguration("D:\\Projects\\EmailSolution\\Email\\App.config");
MailSettingsSectionGroup mMailSettings = mConfigurationFile.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;
if (mMailSettings != null)
{
int mPort = mMailSettings.Smtp.Network.Port;
string mHost = mMailSettings.Smtp.Network.Host;
string mPassword = mMailSettings.Smtp.Network.Password;
string mUsername = mMailSettings.Smtp.Network.UserName;
}
//Allows applications to send e-mail by using the Simple Mail Transfer Protocol (SMTP).
SmtpClient mailClient = new SmtpClient(mHost);
//Sends an e-mail message to an SMTP server for delivery.
mailClient.Send(EmailMessage.CreateEmailMessage(sender, recipient, subject, body));
return true;
}
catch (FormatException ex)
{
return false;
}
catch (SmtpException ex)
{
return false;
}
}
}
}