Advertisement
otkalce

Entity Framework - e-mail processing

Mar 28th, 2023 (edited)
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.93 KB | Source Code | 0 0
  1. // *** Send all e-mails according to data in database ***
  2. [HttpPost("[action]")]
  3. public ActionResult SendAllNotifications()
  4. {
  5.     var client = new SmtpClient("127.0.0.1", 25);
  6.     var sender = "admin@my-cool-webapi.com";
  7.  
  8.     try
  9.     {
  10.         var unsentNotifications =
  11.             _dbContext.Notifications.Where(
  12.                 x => !x.SentAt.HasValue);
  13.  
  14.         foreach (var notification in unsentNotifications)
  15.         {
  16.             try
  17.             {
  18.                 var mail = new MailMessage(
  19.                     from: new MailAddress(sender),
  20.                     to: new MailAddress(notification.Receiver));
  21.  
  22.                 mail.Subject = notification.Subject;
  23.                 mail.Body = notification.Body;
  24.  
  25.                 client.Send(mail);
  26.  
  27.                 notification.SentAt = DateTime.UtcNow;
  28.                 _dbContext.SaveChanges();
  29.  
  30.             }
  31.             catch (Exception)
  32.             {
  33.                 // Black hole for notification is bad handling :(
  34.             }
  35.         }
  36.  
  37.         return Ok();
  38.     }
  39.     catch (Exception)
  40.     {
  41.         return StatusCode(StatusCodes.Status500InternalServerError);
  42.     }
  43. }
  44.  
  45. // *** Model to send batch of e-mails according to data in database
  46. public partial class SendNotificationsResponse
  47. {
  48.     public int SuccessCount { get; set; }
  49.     public int FailCount { get; set; }
  50.  
  51. }
  52.  
  53. // *** Send batch of e-mails according to data in database ***
  54. [HttpPost("[action]/{count}")]
  55. public ActionResult<SendNotificationsResponse> SendNotificationBatch(int? count)
  56. {
  57.     var client = new SmtpClient("127.0.0.1", 25);
  58.     var sender = "admin@my-cool-webapi.com";
  59.  
  60.     try
  61.     {
  62.         var unsentNotifications =
  63.             _dbContext.Notifications
  64.                 .Where(x => !x.SentAt.HasValue)
  65.                 .OrderBy(x => x.CreatedAt)
  66.                 .AsQueryable();
  67.  
  68.         if(count.HasValue)
  69.             unsentNotifications = unsentNotifications.Take(count.Value);
  70.  
  71.         int sendSuccessCount = 0;
  72.         int sendFailCount = 0;
  73.         foreach (var notification in unsentNotifications)
  74.         {
  75.             try
  76.             {
  77.                 var mail = new MailMessage(
  78.                     from: new MailAddress(sender),
  79.                     to: new MailAddress(notification.Receiver));
  80.  
  81.                 mail.Subject = notification.Subject;
  82.                 mail.Body = notification.Body;
  83.  
  84.                 client.Send(mail);
  85.  
  86.                 notification.SentAt = DateTime.UtcNow;
  87.                 _dbContext.SaveChanges();
  88.  
  89.                 sendSuccessCount++;
  90.             }
  91.             catch (Exception)
  92.             {
  93.                 sendFailCount++;
  94.             }
  95.         }
  96.  
  97.         return Ok(new SendNotificationsResponse
  98.         {
  99.             SuccessCount = sendSuccessCount,
  100.             FailCount = sendFailCount
  101.         });
  102.     }
  103.     catch (Exception)
  104.     {
  105.         return StatusCode(StatusCodes.Status500InternalServerError);
  106.     }
  107. }
  108.  
Tags: ef-usage
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement