public ActionResult Index(SendViewModel model) { if (ModelState.IsValid) { if (model.SendBy == 0) HostingEnvironment.QueueBackgroundWorkItem(ct => MessageSender.SendAsync(model)); else if (model.SendBy == 1) { BackgroundJob.Enqueue(() => MessageSender.SendWithEvent(model)); } else { //TODO send queue message to use WebJobs var storageAccount = CloudStorageAccount.Parse( ConfigurationManager.ConnectionStrings["AzureWebJobsStorage"].ConnectionString); var queueClient = storageAccount.CreateCloudQueueClient(); var queue = queueClient.GetQueueReference("pending-mail"); // Create the queue if it doesn't already exist queue.CreateIfNotExists(); var message = new CloudQueueMessage(JsonConvert.SerializeObject(model)); queue.AddMessage(message); } return RedirectToAction("Pending"); } return View(model); }
public static void SendWithEvent(SendViewModel model) { try { var smtpClient = new SmtpClient("smtp.sendgrid.net"); smtpClient.Credentials = GetCredential(); smtpClient.SendCompleted += smtpClient_SendCompleted; smtpClient.SendAsync(GetMailMessage(model), null); } catch (Exception ex) { Trace.TraceError(ex.Message); } }
private static MailMessage GetMailMessage(SendViewModel model) { var myMessage = new MailMessage(); myMessage.From = string.IsNullOrEmpty(model.FromName) ? new MailAddress(model.FromEmail) : new MailAddress(model.FromEmail, model.FromName); myMessage.To.Add(model.ToEmail); myMessage.Subject = model.Subject; //Add the HTML and Text bodies myMessage.IsBodyHtml = false; myMessage.Body = model.Body; return myMessage; }
public static async Task SendAsync(SendViewModel model) { try { var smtpClient = new SmtpClient("smtp.sendgrid.net"); smtpClient.Credentials = GetCredential(); ; await smtpClient.SendMailAsync(GetMailMessage(model)); if (NotifyCompleted != null) await NotifyCompleted(); } catch (Exception ex) { Trace.TraceError(ex.Message); } }