-- Step 3. Access permissions
USE msdb;
GO
ALTER ROLE DatabaseMailUserRole ADD MEMBER [PlamarJobUser];
GO
-- Step 4. Sending test emails
DECLARE @mailitem_id INT;
EXEC msdb.dbo.sp_send_dbmail
@profile_name = N'OperationsProfile',
@recipients = N'recipient@example.com',
@subject = N'SQL Server Notification',
@body = N'<h1>Database Mail works</h1><p>This is a test email from SQL Server.</p>',
@body_format = 'HTML',
@mailitem_id = @mailitem_id OUTPUT;
SELECT @mailitem_id AS mailitem_id;
If you need not just a text email, but, for example, the result of a SQL query, sp_send_dbmail can do that as well:
EXEC msdb.dbo.sp_send_dbmail
@profile_name = N'OperationsProfile',
@recipients = N'report@example.com',
@subject = N'Nightly SQL report',
@body = N'The result of the nightly query is attached.',
@query = N'SELECT TOP (10) Id, Status, CreatedAt FROM dbo.Orders ORDER BY CreatedAt DESC;',
@attach_query_result_as_file = 1,
@query_attachment_filename = N'orders.csv',
@query_result_separator = N';';
-- Verification queries
SELECT TOP (20)
mailitem_id,
sent_status,
send_request_date,
sent_date,
recipients,
subject
FROM msdb.dbo.sysmail_allitems
ORDER BY mailitem_id DESC;
The Database Mail log is also useful for troubleshooting:
SELECT TOP (50)
log_date,
event_type,
description
FROM msdb.dbo.sysmail_event_log
ORDER BY log_date DESC;
If you are specifically interested in failed sends, sysmail_faileditems is also useful:
SELECT TOP (20)
mailitem_id,
subject,
recipients,
last_mod_date,
last_mod_user
FROM msdb.dbo.sysmail_faileditems
ORDER BY mailitem_id DESC;
-- Queue and Service Broker diagnostics
EXEC msdb.dbo.sysmail_help_status_sp;
If Database Mail is stopped, it can be started:
EXEC msdb.dbo.sysmail_start_sp;
Check Service Broker in msdb:
SELECT name, is_broker_enabled
FROM sys.databases
WHERE name = N'msdb';
If is_broker_enabled = 0, that is already a separate administrative problem. It should be fixed carefully and usually only during a database maintenance window.
SMTP, authentication, and TLS errors
Here you need to look at the error description in sysmail_event_log. Usually, that is where you can see what went wrong: incorrect username or password, relay denied, TLS handshake error, inability to connect to the host, or a timeout.
Attachment errors
If you attach a file through @file_attachments, SQL Server must have real file-system-level access to it. Here, the important context is the SQL Server service context, not the current interactive administrator session. When the file “exists” locally but Database Mail cannot see it, the problem is usually permissions or the path.
-- TLS 1.2 strong crypto example
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft.NETFramework\v4.0.30319]
"SchUseStrongCrypto"=dword:00000001
[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft.NETFramework\v4.0.30319]
"SchUseStrongCrypto"=dword:00000001
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Client]
"DisabledByDefault"=dword:00000000
"Enabled"=dword:00000001
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Server]
"DisabledByDefault"=dword:00000000
"Enabled"=dword:00000001
After changing these settings, a restart of the SQL Server service is usually required, and sometimes a full server restart, so that Schannel and .NET reliably pick up the new parameters.
There are three practical nuances here.
First, for Database Mail it is usually better to target port 587 with TLS/STARTTLS. Port 465 is often mentioned in older instructions as an option, but in practice it often proves problematic because Database Mail relies on the .NET SMTP stack and works best with STARTTLS rather than implicit SMTPS.
Second, if you use Microsoft 365 or another modern cloud email service, check not only TLS but also the authentication model. Database Mail does not support a modern OAuth flow. If the provider has disabled basic SMTP AUTH, the email will not be sent even if the T-SQL is correct. In such cases, organizations usually use either a dedicated SMTP relay or a mailbox for which SMTP AUTH is allowed by policy.
Third, the SSL checkbox in the SSMS wizard effectively means that the server side must support a correct SMTP-over-TLS scenario. So you should look not only at the UI, but also at the actual compatibility between your SMTP endpoint and Database Mail.