Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Applies to:
SQL Server
Azure SQL Managed Instance
This article describes how to check the status of the e-mail message sent using Database Mail in SQL Server by using Transact-SQL.
- Database Mail keeps copies of outgoing e-mail messages and displays them in the
sysmail_allitems,sysmail_sentitems,sysmail_unsentitems,sysmail_faileditemsviews of themsdbdatabase. - The Database Mail external program logs activity and displays the log through the Windows Application Event Log and the
sysmail_event_logview in themsdbdatabase. - E-mail messages have one of four possible statuses: sent, unsent, retrying, and failed.
To check the status of an e-mail message, run a query against the msdb.dbo.sysmail_event_log system view.
Use Transact-SQL to view the status of the e-mail sent using Database Mail
Select from the
sysmail_allitemstable, specifying the messages of interest bymailitem_idorsent_status.To check the status returned from the external program for the e-mail messages, join
sysmail_allitemstosysmail_event_logview on themailitem_idcolumn.By default, the external program does not log information about messages that were successfully sent. To log all messages, set the logging level to verbose using the Configure System Parameters page of the Database Mail Configuration Wizard.
The following example lists information about any e-mail messages sent to danw that the external program could not send successfully. The statement lists the subject, the date and time that the external program failed to send the message, and the error message from the Database Mail log.
USE msdb ;
GO
-- Show the subject, the time that the mail item row was last
-- modified, and the log information.
-- Join sysmail_faileditems to sysmail_event_log
-- on the mailitem_id column.
-- In the WHERE clause list items where danw was in the recipients,
-- copy_recipients, or blind_copy_recipients.
-- These are the items that would have been sent
-- to danw.
SELECT items.subject,
items.last_mod_date
,l.description
FROM dbo.sysmail_faileditems AS items
INNER JOIN dbo.sysmail_event_log AS l
ON items.mailitem_id = l.mailitem_id
WHERE items.recipients LIKE '%danw%'
OR items.copy_recipients LIKE '%danw%'
OR items.blind_copy_recipients LIKE '%danw%';
GO