I am developing a website by using ASP.NET. In there I am sending emails to users.
Currently I am using this code to send email asynchronously to user. Emails are sending in background.
public static void SendEmail(string Path, string EmailTo)
{
Thread emailThread = new Thread(delegate()
{
try
{
string body = string.Empty;
using (StreamReader reader = new StreamReader(Path))
{
body = reader.ReadToEnd();
}
MailMessage mail = new MailMessage();
mail.From = new MailAddress("[email protected]");
mail.To.Add(EmailTo);
mail.Subject = "Test email";
mail.Body += body;
mail.IsBodyHtml = true;
SmtpClient smtpClient = new SmtpClient("smtp.test.com");
smtpClient.Port = 587;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential("[email protected]", "test");
smtpClient.EnableSsl = true;
smtpClient.SendCompleted += (s, e) =>
{
smtpClient.Dispose();
mail.Dispose();
};
try
{
smtpClient.Send(mail);
}
catch (Exception ex) { /* exception handling code here */ }
}
catch (Exception)
{
throw;
}
});
emailThread.IsBackground = true;
emailThread.Start();
}
So above code is working fine. But one day I got a problem. When I press the send button at the same time my internet connection was down. So email didn't get fired. Thats the time I realize that this email function need some mechanism to queue the emails and send it to users one by one according to the order in the queue. So if any email got undelivered or if network gets down then retry it. So how to achieve this mechanism?
Create queuing services There are two methods, one that exposes queuing functionality, and another that dequeues previously queued work items. A work item is a Func<CancellationToken, ValueTask> . Next, add the default implementation to the project. The preceding implementation relies on a Channel<T> as a queue.
First: Edit Your AppStart Page SmtpPort: The port the server will use to send SMTP transactions (emails). EnableSsl: True, if the server should use SSL (Secure Socket Layer) encryption. UserName: The name of the SMTP email account used to send the email. Password: The password of the SMTP email account.
An email queue decouples the sender from the recipient. It allows them to communicate without being connected. As such, the queued emails wait for processing until the recipient is available to receive them. You can look at an email queue as a buffer where the emails are stored before they hit the endpoint.
ASP.NET Queue The Queue works like FIFO system , a first-in, first-out collection of Objects. We can Enqueue (add) items in Queue and we can Dequeue (remove from Queue ) or we can Peek (that is we will get the reference of first item ) item from Queue.
Decouple the queuing of emails from sending them. e.g.:
This is a very stripped to bone simple example, but you can easily expand on it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With