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
somehow you need to trigger the email sending after adding the "immediate" email. Method call, message via message hub, event.. whatever
Can we use SQL triggers to initiate an API ? @Pobiega
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
check out hangfire
hangfire is overkill for this
Something like Azure service bus?
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
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
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
re try as in only delete the message when successfully sent
Is Mediatr too much to implement or I can fit it in existing application? @Dropps @Pobiega
depends on your application
did you got a monolith or ?
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
if its monolithic then yes its easy
if you got any distributed stuff then mediatr cant help you at all
Yes its monolithic
I will give a try then.
then absolutely. you just need to think about service lifetimes, but you probably already do
Yes