What should I know about sending emails in the background thread? ASP.NET

I have a background thread that will sleep and retrieve data from the database when something wakes up. I am sending emails using Google Apps using SmtpClient (code below).

I wanted to know if there is anything I should know about? I plan to send only one email at a time (email with registration or forgot password). I am a little worried that something might happen, like an invalid email blocking the flow, because I did not set a timeout or maybe Google apps that were made, and as a result the app exploded. What should I know? I have to ask, how should I also test?

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); 
+7
c # email smtp google-apps
source share
4 answers

If you are using IIS. Install SMTP on your server to send all mail to localhost. Thus, if the email does not pass immediately, the SMTP server will queue the email instead of hanging your application.

You need to configure your SMTP server to use gmail as smarthost. If you need more information on how to configure this, let me know.

+3
source share

Add try / catch to the code, and everything is fine. This, in the worst case, ultimately means a timeout. It will not work forever, and Google will not keep communication open forever (in fact, SMTP is pretty implacable when it comes to timeouts ... if you're 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. You can also check your limit of sent letters per day. You don’t know what the gMail number is, but they do not just allow you to send how much more you want to send.

+2
source share

I assume this is done in ASP.Net as this is one of the tags.

How important is this process? This might be a good candidate for a Windows service, and not part of the ASP.Net runtime, if your environment allows it.

You mentioned that you are retrieving data from a database and sending an email. None of this is dependent on ASP.Net. In addition, the service can give you more accurate information about flow, testing, logging, and asynchronous mode.

If you want to stick to ASP.Net runtime, you can also consider Asynchronous pages , if you haven't already. This will allow you to generate multiple mail streams, and your main page stream will wait for results. You can also set a timeout value that allows you to cancel and log threads. Setting and handling an asynchronous timeout will take care of many of your worries that I suspect. This solution will at least send your mail from your make page request stream.

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

+1
source share

You can set the Timeout property of SmtpClient , and then handle any exceptions caused by the Send call. Then it will not block the thread, although you will have to handle the cause of the exception as you see fit.

0
source share

All Articles