开发者

What should i know about sending emails in a background thread? ASP.NET

开发者 https://www.devze.com 2022-12-17 05:24 出处:网络
I have a thread running in the background that will sleep and pull data from the database when something wakes it up. I am sending the emails using google apps using SmtpClient (code below).

I have a thread running in the background that will sleep and pull data from the database when something wakes it up. I am sending the emails using google apps using SmtpClient (code below).

I wanted to know if there is anything i be aware of? I plan to send only one email at a time (a registration or forgot password email). I am a little worried something can happen like an invalid email locking up the thread because i didnt set a timeout or maybe google apps happen to be done and causing the app to blow up. What should i be aware off? I should ask how i should test as well?

        var client = new SmtpClient("smtp.gmail.com", 587)
        {
            Credentials = new NetworkCredential(fromAddr, pass),
            EnableSsl = true
        };
        MailMessage mail = new MailMessage(fromAddr, toAddr, subject, body);
        mail.IsBodyHtml = true;
        cli开发者_StackOverflowent.Send(mail);


If you're using IIS. Install SMTP on your server an send all mail to localhost. That way if an email doesn't go through right away, the SMTP server will queue the email instead of hang your application.

You will need to configure the SMTP server to use gmail as a smarthost. If you need more information about how to configure this, let me know.


Add a try/catch to the code, and you are fine. It will, at worst, eventually timeout. It won't run forever, and Google won't keep the connection open forever (in fact SMTP is pretty unforgiving when it comes to timeouts... if you are slow, it will kill the connection).

    try
    {
        var client = new SmtpClient("smtp.gmail.com", 587)
        {
            Credentials = new NetworkCredential(fromAddr, pass),
            EnableSsl = true
        };
        MailMessage mail = new MailMessage(fromAddr, toAddr, subject, body);
        mail.IsBodyHtml = true;
        client.Send(mail);
    }
    catch
    {
        // do something
    }

When it comes to testing... use a different gMail account. Also you may want to check on your limit of sent emails per day. Not sure what gMail's number is but they don't just let you send how ever many you want to send.


I am assuming this is being done under ASP.Net since it is one of the tags.

How important is this process? This may be a good candidate for a Windows Service rather than part of the ASP.Net runtime if your environment allows.

You've mentioned that you pull the data from the database and send the email. None of this relies on ASP.Net. Plus a service may give you better threading, testing, logging and async options.

If you want to stick with ASP.Net runtime, then you can also consider Asynchronous Pages if you haven't already. This will allow you to spawn several mail threads and have your main page thread wait for the results. You can also set a timeout value that will allow you to cancel and log stuck threads. Setting and handling an async timeout would take care of much of your worries I suspect. This solution will at least take your mail sending off your make page request thread.

Testing should be straight forward. You can register separate google accounts for testing.


You can set the Timeout property of SmtpClient, and then handle any exceptions thrown by the Send call. Then it will not lock up the thread, though you will have to handle the cause of the exception as you deem appropriate.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号