{"id":9770,"date":"2020-09-20T08:35:01","date_gmt":"2020-09-19T22:35:01","guid":{"rendered":"https:\/\/database.guide\/?p=9770"},"modified":"2020-09-22T09:01:25","modified_gmt":"2020-09-21T23:01:25","slug":"check-for-failed-email-in-sql-server-t-sql","status":"publish","type":"post","link":"https:\/\/database.guide\/check-for-failed-email-in-sql-server-t-sql\/","title":{"rendered":"Check for Failed Email in SQL Server (T-SQL)"},"content":{"rendered":"\n<p>If you&#8217;re trying to <a href=\"https:\/\/database.guide\/how-to-send-emails-from-sql-server-t-sql\/\" title=\"How to Send Emails from SQL Server (T-SQL)\">send mail with SQL Server<\/a> but it&#8217;s not being delivered, try checking the <code>sysmail_faileditems<\/code> view.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<p>Here&#8217;s an example of checking for failed mail. Note that it needs to be run on the <code>msdb<\/code> database.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM msdb.dbo.sysmail_faileditems;<\/code><\/pre>\n\n\n\n<p>Result (using vertical output):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mailitem_id &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | 1\nprofile_id&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | 1\nrecipients&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | homer@example.com\ncopy_recipients &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | NULL\nblind_copy_recipients &nbsp; &nbsp; &nbsp; | NULL\nsubject &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | SQL Server Agent Job: FAILED\nbody&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | Your favorite SQL Server Agent job just failed\nbody_format &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | TEXT\nimportance&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | NORMAL\nsensitivity &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | NORMAL\nfile_attachments&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | NULL\nattachment_encoding &nbsp; &nbsp; &nbsp; &nbsp; | MIME\nquery &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | NULL\nexecute_query_database&nbsp; &nbsp; &nbsp; | NULL\nattach_query_result_as_file | 0\nquery_result_header &nbsp; &nbsp; &nbsp; &nbsp; | 1\nquery_result_width&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | 256\nquery_result_separator&nbsp; &nbsp; &nbsp; | &nbsp;\nexclude_query_output&nbsp; &nbsp; &nbsp; &nbsp; | 0\nappend_query_error&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | 0\nsend_request_date &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | 2020-08-24 02:40:48.093\nsend_request_user &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | sa\nsent_account_id &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | NULL\nsent_status &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | failed\nsent_date &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | 2020-08-24 02:41:53.000\nlast_mod_date &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | 2020-08-24 02:41:53.290\nlast_mod_user &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | sa<\/pre>\n\n\n\n<p>I used vertical output here so that you don&#8217;t need to scroll sideways to see all columns.<\/p>\n\n\n\n<p>In this case, I&#8217;m showing one failed email. I actually had two failed emails but I decided that it was more concise to show just one result.<\/p>\n\n\n\n<p>In my case, I had used the wrong mail server in the code that sends the email, and the emails failed. As soon as I updated the code to use the correct mail server, all subsequent emails were sent successfully. However, this doesn&#8217;t change those that had already failed, and therefore, they remain in the <code>sysmail_faileditems<\/code> view.<\/p>\n\n\n\n<p>You can query <code>sysmail_unsentitems<\/code> to <a href=\"https:\/\/database.guide\/check-unsent-mail-in-sql-server-t-sql\/\" title=\"Check Unsent Mail in SQL Server (T-SQL)\">return a list of unsent emails<\/a> (ones that are yet to be sent, not necessarily failed). <\/p>\n\n\n\n<p>You can also query <code>sysmail_sentitems<\/code> to <a href=\"https:\/\/database.guide\/return-a-list-of-emails-sent-from-sql-server-database-mail-t-sql\/\" title=\"Return a List of Emails Sent from SQL Server Database Mail (T-SQL)\">get all sent emails<\/a>.<\/p>\n\n\n\n<p>You can also query <code>sysmail_allitems<\/code> to <a href=\"https:\/\/database.guide\/check-the-status-of-all-database-mail-messages-in-sql-server-t-sql\/\">get all emails<\/a> (sent, unsent, failed, and retrying).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;re trying to send mail with SQL Server but it&#8217;s not being delivered, try checking the sysmail_faileditems view.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[10,77,104,61],"class_list":["post-9770","post","type-post","status-publish","format-standard","hentry","category-sql-server","tag-how-to","tag-mssql","tag-send-email","tag-t-sql"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/9770","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/comments?post=9770"}],"version-history":[{"count":5,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/9770\/revisions"}],"predecessor-version":[{"id":10067,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/9770\/revisions\/10067"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=9770"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=9770"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=9770"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}