Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use .NET (System.Net.Mail) to batch send my web application's emails or SQL (msdb.sp_send_dbmail)?

Scenario:

1) My application is a .NET 3.5 C# web app and the database is SQL 2008.

2) Emails will be in the region of 100 to 1000 a day and are triggered by various web user interactions with the app.

3) Most emails will contain attachment(s) between 50KB and 5MB. Some emails will be HTML and some will be plain text.

4) All attachments will be sourced from a directory on the web server.

5) The SQL server is a separate machine to the Web Server. All SQL connections from the application are via SQL logins not Windows Authentication.

6) For a scalable solution, Emails to be sent will be queued in a database table ready for a batch process to pickup. Failed emails should be retried up to 4 times.

Dilemma:

I'm not sure whether to write a web server solution to send emails (e.g. a windows service which polls the emails ready to send) or maybe to use SQL database mail which is easy to setup and use and doesn't require much development.

The fact the attachments sit on the web server suggests to me to use a web server solution but I would be interested to see if I've missed something.

like image 551
iandayman Avatar asked Jun 24 '10 12:06

iandayman


People also ask

Why would you enable the SQL mail capability?

SQL Server's email functionality—which you can use to notify DBAs of potential problems and of task completion—is a powerful means of keeping your server running.

Can we send email through SQL Server?

Procedure. Using Object Explorer in SQL Server Management Studio, connect to an instance of SQL Server Database Engine where Database Mail is configured, expand Management, right-click Database Mail, and then select Send Test E-Mail.

How do I send an email to multiple recipients in SQL Server?

In order to send mail using sp_send_dbmail multiple recipients, @p_recipients parameter should have concatenated emails with ";" semi-colon. So in your Database Mail sending procedure, if you concatenate emails using ";" you can send emails to multiple recipients using sp_send_dbmail system procedure.

What is SQL Mail?

Database Mail is an enterprise solution for sending e-mail messages from the SQL Server Database Engine or Azure SQL Managed Instance. Your applications can send e-mail messages to users using Database Mail via an external SMTP server.


2 Answers

I think you've answered your own question. As you've chosen to queue the emails in the database IMO there would be little advantage to creating a managed solution on the webserver in order to send the emails.

The one thing I'd consider is the fact you're storing the attachments on the webserver, this means there would be more network traffic between webserver and database than if the webserver were sending the emails (assuming they run on different physical servers). Using your figures that could be 5GB traffic per day in attachments. However, I'd go with the DB solution and test it rigorously to see if traffic becomes a problem. If so, you haven't lost much dev time on the DB solution.

like image 195
David Neale Avatar answered Oct 26 '22 05:10

David Neale


First, be wary about sending 1000s of emails a day in bursts. That's a good way to get blacklisted from other mail servers or even your Internet provider. Second, I'd recommend going the .NET routine. This problem smacks of a service. Even if you do not build it as a service today, you may want to do so at some point in the future. With .NET, you could refactor your solution to send emails in parallel or access the queue in a multi-threaded way. All of this would be difficult in SQL Server. In short, .NET gives you the ability to make a more extensible solution than SQL Server.

like image 24
Thomas Avatar answered Oct 26 '22 06:10

Thomas