Notes on debugging Database Mail problems

6

July 29, 2013 by Kenneth Fisher

I’ve had to debug database mail problems several times over the last year or so. Below is a collection of the stored procedures and queries that I’ve ended up using. I’ve added notes to most of them to help explain and work with them.

USE msdb
GO

-- Check that the service broker is enabled on MSDB. 
-- Is_broker_enabled must be 1 to use database mail.
SELECT is_broker_enabled FROM sys.databases WHERE name = 'msdb';
-- Check that Database mail is turned on. 
-- Run_value must be 1 to use database mail.
-- If you need to change it this option does not require
-- a server restart to take effect.
EXEC sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
EXEC sp_configure 'Database Mail XPs';

-- Check the Mail queues
-- This system stored procedure lists the two Database Mail queues.  
-- The optional @queue_type parameter tells it to only list that queue.
-- The list contains the length of the queue (number of emails waiting),
-- the state of the queue (INACTIVE, NOTIFIED, RECEIVES_OCCURRING, the 
-- last time the queue was empty and the last time the queue was active.
EXEC msdb.dbo.sysmail_help_queue_sp -- @queue_type = 'Mail' ;

-- Check the status (STARTED or STOPPED) of the sysmail database queues
-- EXEC msdb.dbo.sysmail_start_sp -- Start the queue
-- EXEC msdb.dbo.sysmail_stop_sp -- Stop the queue
EXEC msdb.dbo.sysmail_help_status_sp;

-- Check the different database mail settings.  
-- These are system stored procedures that list the general 
-- settings, accounts, profiles, links between the accounts
-- and profiles and the link between database principles and 
-- database mail profiles.
-- These are generally controlled by the database mail wizard.

EXEC msdb.dbo.sysmail_help_configure_sp;
EXEC msdb.dbo.sysmail_help_account_sp;
-- 	Check that your server name and server type are correct in the
--		account you are using.
--	Check that your email_address is correct in the account you are
--		using.
EXEC msdb.dbo.sysmail_help_profile_sp;
--	Check that you are using a valid profile in your dbmail command.
EXEC msdb.dbo.sysmail_help_profileaccount_sp;
--	Check that your account and profile are joined together
--		correctly in sysmail_help_profileaccount_sp.
EXEC msdb.dbo.sysmail_help_principalprofile_sp;

-- I’m doing a TOP 100 on these next several queries as they tend
-- to contain a great deal of data.  Obviously if you need to get
-- more than 100 rows this can be changed.
-- Check the database mail event log.
-- Particularly for the event_type of "error".  These are where you
-- will find the actual sending error.
SELECT TOP 100 * 
FROM msdb.dbo.sysmail_event_log 
ORDER BY last_mod_date DESC;

-- Check the actual emails queued
-- Look at sent_status to see 'failed' or 'unsent' emails.
SELECT TOP 100 * 
FROM msdb.dbo.sysmail_allitems 
ORDER BY last_mod_date DESC;

-- Check the emails that actually got sent. 
-- This is a view on sysmail_allitems WHERE sent_status = 'sent'
SELECT TOP 100 * 
FROM msdb.dbo.sysmail_sentitems 
ORDER BY last_mod_date DESC;

-- Check the emails that failed to be sent.
-- This is a view on sysmail_allitems WHERE sent_status = 'failed'
SELECT TOP 100 * 
FROM msdb.dbo.sysmail_faileditems 
ORDER BY last_mod_date DESC

-- Clean out unsent emails
-- Usually I do this before releasing the queue again after fixing the problem.
-- Assuming of course that I don't want to send out potentially thousands of 
-- emails that are who knows how old.
-- Obviously can be used to clean out emails of any status.
EXEC msdb.dbo.sysmail_delete_mailitems_sp  
	@sent_before =  '2009-11-10',
	@sent_status = 'unsent';

6 thoughts on “Notes on debugging Database Mail problems

  1. Kathy says:

    Thanks so much for this very helpful post. In using it I discovered an odd bug that nobody has reported to me, but looks like its been happening for quite some time. I have a sp that sends emails to a number of recipients when it runs (usually between 60 and 80), and the last 2 (or 2 within the last 4) emails in each session have been failing. The error log exception says there was a mail server failure. Looking at the log it appears they attempt a resend once, but then quit. In the cases where there is a sucessful email following the 2 failures, that one failed the first time too but was sent on the resend attempt.
    I am just starting to try and figure out what could be causing this, and wondered if you had any suggestions of places to look.

    • Not really. I would contact your mail administrator and see if it’s something on their end. Possibly because of the large number of emails going out at once. Generally I would confirm that the emails are valid emails but it sounds like it’s a random failure and the emails are working on other occasions. The other possibility is to see if you can modify your app to send 2 emails with 30-40 emails or something similar. Glad you liked the post. Let me know when you figure out what happened. 🙂

  2. Gordon613 says:

    Thanks these were very useful, and helped me realize that the reason that my database had stopped sending emails (through gmail) was that the gmail password has been changed and the database was not updated. I used your EXEC msdb.dbo.sysmail_help_account_sp; to identify the account number and then EXECUTE msdb.dbo.sysmail_update_account_sp @account_id=1,@username=’XXX@gmail.com’, @password=”YYY” to change the password

  3. cefrein says:

    Needed to enable .NET 35 on the server. If you attempt to run databasemail.exe by double clicking on it, you either get a window or an error about .NET. After I installed .NET, mails started going out.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 2,134 other followers

Follow me on Twitter

ToadWorld Pro of the Month November 2013
%d bloggers like this: