Advertisement
Guest User

Untitled

a guest
Jun 20th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.46 KB | None | 0 0
  1. ///Инициализация
  2.  
  3. protected override void OnInit(HttpApplication application)
  4.         {
  5.             if (application == null)
  6.                 throw new ArgumentNullException("application");
  7.            
  8.             //
  9.             // Get the configuration section of this module.
  10.             // If it's not there then there is nothing to initialize or do.
  11.             // In this case, the module is as good as mute.
  12.             //
  13.  
  14.             IDictionary config = (IDictionary) GetConfig();
  15.  
  16.             if (config == null)
  17.                 return;
  18.  
  19.             //
  20.             // Extract the settings.
  21.             //
  22.  
  23.             string mailRecipient = GetSetting(config, "to");
  24.             string mailSender = GetSetting(config, "from", mailRecipient);
  25.             string mailCopyRecipient = GetSetting(config, "cc", string.Empty);
  26.             string mailSubjectFormat = GetSetting(config, "subject", string.Empty);
  27.             MailPriority mailPriority = (MailPriority) Enum.Parse(typeof(MailPriority), GetSetting(config, "priority", MailPriority.Normal.ToString()), true);
  28.             bool reportAsynchronously = Convert.ToBoolean(GetSetting(config, "async", bool.TrueString));
  29.             string smtpServer = GetSetting(config, "smtpServer", string.Empty);
  30.             int smtpPort = Convert.ToUInt16(GetSetting(config, "smtpPort", "0"), CultureInfo.InvariantCulture);
  31.             string authUserName = GetSetting(config, "userName", string.Empty);
  32.             string authPassword = GetSetting(config, "password", string.Empty);
  33.             bool sendYsod = Convert.ToBoolean(GetSetting(config, "noYsod", bool.FalseString));
  34. #if !NET_1_0 && !NET_1_1
  35.             bool useSsl = Convert.ToBoolean(GetSetting(config, "useSsl", bool.FalseString));
  36. #endif
  37.             //
  38.             // Hook into the Error event of the application.
  39.             //
  40.  
  41.             application.Error += new EventHandler(OnError);
  42.             ErrorSignal.Get(application).Raised += new ErrorSignalEventHandler(OnErrorSignaled);
  43.            
  44.             //
  45.             // Finally, commit the state of the module if we got this far.
  46.             // Anything beyond this point should not cause an exception.
  47.             //
  48.  
  49.             _mailRecipient = mailRecipient;
  50.             _mailSender = mailSender;
  51.             _mailCopyRecipient = mailCopyRecipient;
  52.             _mailSubjectFormat = mailSubjectFormat;
  53.             _mailPriority = mailPriority;
  54.             _reportAsynchronously = reportAsynchronously;
  55.             _smtpServer = smtpServer;
  56.             _smtpPort = smtpPort;
  57.             _authUserName = authUserName;
  58.             _authPassword = authPassword;
  59.             _noYsod = sendYsod;
  60. #if !NET_1_0 && !NET_1_1
  61.             _useSsl = useSsl;
  62. #endif
  63.         }
  64.  
  65.  
  66. //Посылка:
  67. SmtpClient client = new SmtpClient();
  68.  
  69.             string host = SmtpServer ?? string.Empty;
  70.  
  71.             if (host.Length > 0)
  72.             {
  73.                 client.Host = host;
  74.                 client.DeliveryMethod = SmtpDeliveryMethod.Network;
  75.             }
  76.  
  77.             int port = SmtpPort;
  78.             if (port > 0)
  79.                 client.Port = port;
  80.  
  81.             string userName = AuthUserName ?? string.Empty;
  82.             string password = AuthPassword ?? string.Empty;
  83.  
  84.             if (userName.Length > 0 && password.Length > 0)
  85.                 client.Credentials = new NetworkCredential(userName, password);
  86.  
  87.             client.EnableSsl = UseSsl;
  88.  
  89.             client.Send(mail);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement