コード例 #1
0
        public async Task ScheduleErrorEmail()
        {
            string Template = File.ReadAllText(Path.Combine(HostingEnvironment.MapPath("~/Content/EmailTemplates"), "ErrorReport.html"));

            Template = Template.Replace("{{ErrorLink}}", _configService.getConfigValusAsString("ErrorLink"));

            // Call get severity and add to template
            var ErrorList = _errorLogService.GetSeverity();

            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < ErrorList.Count; i++)
            {
                sb.Append("<br/>");
                if (i == 0)
                {
                    sb.Append(string.Format("There are {0} info errors", ErrorList[i].ErrorCount));
                }
                else if (i == 1)
                {
                    sb.Append(string.Format("There are {0} warnings", ErrorList[i].ErrorCount));
                }
                else if (i == 2)
                {
                    sb.Append(string.Format("There are {0} errors", ErrorList[i].ErrorCount));
                }
                else if (i == 3)
                {
                    sb.Append(string.Format("There are {0} critical errors", ErrorList[i].ErrorCount));
                }
            }
            Template = Template.Replace("{{Errors}}", sb.ToString());

            var FromEmail        = _configService.getConfigValusAsString("FromEmailErrorReport");
            var FromName         = _configService.getConfigValusAsString("FromNameSendGrid");
            var ToEmail          = _configService.getConfigValusAsString("AdminEmail");
            var ToName           = _configService.getConfigValusAsString("AdminName");
            var apiKey           = _configService.getConfigValusAsString("SendGridKey");
            var client           = new SendGridClient(apiKey);
            var from             = new EmailAddress(FromEmail, FromName);
            var subject          = _configService.getConfigValusAsString("ErrorReportSubject");
            var plainTextContent = "";
            var to          = new EmailAddress(ToEmail, ToName); // send this to admin for errors
            var htmlContent = Template;                          // send list of errors here - go make a database call to gather information that will fill the body of the email.
            var msg         = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
            var response    = await client.SendEmailAsync(msg);
        }