public static bool Load() { if (_section == null) { lock (_lock) { if (_section == null) { //Configuration exeConfig = ConfigurationManager.OpenExeConfiguration(Environment.GetCommandLineArgs()[0]); Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); var section = (ReviewNotifierConfiguration)config.GetSection(_sectionName); if (section == null) { section = new ReviewNotifierConfiguration(); section.SectionInformation.AllowExeDefinition = ConfigurationAllowExeDefinition.MachineToApplication; section.SectionInformation.AllowLocation = false; } _config = config; _section = section; } } } return _section != null; }
/// <summary> /// Sends mail through SMTP server. /// </summary> /// <param name="Config"> ReviewNotifierConfiguration. </param> /// <param name="SmtpItems"> Mail to send. </param> /// <returns> true if successful. </returns> private static bool SendSmtpMail(ReviewNotifierConfiguration Config, List<MailMessage> SmtpItems) { SmtpClient client = new SmtpClient(Config.SmtpServer); if (Config.Password == null) client.UseDefaultCredentials = true; else client.Credentials = new NetworkCredential(Config.User, Config.Password, Config.Domain); if (Config.UseSsl) client.EnableSsl = true; foreach (MailMessage email in SmtpItems) client.Send(email); return true; }
/// <summary> /// Resolves the email address from the user name, performing LDAP query if necessary. /// </summary> /// <param name="Config"></param> /// <param name="userName"></param> /// <returns>User's email address.</returns> private static string ResolveUser(ReviewNotifierConfiguration Config, string userName) { if (!Config.UseLdap) return userName + "@" + Config.EmailDomain; string email; if (emailDictionary.TryGetValue(userName, out email)) return email; email = RetrieveLdapProperty("mail", userName); if (email != null) { emailDictionary[userName] = email; } else { email = userName + "@" + Config.EmailDomain; Console.Error.WriteLine("Failed ldap lookup for {0}. Using {1}.", userName, email); } return email; }
/// <summary> /// Sends mail through the exchange server. /// </summary> /// <param name="Config"> ReviewNotifierConfiguration. </param> /// <param name="ExchangeItems"> Mail to send. </param> /// <returns> true if successful. </returns> private static bool SendExchangeMail(ReviewNotifierConfiguration Config, List<MessageType> ExchangeItems) { int maxQuotaNum = Int32.MaxValue; var binding = (ExchangeServicePortType)new ExchangeServicePortTypeClient( new BasicHttpBinding("ExchangeServiceBinding") { MaxReceivedMessageSize = maxQuotaNum, MaxBufferSize = maxQuotaNum, ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas() { MaxArrayLength = maxQuotaNum, MaxStringContentLength = maxQuotaNum, MaxNameTableCharCount = maxQuotaNum } }, new EndpointAddress(Config.EmailService)); DistinguishedFolderIdType folder = new DistinguishedFolderIdType(); folder.Id = DistinguishedFolderIdNameType.sentitems; TargetFolderIdType targetFolder = new TargetFolderIdType(); targetFolder.Item = folder; CreateItemType createItem = new CreateItemType(); createItem.MessageDisposition = MessageDispositionType.SendAndSaveCopy; createItem.MessageDispositionSpecified = true; createItem.SavedItemFolderId = targetFolder; createItem.Items = new NonEmptyArrayOfAllItemsType(); createItem.Items.Items = ExchangeItems.ToArray(); var createReq = new CreateItemRequest() { CreateItem = createItem }; var response = binding.CreateItem(createReq); bool result = true; foreach (ResponseMessageType r in response.CreateItemResponse1.ResponseMessages.Items) { if (r.ResponseClass != ResponseClassType.Success) { Log.Info("Failed to send the message. "); Log.Info(r.MessageText); result = false; } } return result; }
/// <summary> /// Returns the user's friendly name, if LDAP is enabled; otherwise returns userName. /// </summary> /// <param name="Config"></param> /// <param name="userName"></param> /// <returns>User's friendly (given) name.</returns> private static string ResolveFriendlyName(ReviewNotifierConfiguration Config, string userName) { if (!Config.UseLdap) return userName; string givenname; if (givennameDictionary.TryGetValue(userName, out givenname)) return givenname; givenname = RetrieveLdapProperty("givenname", userName); if (givenname != null) { givennameDictionary[userName] = givenname; } else { givenname = userName; Console.Error.WriteLine("Failed ldap lookup for {0}. Using {1}.", userName, givenname); } return givenname; }