3

Hello we have a Business Logic Layer which has an Email Services Class. In this class we have a method which will create an email(This part works and compiles fine). However when we try to access the app config file in order to test the method we get an error saying - Can't retrieve the app config mail settings and says all values are null when they are not. Here is the app config section for our code:


<mailSettings>
  <smtp deliveryMethod="Network" from="info@example.com">
    <network host="localhost" port="25" defaultCredentials="true"/>
  </smtp>
</mailSettings>

Here is there code we use to connect to the app.config:


private System.Net.Configuration.MailSettingsSectionGroup mailSettings;

SmtpClient client = new SmtpClient(mailSettings.Smtp.Network.Host, mailSettings.Smtp.Network.Port);

What are we doing wrong here?

musefan
  • 47,875
  • 21
  • 135
  • 185
mw.
  • 43
  • 1
  • 5
  • the app config does not show. Please indent it (and the code as well) with 4 spaces – Marek Apr 13 '10 at 10:24
  • @Marek: I've edited the question with correct indents/code outline. – Andy Shellam Apr 13 '10 at 10:29
  • 2
    Are the mailsettings section within the system.net tag? Why do you need to access them directly? The default constructor SmtpClient() should pick them up automatically – adrianm Apr 13 '10 at 10:36
  • Yes, it is in the section within the system.net tag – mw. Apr 13 '10 at 10:41
  • We have a solution with a number of projects 1 of which is the Business Logic Layer with its own App Config File. The Business Logic Layer fails to pick it up automatically – mw. Apr 13 '10 at 10:45

3 Answers3

6

Your mailSettings variable isn't initialised to anything - it won't magically contain your configuration.

You need to use the ConfigurationManager class to access it (remember to add a reference to System.Configuration if not already done so.) You will also need to add using System.Net.Configuration for the following code.

SmtpSection smtpSection = ConfigurationManager.GetSection("system.net/mailSettings/smtp") as SmtpSection;

if (smtpSection != null)
{
    SmtpClient client = new SmtpClient(smtpSection.Network.Host, smtpSection.Network.Port);
}
Andy Shellam
  • 15,403
  • 1
  • 27
  • 41
1
SmtpSection smtpSection = ConfigurationManager.GetSection("system.net/mailSettings/smtp") as SmtpSection;
if (smtpSection != null) {
    SmtpClient client = new SmtpClient(smtpSection.Network.Host, smtpSection.Network.Port);
} 
Tisho
  • 8,320
  • 6
  • 44
  • 52
vinu
  • 21
  • 1
1

<mailSettings>

  <smtp>

    <network host="smtp.mailserver.com" password="password" userName="username"/>

  </smtp>

</mailSettings>

Then public static bool SendEmail(string sender, string recipient, string subject, string body)

    {

        try

        {

            Configuration mConfigurationFile = ConfigurationManager.OpenExeConfiguration("D:\\Projects\\EmailSolution\\Email\\App.config");

            MailSettingsSectionGroup mMailSettings = mConfigurationFile.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;



            string mHost = string.Empty;



            if (mMailSettings != null)

            {

                //int mPort = mMailSettings.Smtp.Network.Port;

                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;

        }
vinu
  • 21
  • 1