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

  1. //------------------------------------------------------------------------------
  2. // <copyright file="SmtpNetworkElement.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.Net;
  20.     using System.Net.Mail;
  21.     using System.Reflection;
  22.     using System.Security.Permissions;
  23.    
  24.     public sealed class SmtpNetworkElement : ConfigurationElement
  25.     {
  26.         public SmtpNetworkElement()
  27.         {
  28.             this.properties.Add(this.defaultCredentials);
  29.             this.properties.Add(this.host);
  30.             this.properties.Add(this.password);
  31.             this.properties.Add(this.port);
  32.             this.properties.Add(this.userName);
  33.         }
  34.        
  35.         protected override void PostDeserialize()
  36.         {
  37.             // Perf optimization. If the configuration is coming from machine.config
  38.             // It is safe and we don't need to check for permissions.
  39.             if (EvaluationContext.IsMachineLevel)
  40.                 return;
  41.            
  42.             PropertyInformation portPropertyInfo = ElementInformation.Properties[ConfigurationStrings.Port];
  43.            
  44.             if (portPropertyInfo.ValueOrigin == PropertyValueOrigin.SetHere && (int)portPropertyInfo.Value != (int)portPropertyInfo.DefaultValue) {
  45.                 try {
  46.                     (new SmtpPermission(SmtpAccess.ConnectToUnrestrictedPort)).Demand();
  47.                 }
  48.                 catch (Exception exception) {
  49.                     throw new ConfigurationErrorsException(SR.GetString(SR.net_config_property_permission, portPropertyInfo.Name), exception);
  50.                 }
  51.             }
  52.         }
  53.        
  54.         protected override ConfigurationPropertyCollection Properties {
  55.             get { return this.properties; }
  56.         }
  57.        
  58.         [ConfigurationProperty(ConfigurationStrings.DefaultCredentials, DefaultValue = false)]
  59.         public bool DefaultCredentials {
  60.             get { return (bool)this[this.defaultCredentials]; }
  61.             set { this[this.defaultCredentials] = value; }
  62.         }
  63.        
  64.         [ConfigurationProperty(ConfigurationStrings.Host)]
  65.         public string Host {
  66.             get { return (string)this[this.host]; }
  67.             set { this[this.host] = value; }
  68.         }
  69.        
  70.         [ConfigurationProperty(ConfigurationStrings.Password)]
  71.         public string Password {
  72.             get { return (string)this[this.password]; }
  73.             set { this[this.password] = value; }
  74.         }
  75.        
  76.         [ConfigurationProperty(ConfigurationStrings.Port, DefaultValue = 25)]
  77.         public int Port {
  78.             get { return (int)this[this.port]; }
  79.             set { this[this.port] = value; }
  80.         }
  81.        
  82.         [ConfigurationProperty(ConfigurationStrings.UserName)]
  83.         public string UserName {
  84.             get { return (string)this[this.userName]; }
  85.             set { this[this.userName] = value; }
  86.         }
  87.        
  88.        
  89.        
  90.         ConfigurationPropertyCollection properties = new ConfigurationPropertyCollection();
  91.        
  92.         readonly ConfigurationProperty defaultCredentials = new ConfigurationProperty(ConfigurationStrings.DefaultCredentials, typeof(bool), false, ConfigurationPropertyOptions.None);
  93.        
  94.         readonly ConfigurationProperty host = new ConfigurationProperty(ConfigurationStrings.Host, typeof(string), null, ConfigurationPropertyOptions.None);
  95.        
  96.         readonly ConfigurationProperty password = new ConfigurationProperty(ConfigurationStrings.Password, typeof(string), null, ConfigurationPropertyOptions.None);
  97.        
  98.         readonly ConfigurationProperty port = new ConfigurationProperty(ConfigurationStrings.Port, typeof(int), 25, null, new IntegerValidator(IPEndPoint.MinPort + 1, IPEndPoint.MaxPort), ConfigurationPropertyOptions.None);
  99.        
  100.         readonly ConfigurationProperty userName = new ConfigurationProperty(ConfigurationStrings.UserName, typeof(string), null, ConfigurationPropertyOptions.None);
  101.        
  102.     }
  103.    
  104.     internal sealed class SmtpNetworkElementInternal
  105.     {
  106.         internal SmtpNetworkElementInternal(SmtpNetworkElement element)
  107.         {
  108.             this.host = element.Host;
  109.             this.port = element.Port;
  110.            
  111.             if (element.DefaultCredentials) {
  112.                 this.credential = (NetworkCredential)CredentialCache.DefaultCredentials;
  113.             }
  114.             else if (element.UserName != null && element.UserName.Length > 0) {
  115.                 this.credential = new NetworkCredential(element.UserName, element.Password);
  116.             }
  117.            
  118.         }
  119.        
  120.         internal NetworkCredential Credential {
  121.             get { return this.credential; }
  122.         }
  123.        
  124.         internal string Host {
  125.             get { return this.host; }
  126.         }
  127.        
  128.         internal int Port {
  129.             get { return this.port; }
  130.         }
  131.        
  132.         string host;
  133.         int port;
  134.         NetworkCredential credential = null;
  135.     }
  136. }

Developer Fusion