Question

In my application I have a functionality to save and publish articles. So when I click on "Save and Publish" button three things happened:

  1. Published articles get saved in database.
  2. A Notification email goes to a group of users that a new articles is available.
  3. After sending emails page get redirect to "Article Listing" page without showing any success or failure message for an email.

Now Number of users who will receive emails can vary say for e.g 10, 30 50 and so on. I want to send notification emails asynchronously so that page won't get block until all the mails doesn't go to their receptionists.

Given below is a piece of code from "PublishArticle" action method

foreach (string to in emailIds)
 {
  ArticleNotificationDelegate proc = Email.Send;                              
  IAsyncResult asyncResult = proc.BeginInvoke(subject, body, to, from, cc, null,     null, null);
 }           

Below I have defined a delegate to invoke Send method

private delegate bool ArticleNotificationDelegate (string subject, string body, string to, string from, string cc, string bcc = null);

and this is the code to send an email:

public static bool Send(string subject, string body, string to, string from, string cc, string bcc = null)
    {
        bool response;
        MailMessage mail = new MailMessage();
        MailAddress fromAddress = new MailAddress(from);
        mail.To.Add(to);
        if (!string.IsNullOrWhiteSpace(cc))
        {
            mail.CC.Add(cc);
        }
        if (!string.IsNullOrWhiteSpace(bcc))
        {
            mail.Bcc.Add(bcc);
        }
        mail.From = fromAddress;
        mail.Subject = subject;
        mail.Body = body;
        mail.IsBodyHtml = true;
        mail.Priority = MailPriority.High;

        SmtpClient client = new SmtpClient();
        try
        {
            client.Send(mail);
            response = true;
        }          
        catch (Exception)
        {
            response = false;
        }
        finally
        {
            client.Dispose();
            mail.Dispose();
        }
        return response;
    }

Although this code is working fine but still I want to know that whether my this approach is fine and will not cause any problem in future.
If there is a better approach to accomplish my objective then please suggest me.

Note: As I am using .net framework 4.0 so cannot use the new features of Asyn and await available in 4.5.
Also I had used method client.SendAsync by simply replacing the client.Send and rest of my code in above Send method was same. After this change NO mails were being send and also it did not throw any exception. So I did not go with this change.

Was it helpful?

Solution

I want to send notification emails asynchronously so that page won't get block until all the mails doesn't go to their receptionists.

That's a very dangerous approach, because ASP.NET will feel free to tear down your app domain if there are no active requests. ASP.NET doesn't know that you've queued work to its thread pool (via BeginInvoke). I recommend that you use a solution like HangFire to reliably send email.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top