private async Task SendEmailAsync(string subject, string body, MailPriority priority) { try { using (var client = new SmtpClient { Port = SmptPort, Host = SmtpHost, EnableSsl = true, Timeout = 10000, DeliveryMethod = SmtpDeliveryMethod.Network, UseDefaultCredentials = false, Credentials = new System.Net.NetworkCredential(Login, Password), }) { var mailMessage = new MailMessage(Sender, Receivers.First(), subject, body) { BodyEncoding = Encoding.UTF8, DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure, Priority = priority }; Receivers.ForEach(r => { if (mailMessage.To.All(ma => ma.Address != r)) { mailMessage.To.Add(r); } }); await client.SendMailAsync(mailMessage); } } catch (Exception ex) { Logger.LogException("MAIL LOG", LogLevel.Warning, ex); } }
/// <summary> /// Start a run /// </summary> public void Run() { if (Receivers.Count == 0) { Exception ex = new Exception($"No receiver declared"); Logger.Warn(ex.Message, ex); return; } List <Runner> runners; switch (StartingRunningLevel.BaseRunningLevel) { case BaseRunningLevel.Tenant: // If Tenant, must be only one url runners = new List <Runner>() { GetTenantRunner(Urls.FirstOrDefault()) }; break; case BaseRunningLevel.SiteCollection: runners = new List <Runner>(GetSiteCollectionRunners(Urls)); break; case BaseRunningLevel.Site: runners = new List <Runner>(GetSiteRunners(Urls)); break; case BaseRunningLevel.List: runners = new List <Runner>(GetListRunners(Urls)); break; default: throw new Exception($"Run cannot start at '{StartingRunningLevel.ToString()}' level"); } Logger.Info("RunningManager initialized"); Logger.Info($"Receivers count: {Receivers.Count}"); Logger.Info($"StartingRunningLevel: {StartingRunningLevel}"); Logger.Info($"Runners count: {runners.Count} for URLs: '{string.Join(", ", Urls)}'"); // OnStart Logger.Debug("RunningManager OnStart()"); Receivers.ForEach(r => r.OnStart()); // Launch runners runners.ForEach(r => r.Process()); // OnEnd Logger.Debug("RunningManager OnEnd()"); Receivers.ForEach(r => r.OnEnd()); Logger.Info("RunningManager finished"); }
/// <summary> /// 初始化邮件 /// </summary> public void Init() { this.message.From.Add(new MailboxAddress(Sender)); if (this.Receivers != null && this.Receivers.Count != 0) { Receivers.ForEach(r => { this.message.To.Add(new MailboxAddress(r)); }); } this.message.Subject = Subject; var builder = new BodyBuilder(); builder.HtmlBody = this.Text; if (!String.IsNullOrEmpty(ImagePath)) { builder.Attachments.Add(ImagePath); builder.Attachments[0].ContentId = MimeUtils.GenerateMessageId(); builder.HtmlBody += $"\n<img src=\"\"cid:{builder.Attachments[0].ContentId}\"\">"; } message.Body = builder.ToMessageBody(); }