I am looking for a way to check if a particular e-mails queued using sp_send_dbmail
are eventually successful sent from our Exchange server. I've looked at the system tables msdb.dbo.sysmail_mailitems
and msdb.dbo.sysmail_log
. msdb.dbo.sysmail_log
seems to be the more helpful of the two; specifically, its description
column. From the tests I've conducted so far, it seems whenever an error occurs, a message in the following format appears in the description
column:
The mail could not be sent to the recipients because of the mail server failure. (Sending Mail using Account 3 (2012-11-01T11:28:04). Exception Message: Cannot send mails to mail server. (Mailbox unavailable. The server response was: 5.7.1 Unable to relay for [email protected]). )
This is enclosed with other rows that share the same process_id
. The description for the enclosing records are
DatabaseMail process is started
and
DatabaseMail process is shutting down
If an e-mail was successfully sent, the same 2 rows are recorded in the table, except with no enclosed rows between them.
So if I have a successful send, the following appears in the table
and if I have a send failure, the log records this
Are there other instances of how the entries could be logged if a send failed or if a send was successful? For example, could it be possible that there are 4 rows of entries for a send (2 enclosing stating when it was started and when it shut down, and 2 enclosed stating the e-mail was successfully sent). I've found no log records that diverged from the pattern listed above, but would like to be sure before I write logic based on this assumption.
Is it Enabled? First to check to make sure that Database Mail is enabled. Look in 'sys. configurations' for the setting Database Mail XPs, and if it is set to 0, Database Mail is not enabled.
Check How Many Mail Items are in the Queue in Database Mail in SQL Server (T-SQL) In SQL Server, you can use the sysmail_help_queue_sp stored procedure on the msdb database to see how many mail items are in the queue, the status of the queue, and when it was last activated.
To see only messages with the failed status, use sysmail_faileditems (Transact-SQL). To see only messages that were sent, use sysmail_sentitems (Transact-SQL). Identifier of the mail item in the mail queue.
sysmail_faileditems
will only get you the list of failed emails. If you need to see a list of successfull emails you need to use sysmail_mailitems
.
Use the below query to get details for all emails sent the same date:
SELECT * FROM msdb..sysmail_mailitems WHERE sent_date > DATEADD(DAY, -1,GETDATE())
And here is the complete query to get all the failed emails from the past 24 hours:
SELECT items.subject , items.recipients , items.copy_recipients , items.blind_copy_recipients , items.last_mod_date , l.description FROM msdb.dbo.sysmail_faileditems AS items LEFT OUTER JOIN msdb.dbo.sysmail_event_log AS l ON items.mailitem_id = l.mailitem_id WHERE items.last_mod_date > DATEADD(DAY, -1,GETDATE())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With