SQL Server’s Database Mail feature allows the database to send emails directly from within SQL Server—for alerts, job notifications, reports, etc.
However, SQL Server Express Edition does not include the Database Mail feature out-of-the-box. But don’t worry—you can still send email using stored procedures with .NET integration or external tools.
In this tutorial, we’ll cover:
- Limitations of Express Edition.
- Alternative methods to send emails from SQL Server 2012 Express.
- How to use
sp_send_dbmail
in higher editions. - A full example using CLR integration for Express.
1. Limitations in SQL Server 2012 Express
SQL Server Express Edition:
- Supports T-SQL, Stored Procedures, and CLR.
- Does NOT include the built-in Database Mail feature.
- Cannot use SQL Agent for scheduling emails.
Solution: You can use:
- A custom CLR stored procedure (Common Language Runtime).
- PowerShell scripts.
- External applications (e.g., C#, Python, etc.).
2. Send Email via CLR Stored Procedure (Works in Express)
Step-by-Step Guide:
Step 1: Enable CLR Integration
EXEC sp_configure 'show advanced options', 1;
RECONFIGURE;
EXEC sp_configure 'clr enabled', 1;
RECONFIGURE;
Step 2: Create a CLR .NET Project
Use Visual Studio to create a C# class library.
using System.Net;
using System.Net.Mail;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
public class EmailSender
{
[SqlProcedure]
public static void SendEmail(string to, string subject, string body)
{
MailMessage mail = new MailMessage("[email protected]", to, subject, body);
SmtpClient client = new SmtpClient("smtp.yourserver.com");
client.Credentials = new NetworkCredential("[email protected]", "your_password");
client.Port = 587; // or your SMTP port
client.EnableSsl = true;
client.Send(mail);
}
}
Step 3: Deploy the Assembly to SQL Server
- Compile the project to get a
.dll
file. - In SQL Server, run:
CREATE ASSEMBLY EmailSender FROM 'C:\Path\To\EmailSender.dll'
WITH PERMISSION_SET = UNSAFE;
Create the SQL wrapper for the stored procedure:
CREATE PROCEDURE dbo.SendEmail
@to NVARCHAR(100),
@subject NVARCHAR(200),
@body NVARCHAR(MAX)
AS EXTERNAL NAME EmailSender.[EmailSender].SendEmail;
Step 4: Send an Email
EXEC dbo.SendEmail '[email protected]', 'Test Subject', 'Hello from SQL Express!';
3. (For Reference) Using sp_send_dbmail
(Not in Express)
If you’re using SQL Server Standard or higher, use the built-in sp_send_dbmail
.
Enable Database Mail:
EXEC sp_configure 'Database Mail XPs', 1;
RECONFIGURE;
Create Profile & Account via SSMS:
- Go to Object Explorer > Management > Database Mail.
- Set up a new profile and SMTP account.
- Grant access to users.
Send a Test Email:
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'MyMailProfile',
@recipients = '[email protected]',
@subject = 'Test Email',
@body = 'This is a test email from SQL Server.';
While SQL Server 2012 Express Edition does not support Database Mail natively, you can still send emails using custom solutions like CLR stored procedures. This gives you the flexibility to integrate messaging into your applications, trigger alerts, or report generation—even on a free edition of SQL Server.