'Unrecognized configuration section system.net

Hi i'll try to do the TournamentTracker and it works fine until when im stocked with the mailing lesson. Got problems in app.config when i add the lines: <system.net> and <mailsettings>

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="filePath" value="C:\Users\gertl\Source\Repos\TournamentTracker\TextData"/>
    <add key="greaterWins" value="1"/>
    <add key="senderEmail" value="[email protected] "/>
    <add key="senderDisplayName" value="TournamentTracker "/>    
  </appSettings>
  <connectionStrings>    
    <add name="Tournaments" connectionString="Server=xxx;Database=Tournaments;Trusted_Connection=True;" providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <system.net>
    <mailSettings>
      <smtp deliveryMethod="Network">
        <network host="127.0.0.1" userName="Tim" password="testing" port="25" enableSsl="false"/>        
      </smtp>
    </mailSettings>
  </system.net>
  <!--<startup>
    <supportedRuntime version="v4.0" sku=".NETFrameWork,Version=v4.5.2"/>
  </startup>-->
</configuration>

When i comment away the system.net section the connectionString works again.



Solution 1:[1]

See if the following helps.

  • Reads email settings
  • Reads connection string
  • Reads one AppSetting value

enter image description here

app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="mailSettings">
            <section name="smtp_1" type="System.Net.Configuration.SmtpSection"/>
            <section name="smtp_2" type="System.Net.Configuration.SmtpSection"/>
        </sectionGroup>
    </configSections>

    <appSettings>
        <add key="filePath" value="C:\Users\gertl\Source\Repos\TournamentTracker\TextData"/>
        <add key="greaterWins" value="1"/>
        <add key="senderEmail" value="[email protected] "/>
        <add key="senderDisplayName" value="TournamentTracker "/>
    </appSettings>

    <connectionStrings>
        <add name="Tournaments" connectionString="Server=xxx;Database=Tournaments;Trusted_Connection=True;" providerName="System.Data.SqlClient"/>
    </connectionStrings>

    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>

    <mailSettings>
        <smtp_1 from="[email protected]">
            <network
              host="smtp.gmail.com"
              port="587"
              enableSsl="true"
              userName="MssGMail"
              password="!@45cow"
              defaultCredentials="false" />
            <specifiedPickupDirectory pickupDirectoryLocation="MailDrop"/>
        </smtp_1>

        <smtp_2 from="[email protected]">
            <network
              host="smtp.gmail.com"
              port="587"
              enableSsl="true"
              userName="[email protected]"
              password="password"
              defaultCredentials="false" />
            <specifiedPickupDirectory pickupDirectoryLocation="MailDrop"/>
        </smtp_2>

    </mailSettings>

    <system.net>
        <mailSettings>
            <smtp  from="[email protected]">
                <network
                  host="smtp.comcast.net"
                  port="587"
                  enableSsl="true"
                  userName="MissComcast"
                  password="d@45cat"
                  defaultCredentials="true" />
                <specifiedPickupDirectory pickupDirectoryLocation="MailDrop"/>
            </smtp>
        </mailSettings>
    </system.net>

</configuration>

Class for getting email settings

using System;
using System.Configuration;
using System.IO;
using System.Net.Configuration;

namespace SmtpConfigurationExample.Classes
{
    public class MailConfiguration
    {
        private readonly SmtpSection _smtpSection;


        public MailConfiguration(string section = "system.net/mailSettings/smtp")
        {
            _smtpSection = (ConfigurationManager.GetSection(section) as SmtpSection);
        }

        public string FromAddress => _smtpSection.From;
        public string UserName => _smtpSection.Network.UserName;
        public string Password => _smtpSection.Network.Password;
        public bool DefaultCredentials => _smtpSection.Network.DefaultCredentials;
        public bool EnableSsl => _smtpSection.Network.EnableSsl;
        public string PickupFolder
        {
            get
            {
                var mailDrop = _smtpSection.SpecifiedPickupDirectory.PickupDirectoryLocation;

                if (mailDrop != null)
                {
                    mailDrop = Path.Combine(
                        AppDomain.CurrentDomain.BaseDirectory,
                        _smtpSection.SpecifiedPickupDirectory.PickupDirectoryLocation);
                }

                return mailDrop;
            }
        }
        /// <summary>
        /// Determine if pickup folder exists
        /// </summary>
        /// <returns></returns>
        public bool PickupFolderExists() => Directory.Exists(PickupFolder);

        /// <summary>
        /// Gets the name or IP address of the host used for SMTP transactions.
        /// </summary>
        public string Host => _smtpSection.Network.Host;

        /// <summary>
        /// Gets the port used for SMTP transactions
        /// </summary>
        public int Port => _smtpSection.Network.Port;

        /// <summary>
        /// Gets a value that specifies the amount of time after 
        /// which a synchronous Send call times out.
        /// </summary>
        public int TimeOut => 2000;
        public override string ToString() => $"From: [ {FromAddress} ]" +
                                             $"Host: [{Host}] Port: [{Port}] " +
                                             $"Pickup: {Directory.Exists(PickupFolder)}";
    }
}

Form code

using System;
using System.Text;
using System.Windows.Forms;
using SmtpConfigurationExample.Classes;
using static System.Configuration.ConfigurationManager;

namespace SmtpConfigurationExample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void GetEmailSettingsButton_Click(object sender, EventArgs e)
        {
            var mc = new MailConfiguration();
            var builder = new StringBuilder();

            builder.AppendLine($"User name: {mc.UserName}");
            builder.AppendLine($"From: {mc.FromAddress}");
            builder.AppendLine($"Host: {mc.Host}");
            builder.AppendLine($"Port: {mc.Port}");

            ResultsTextBox.Text = builder.ToString();

        }

        private void GetConnectionButton_Click(object sender, EventArgs e)
        {
            ResultsTextBox.Text = ConnectionStrings["Tournaments"].ConnectionString;
        }

        private void FilePathButton_Click(object sender, EventArgs e)
        {
            ResultsTextBox.Text = AppSettings["filePath"];
        }
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Karen Payne