public async Task <ActionResult> SendNewsletter(HttpPostedFileBase recipient, string subject, string mailMessage, HttpPostedFileBase[] fileAttachments = null) { try { // begin validations if (recipient == null) { throw new Exception("Recipient file must be uploaded"); } if (string.IsNullOrWhiteSpace(subject)) { throw new Exception("Subject is required"); } if (string.IsNullOrWhiteSpace(mailMessage)) { throw new Exception("Mail body is required"); } DataTable recipients = LoadExcelFile("recipient", "Recipients"); if (recipients.Rows.Count == 0) { throw new Exception("At least one recipient is required."); } if (!recipients.Columns.Contains("Email")) { throw new Exception("Column Email is required in the recipients file uploaded."); } if (!recipients.Columns.Contains("Title")) { throw new Exception("Column Title is required in the recipients file uploaded."); } if (!recipients.Columns.Contains("Name")) { throw new Exception("Column Name is required in the recipients file uploaded."); } // end validations SendNewsletterCallResult result = new SendNewsletterCallResult(); await DispatchEmailsAsync(result, recipients, subject, mailMessage, fileAttachments); if (result.NoOfFailedMessages > 0) { ViewBag.Message = string.Format($"{recipients.Rows.Count - result.NoOfFailedMessages} of {recipients.Rows.Count} email was sent. {result.NoOfFailedMessages} failed to deliver."); } else { ViewBag.Message = "Mail was sent to all recipients."; } return(View()); } catch (Exception ex) { ViewBag.Message = "Error: " + ex.Message; return(View()); } }
private async Task <bool> DispatchEmailsAsync(SendNewsletterCallResult result, DataTable recipients, string subject, string mailMessage, HttpPostedFileBase[] fileAttachments = null) { int noOfEmailsProcessed = 0, noOfEmailsPerHour = int.Parse(ConfigurationManager.AppSettings["NoOfEmailsPerHour"].ToString()); DateTime dt = DateTime.Now; int noOfFailedMessages = 0; DataAccess dataAccess = new DataAccess(DataProvider.MsSQLServer, ConfigurationManager.ConnectionStrings["AlertDataConnectionString"].ConnectionString); string reference = Guid.NewGuid().ToString(); foreach (DataRow recipient in recipients.Rows) { string email = recipient["Email"].ToString().Trim(); if (IEIA.CommonUtil.RegexUtilities.IsValidEmail(email)) { string mailBody = string.Format($"Dear {recipient["Title"].ToString().Trim()} {recipient["Name"].ToString().Trim()},<br><br>{mailMessage}"); var isMailSent = await SendEmailAsync(subject, mailBody, email, fileAttachments); if (!isMailSent) { noOfFailedMessages++; } dataAccess.Execute("INSERT INTO MessageHistory (MessageType, Recipient, Message, Subject, IsSent, IsDelivered, Ref, ProviderID, UserName) VALUES ('E', @Recipient, @Message, @Subject, @IsSent, 0, @Ref, @ProviderID, @UserName)", new DataParam[] { new DataParam("@Recipient", email), new DataParam("@Message", mailBody), new DataParam("@Subject", subject), new DataParam("@IsSent", isMailSent), new DataParam("@Ref", reference), new DataParam("@ProviderID", 3), new DataParam("@UserName", Session["userName"]) }); noOfEmailsProcessed++; } TimeSpan span = DateTime.Now.Subtract(dt); if (span.Minutes <= 60 && noOfEmailsProcessed >= noOfEmailsPerHour) { System.Threading.Thread.Sleep(60 - span.Minutes); // wait for the remaining time to 1 hour // reset the time and no of e-mails sent dt = DateTime.Now; noOfEmailsProcessed = 0; } } dataAccess = null; result.NoOfFailedMessages = noOfFailedMessages; return(true); }