C
C#4mo ago
Shiv

Best way to trigger a Queued Email?

Hi Team, We are storing all the mails data in Queued Email table (Id,FromEmail,FromName,ToEmail ,ToName,Cc,Bcc,Subject,Body,CreatedOn,SendTries,IsSent,SentOn) before actually sending the emails. Now the question is how to trigger this at x min, y min or Immediately send. I can use schedulers for x min and y min but some mails need to sent immediately like OTP related mails. I cant wait for scheduler to pick it up. Any ideas on this ?
17 Replies
Pobiega
Pobiega4mo ago
somehow you need to trigger the email sending after adding the "immediate" email. Method call, message via message hub, event.. whatever
Shiv
Shiv4mo ago
Can we use SQL triggers to initiate an API ? @Pobiega
Pobiega
Pobiega4mo ago
I'm not familiar with SQL triggers but that just sounds like you are trying to use the sql database as your message broker tbh and then why not use a real message broker instead? :p
tera
tera4mo ago
check out hangfire
Pobiega
Pobiega4mo ago
hangfire is overkill for this
Shiv
Shiv4mo ago
Something like Azure service bus?
Pobiega
Pobiega4mo ago
depends on your app if its a monolith you dont need out-of-app messaging you could use something like Mediatr, ImmediateHandlers or similar if you dont already have a scheduler, then hangfire is certainly an alternative - but its APIs are a bit clunky imho
Dropps
Dropps4mo ago
mediatr gives you the ability to use notifications in code if you have some sort of "distributed cloud clusterfuck" then use something like rabbitmq or azure service bus aws SQS as a message queue to queue up those email sendings the consumer of those mails can just always send immediately and you just push your emails to the queue when you want to send something that sending part you can do with a scheudler like hangfire quartz or whatever else background jobs you use
Pobiega
Pobiega4mo ago
the good part about putting it on the queue even if its supposed to go right now is that you get re-try logic "for free" instead of having to implement that as part of your immediate code too
Dropps
Dropps4mo ago
re try as in only delete the message when successfully sent
Shiv
Shiv4mo ago
Is Mediatr too much to implement or I can fit it in existing application? @Dropps @Pobiega
Dropps
Dropps4mo ago
depends on your application did you got a monolith or ?
Pobiega
Pobiega4mo ago
can easily add it to an existing app and use it only for notifications assuming monolith, yes if its a microservice app then you need an external message hub/bus
Dropps
Dropps4mo ago
if its monolithic then yes its easy if you got any distributed stuff then mediatr cant help you at all
Shiv
Shiv4mo ago
Yes its monolithic I will give a try then.
Pobiega
Pobiega4mo ago
then absolutely. you just need to think about service lifetimes, but you probably already do
Shiv
Shiv4mo ago
Yes