SQL Server Database Mail Setup

Configure SQL Server to send automated emails for alerts, jobs, and reports.

Database Mail lets SQL Server send emails directly from stored procedures, triggers, or scheduled SQL Agent jobs — perfect for failure alerts, daily reports, or notifying a team when a batch job completes.

Step 1: Enable Database Mail

EXEC sp_configure 'show advanced options', 1;
RECONFIGURE;
EXEC sp_configure 'Database Mail XPs', 1;
RECONFIGURE;

Step 2: Launch the Configuration Wizard

In SSMS, expand Management → right-click Database Mail → Configure Database Mail. Choose "Set up Database Mail by performing the following tasks" and create a new profile.

Step 3: Configure an SMTP Account

  • Provide an outgoing mail server address (SMTP)
  • Enter the port number (commonly 587 or 25)
  • Add authentication credentials if your SMTP server requires them

Step 4: Send a Test Email

EXEC msdb.dbo.sp_send_dbmail
  @profile_name = 'MainProfile',
  @recipients = 'you@example.com',
  @subject = 'Database Mail Test',
  @body = 'This is a test email from SQL Server.';
💡 Tip: Check the sysmail_allitems and sysmail_log views in msdb if an email doesn't arrive — they show delivery status and errors.

Common Use Cases

  • Alerting DBAs when a SQL Agent job fails
  • Emailing daily or weekly report summaries automatically
  • Notifying stakeholders when a scheduled ETL process completes

Once configured, Database Mail becomes a lightweight, reliable way to keep your team informed without building a separate notification system.

Connecting Database Mail to SQL Server Agent

Sending a test message proves the profile works, but most teams set up Database Mail so SQL Server Agent can warn them about failed jobs. That needs three more pieces: a mail profile chosen in Agent, an operator who receives messages, and a notification on each job.

  1. In SSMS, right-click SQL Server Agent, open Properties, go to Alert System, tick Enable mail profile and pick your profile.
  2. Restart the SQL Server Agent service so it reads the new setting.
  3. Create an operator, which is simply a named recipient.
  4. On each important job, open Notifications and choose to email the operator when the job fails.
USE msdb;
EXEC dbo.sp_add_operator
     @name = N'DBA Team',
     @email_address = N'dba-team@example.com';

EXEC dbo.sp_update_job
     @job_name = N'Nightly Backup',
     @notify_level_email = 2,          -- 2 = on failure
     @notify_email_operator_name = N'DBA Team';

Sending Reports with Query Results

sp_send_dbmail can run a query and include its output in the message body or as an attachment. HTML formatting makes small summaries easier to read.

EXEC msdb.dbo.sp_send_dbmail
     @profile_name = 'MainProfile',
     @recipients   = 'reports@example.com',
     @subject      = 'Daily order count',
     @query        = 'SELECT COUNT(*) AS Orders FROM Sales.dbo.Orders
                      WHERE OrderDate >= CAST(GETDATE() AS date);',
     @attach_query_result_as_file = 1,
     @query_attachment_filename   = 'orders.csv',
     @query_result_separator      = ',';

The query runs in its own session, so use three-part names (database, schema, table) or set @execute_query_database. For an HTML body, build the markup in a variable and pass @body_format = 'HTML'.

Troubleshooting Messages That Never Arrive

CheckWhere to look
Did the message fail?msdb.dbo.sysmail_faileditems
What error did the SMTP server return?msdb.dbo.sysmail_event_log
Is the mail queue running?EXEC msdb.dbo.sysmail_help_status_sp; and, if stopped, sysmail_start_sp
Can a non-admin user send mail?They need the DatabaseMailUserRole in msdb and access to the profile
Agent alerts silent?Agent mail profile set and Agent restarted

Most real failures come from the SMTP side: wrong port, TLS not enabled when the server requires it, or a mail provider that blocks basic authentication. Your mail administrator can confirm which settings the relay expects.

Good Practices for a Safe Setup

  • Use a dedicated sending account with only the rights it needs, not a personal mailbox.
  • Enable SSL/TLS on the account whenever the SMTP server supports it.
  • Never email passwords, personal data or full table dumps; send counts, statuses or links to secured reports instead.
  • Keep the msdb mail history small by scheduling sysmail_delete_mailitems_sp and sysmail_delete_log_sp to remove old entries.
  • Add a second account to the profile as a failover so alerts still go out if the main relay is down.

Ready to go beyond the basics?

Get hands-on training, live mentorship, and placement support with Uncodemy's Data Analytics Course.

Explore Data Analytics Course →