The Labs \ Source Viewer \ SSCLI \ System.Net.Configuration \ SmtpSection

  1. //------------------------------------------------------------------------------
  2. // <copyright file="SmtpSection.cs" company="Microsoft Corporation">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. namespace System.Net.Configuration
  16. {
  17.     using System;
  18.     using System.Configuration;
  19.     using System.ComponentModel;
  20.     using System.Globalization;
  21.     using System.Net;
  22.     using System.Net.Mail;
  23.     using System.Reflection;
  24.     using System.Threading;
  25.    
  26.     public sealed class SmtpSection : ConfigurationSection
  27.     {
  28.         public SmtpSection()
  29.         {
  30.             this.properties.Add(this.deliveryMethod);
  31.             this.properties.Add(this.from);
  32.            
  33.             this.properties.Add(this.network);
  34.             this.properties.Add(this.specifiedPickupDirectory);
  35.         }
  36.        
  37.         [ConfigurationProperty(ConfigurationStrings.DeliveryMethod, DefaultValue = (SmtpDeliveryMethod)SmtpDeliveryMethod.Network)]
  38.         public SmtpDeliveryMethod DeliveryMethod {
  39.             get { return (SmtpDeliveryMethod)this[this.deliveryMethod]; }
  40.             set { this[this.deliveryMethod] = value; }
  41.         }
  42.        
  43.         [ConfigurationProperty(ConfigurationStrings.From)]
  44.         public string From {
  45.             get { return (string)this[this.from]; }
  46.             set { this[this.from] = value; }
  47.         }
  48.        
  49.        
  50.         [ConfigurationProperty(ConfigurationStrings.Network)]
  51.         public SmtpNetworkElement Network {
  52.             get { return (SmtpNetworkElement)this[this.network]; }
  53.         }
  54.        
  55.         [ConfigurationProperty(ConfigurationStrings.SpecifiedPickupDirectory)]
  56.         public SmtpSpecifiedPickupDirectoryElement SpecifiedPickupDirectory {
  57.             get { return (SmtpSpecifiedPickupDirectoryElement)this[this.specifiedPickupDirectory]; }
  58.         }
  59.        
  60.         protected override ConfigurationPropertyCollection Properties {
  61.             get { return this.properties; }
  62.         }
  63.        
  64.        
  65.        
  66.         ConfigurationPropertyCollection properties = new ConfigurationPropertyCollection();
  67.        
  68.         readonly ConfigurationProperty from = new ConfigurationProperty(ConfigurationStrings.From, typeof(string), null, ConfigurationPropertyOptions.None);
  69.        
  70.         readonly ConfigurationProperty network = new ConfigurationProperty(ConfigurationStrings.Network, typeof(SmtpNetworkElement), null, ConfigurationPropertyOptions.None);
  71.        
  72.         readonly ConfigurationProperty specifiedPickupDirectory = new ConfigurationProperty(ConfigurationStrings.SpecifiedPickupDirectory, typeof(SmtpSpecifiedPickupDirectoryElement), null, ConfigurationPropertyOptions.None);
  73.        
  74.         readonly ConfigurationProperty deliveryMethod = new ConfigurationProperty(ConfigurationStrings.DeliveryMethod, typeof(SmtpDeliveryMethod), SmtpDeliveryMethod.Network, new SmtpDeliveryMethodTypeConverter(), null, ConfigurationPropertyOptions.None);
  75.        
  76.         class SmtpDeliveryMethodTypeConverter : TypeConverter
  77.         {
  78.             public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  79.             {
  80.                 if (sourceType == typeof(string)) {
  81.                     return true;
  82.                 }
  83.                 return base.CanConvertFrom(context, sourceType);
  84.             }
  85.            
  86.             public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  87.             {
  88.                 string s = value as string;
  89.                 if (s != null) {
  90.                     s = s.ToLower(CultureInfo.InvariantCulture);
  91.                     switch (s) {
  92.                         case "network":
  93.                             return SmtpDeliveryMethod.Network;
  94.                         case "specifiedpickupdirectory":
  95.                             return SmtpDeliveryMethod.SpecifiedPickupDirectory;
  96.                     }
  97.                 }
  98.                
  99.                 return base.ConvertFrom(context, culture, value);
  100.             }
  101.         }
  102.     }
  103.    
  104.     internal sealed class SmtpSectionInternal
  105.     {
  106.         internal SmtpSectionInternal(SmtpSection section)
  107.         {
  108.             this.deliveryMethod = section.DeliveryMethod;
  109.             this.from = section.From;
  110.            
  111.             this.network = new SmtpNetworkElementInternal(section.Network);
  112.             this.specifiedPickupDirectory = new SmtpSpecifiedPickupDirectoryElementInternal(section.SpecifiedPickupDirectory);
  113.         }
  114.        
  115.         internal SmtpDeliveryMethod DeliveryMethod {
  116.             get { return this.deliveryMethod; }
  117.         }
  118.        
  119.         internal SmtpNetworkElementInternal Network {
  120.             get { return this.network; }
  121.         }
  122.        
  123.         internal string From {
  124.             get { return this.from; }
  125.         }
  126.        
  127.         internal SmtpSpecifiedPickupDirectoryElementInternal SpecifiedPickupDirectory {
  128.             get { return this.specifiedPickupDirectory; }
  129.         }
  130.        
  131.         SmtpDeliveryMethod deliveryMethod;
  132.         string from = null;
  133.         SmtpNetworkElementInternal network = null;
  134.         SmtpSpecifiedPickupDirectoryElementInternal specifiedPickupDirectory = null;
  135.        
  136.         static internal object ClassSyncObject {
  137.             get {
  138.                 if (classSyncObject == null) {
  139.                     Interlocked.CompareExchange(ref classSyncObject, new object(), null);
  140.                 }
  141.                 return classSyncObject;
  142.             }
  143.         }
  144.        
  145.         static internal SmtpSectionInternal GetSection()
  146.         {
  147.             lock (ClassSyncObject) {
  148.                 SmtpSection section = PrivilegedConfigurationManager.GetSection(ConfigurationStrings.SmtpSectionPath) as SmtpSection;
  149.                 if (section == null)
  150.                     return null;
  151.                
  152.                 return new SmtpSectionInternal(section);
  153.             }
  154.         }
  155.        
  156.         private static object classSyncObject;
  157.     }
  158. }

Developer Fusion