๐ง Introduction
If you manage SQL Server instances, youโve probably configured Database Mail at least once to send alerts or job notifications. Doing it manually through SQL Server Management Studio (SSMS) can be tedious โ especially across multiple servers.
This guide shows you how to automate Database Mail setup in SQL Server using a simple T-SQL script.
The script enables the mail feature, creates mail profiles and SMTP accounts, associates them, and even provides cleanup options โ a complete solution for SQL email alert automation.
โ๏ธ Step 1. Enable Advanced Options and Database Mail
Before configuring Database Mail, you must enable it at the instance level.
USE master;
GO
sp_configure 'show advanced options', 1;
GO
RECONFIGURE WITH OVERRIDE;
GO
sp_configure 'Database Mail XPs', 1;
GO
RECONFIGURE;
GO
โ
Explanation:
This enables the Database Mail XPs option, which allows SQL Server to send emails through the Database Mail system.
โ๏ธ Step 2. Create a Database Mail Profile โ DBA Alerts
A Database Mail profile groups one or more mail accounts together.
You can use a single profile for different email notifications โ such as job alerts, error messages, and report delivery.
IF NOT EXISTS(SELECT * FROM msdb.dbo.sysmail_profile WHERE name = 'DBA Alerts')
BEGIN
EXEC msdb.dbo.sysmail_add_profile_sp
@profile_name = 'DBA Alerts',
@description = '';
END
โ
Tip:
Profiles provide flexibility โ you can have one for general alerts and another for system-critical alerts.
๐ Step 3. Create a Database Mail Account โ SMTP Alert
Now, create a Database Mail account, which defines how and where SQL Server sends emails.
IF NOT EXISTS(SELECT * FROM msdb.dbo.sysmail_account WHERE name = 'SMTP Alert')
BEGIN
EXEC msdb.dbo.sysmail_add_account_sp
@account_name = 'SMTP Alert',
@email_address = '[email protected]',
@display_name = 'DBA Reports',
@mailserver_name = 'yourserver.com',
@mailserver_type = 'SMTP',
@port = '25',
@use_default_credentials = 0,
@enable_ssl = 0;
END
โ
Explanation:
This creates an SMTP mail account named SMTP Alert that uses the mail server yourserver.com.
You can replace these values with your own SMTP settings.
๐ Step 4. Associate the Mail Account with the Profile
Next, connect the mail account to the profile so SQL Server knows which account to use for each profile.
IF NOT EXISTS(
SELECT *
FROM msdb.dbo.sysmail_profileaccount pa
INNER JOIN msdb.dbo.sysmail_profile p ON pa.profile_id = p.profile_id
INNER JOIN msdb.dbo.sysmail_account a ON pa.account_id = a.account_id
WHERE p.name = 'DBA Alerts' AND a.name = 'SMTP Alert'
)
BEGIN
EXEC msdb.dbo.sysmail_add_profileaccount_sp
@profile_name = 'DBA Alerts',
@account_name = 'SMTP Alert',
@sequence_number = 1;
END
โ
Result:
Your DBA Alerts profile is now linked to the SMTP Alert account and ready to send automated emails.
๐งฉ Step 5. Create Another Mail Profile โ DBAalerts
If you want multiple profiles (for example, one for daily reports and another for high-priority alerts), you can duplicate the logic for a new profile:
IF NOT EXISTS(SELECT * FROM msdb.dbo.sysmail_profile WHERE name = 'DBAalerts')
BEGIN
EXEC msdb.dbo.sysmail_add_profile_sp
@profile_name = 'DBAalerts',
@description = 'Secondary mail profile for DBA alerts';
END
๐งน Step 6. Optional: Drop Profiles and Accounts
Need to reset your Database Mail configuration?
The script includes cleanup commands (commented out by default):
-- Deleting profile and account if exists
EXEC msdb.dbo.sysmail_delete_profileaccount_sp @profile_name = 'DBA Alerts', @account_name = 'SMTP Alert';
EXEC msdb.dbo.sysmail_delete_account_sp @account_name = 'SMTP Alert';
EXEC msdb.dbo.sysmail_delete_profile_sp @profile_name = 'DBA Alerts';
โ
Tip:
Uncomment and run this section if you ever need to remove or rebuild your Database Mail setup.
๐ Summary of Key Concepts
| Concept | Description |
|---|---|
| Profile | A logical container that groups one or more mail accounts. |
| Account | Defines SMTP settings such as server, port, email address, and authentication. |
| Profile-Account Link | Connects profiles to accounts, allowing SQL Server to send emails. |
| Cleanup Commands | Optional steps to remove or recreate profiles and accounts. |
๐ก Why Automate Database Mail Setup?
Automating Database Mail setup with a T-SQL script offers several benefits:
- ๐ Consistency: Ensures identical mail settings across all SQL instances.
- โก Speed: Saves time when deploying or rebuilding servers.
- ๐งฉ Reusability: You can easily version or schedule the script.
- ๐งพ Auditability: Configuration is logged and repeatable โ no guesswork.
๐จ Example Use Cases
Once configured, you can use the profile to:
- Send SQL Agent job alerts automatically
- Deliver daily reports or error summaries
- Notify DBAs about backup or maintenance job failures
- Integrate with custom monitoring scripts
๐ Conclusion
This SQL Server Database Mail setup script gives you full control over email alert configuration in a consistent, automated way.
By enabling, creating, and linking Database Mail profiles and accounts, you can automate SQL email alerts efficiently โ without manually clicking through SSMS wizards.
Start using this script in your environment to simplify alert configuration and improve visibility into your SQL Server operations.

