public async Task SendEmails(int timeoutMinutes = 3) { var context = new MailerContext(); var start = DateTime.UtcNow; var leaseTime = TimeSpan.FromMinutes(timeoutMinutes); var query = context.Outboxes.Query(); TableContinuationToken token = null; do { var segment = query.ExecuteSegmented(token); token = segment.ContinuationToken; if (segment.Results.Count <= 0) { continue; } foreach (var email in segment.Results) { if ((DateTime.UtcNow - start) > leaseTime) { token = null; break; } if (email.LeaseExpire.GetValueOrDefault() > start || !context.Outboxes.Lease(email, leaseTime)) { continue; } await sendEmailFromStore(context, email); } }while (token != null); }
public Task SendEmails(int timeoutMinutes = 3) { var context = new MailerContext(); var query = context.Outboxes.Query(); var leaseTime = TimeSpan.FromMinutes(timeoutMinutes); var emails = context.Outboxes.BulkLease(query, leaseTime); var task = emails.Select(email => sendEmailFromStore(context, email)); return(Task.WhenAll(task)); }
static async Task sendEmailFromStore(MailerContext context, EmailMessage email) { email.DequeueCount++; var result = await safeSendEmail(email); if (result == SmtpStatusCode.Ok) { context.Outboxes.Delete(email); } else if (email.DequeueCount >= 3 || !ShouldRetry(result)) { var op = TableOperation.Insert(email); context.DeadEmails.Execute(op); context.Outboxes.Delete(email); } else { context.Outboxes.Replace(email); } }
public void AddToMailQueue(EmailMessage message) { var context = new MailerContext(); context.Outboxes.Insert(message); }