private async Task NotifyUser(SummaryViewModel summary, RazorLightEngine razor, SmtpClient smtpClient) { if (!summary.Users.Any()) { Logger.Warn("Not sending user notification, no users to notify"); return; } string subjectTemplate = File.ReadAllText(Path.Combine(new FileInfo(Assembly.GetExecutingAssembly().Location).DirectoryName, "Views", "UserSubject", "Template.cshtml")); string bodyTemplate = File.ReadAllText(Path.Combine(new FileInfo(Assembly.GetExecutingAssembly().Location).DirectoryName, "Views", "UserBody", "Template.cshtml")); ExpandoObject viewBag = new { SonarUrl }.ToExpando(); foreach (UserViewModel model in summary.Users) { if (IgnoredRecipients.Contains(model.User.Key) || (RecipientFilter.Any() && !RecipientFilter.Contains(model.User.Key))) { Logger.Info($"Not sending mail to: {model.User.Key}, recipient ignored"); continue; } await DoSendMessage( smtpClient : smtpClient, recipient : model.User.EmailAddress, subject : (await razor.CompileRenderStringAsync("user-subject", subjectTemplate, model, viewBag).OnAnyThread()).Trim(), message : (await razor.CompileRenderStringAsync("user-body", bodyTemplate, model, viewBag).OnAnyThread()).Trim()).OnAnyThread(); } }
public void addRecipientIds(string RecipientIds, int DocumentFiltersID) { try { string[] arr = RecipientIds.Split(','); RecipientFilter rec = new RecipientFilter(); rec.DocumentFiltersID = DocumentFiltersID; for (int i = 0; i < arr.Length; i++) { string[] val = arr[i].Split('-'); if (val.Length > 1) { if (val[0] == "Department") { rec.DepartmentID = Convert.ToInt32(val[1]); } else if (val[0] == "Designation") { rec.DesignationID = Convert.ToInt32(val[1]); } else if (val[0] == "Role") { rec.RoleID = Convert.ToInt32(val[1]); } } } rec.EntityState = DomainModelLibrary.EntityState.Added; AddRecipientFilterts(rec); } catch (Exception ex) { throw ex; } }
public async Task ProcessAsync() { Require.NonNullNonEmpty(SonarUrl, "SonarUrl"); Require.NonNullNonEmpty(Token, "Token"); Require.NonNullNonEmpty(SmtpServer, "SmtpServer"); Require.NonNullNonEmpty(SmtpSenderAddress, "SmtpServerAddress"); if (SmtpUsername != null && SmtpPassword != null) { Require.NonNullNonEmpty(SmtpUsername, "SmtpUsername"); Require.NonNullNonEmpty(SmtpPassword, "SmtpPassword"); } Logger.Info($"Sonar URL: {SonarUrl}"); Logger.Info($"Max Issue Date: {MaxCreationDate}"); Logger.Info($"Project filter: {ProjectFilter.ToPrettyString()}, ignored projects: {IgnoredProjects.ToPrettyString()}"); Logger.Info($"User filter: {UserFilter.ToPrettyString()}, ignored users: {IgnoredUsers.ToPrettyString()}"); Logger.Info($"SMTP server: {SmtpServer} ({(SmtpSslEnabled ? "SSL enabled" : "SSL disabled")})"); Logger.Info($"SMTP sender address: {SmtpSenderAddress}"); Logger.Info($"SMTP authentication: {(SmtpUsername != null && SmtpPassword != null ? $"Yes (username: {SmtpUsername})" : "None")}"); Logger.Info($"Recipient filter: {RecipientFilter.ToPrettyString()}, ignored recipients: {IgnoredRecipients.ToPrettyString()}"); Logger.Info($"Summary recipients: {SummaryRecipients.ToPrettyString()}"); SummaryViewModel summary = await GenerateSummary().OnAnyThread(); Logger.Info($"Processing complete, {summary.NumberOfOverdueIssues} issues overdue, {summary.NumberOfUnassignedIssues} issues unassigned, {summary.Users.Count} users to notify, {summary.Projects.Count} projects to notify"); RazorLightEngine razor = new RazorLightEngineBuilder() .UseEmbeddedResourcesProject(typeof(Startup)) // exception without this (or another project type) .UseMemoryCachingProvider() .Build(); SmtpClient smtpClient = new SmtpClient(SmtpServer !); smtpClient.EnableSsl = SmtpSslEnabled; if (SmtpUsername != null && SmtpPassword != null) { smtpClient.Credentials = new NetworkCredential(SmtpUsername, SmtpPassword); } await NotifyUser(summary, razor, smtpClient).OnAnyThread(); await NotifySummary(summary, razor, smtpClient).OnAnyThread(); }