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(); }
private async Task NotifySummary(SummaryViewModel summary, RazorLightEngine razor, SmtpClient smtpClient) { if (!SummaryRecipients.Any()) { Logger.Warn("Not sending summary, no recipients defined"); return; } string subjectTemplate = File.ReadAllText(Path.Combine(new FileInfo(Assembly.GetExecutingAssembly().Location).DirectoryName, "Views", "SummarySubject", "Template.cshtml")); string bodyTemplate = File.ReadAllText(Path.Combine(new FileInfo(Assembly.GetExecutingAssembly().Location).DirectoryName, "Views", "SummaryBody", "Template.cshtml")); ExpandoObject viewBag = new { SonarUrl }.ToExpando(); Logger.Info($"Preparing to send summary mail to: {SummaryRecipients.ToPrettyString()}"); foreach (string summaryRecipient in SummaryRecipients) { await DoSendMessage( smtpClient : smtpClient, recipient : summaryRecipient, subject : (await razor.CompileRenderStringAsync("summary-subject", subjectTemplate, summary, viewBag).OnAnyThread()).Trim(), message : (await razor.CompileRenderStringAsync("summary-body", bodyTemplate, summary, viewBag).OnAnyThread()).Trim()).OnAnyThread(); } }