2

I am having a problem sending email from one of my servers.

I want to use DatabaseMail and/or msdb.dbo.sp_send_dbmail

I have other production servers that use all the same databaseMail settings and the same SMTP server, and they are all working fine, only this one particular server is not sending emails via Sql Server and I need to find out why.

The first thing I test is the connection to the SMTP server, and for testing that I use Powershell to send an email, and it works fine!!!!

Here is the script as how to send emails using Powershell.

$smtpServer = "200.1.1.223"
$smtpPort = 25
$emailFrom = "donotreply@xxxxx.co.uk"
$emailTo = "mmiorelli@xxxxx.co.uk"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Port = $smtpPort
$subject = "ccatsql" 
$body = "test email from ccatsql " 
$smtp.Send($emailFrom, $emailTo, $subject, $body)

and that works fine.

the problem that I am having in that server is DatabaseMail didn't start when an email is queued

when I query the sysmail_event_log I don't see any event related to the emails I have just sent (attempted to).

SELECT * FROM msdb.dbo.sysmail_event_log order by log_date desc 

when I query the sysmail_unsentitems I can see all my emails are there, in an unsert state.

SELECT * FROM msdb.dbo.sysmail_unsentitems 
ORDER BY send_request_date DESC

enter image description here

This is the piece of code that I am mostly using to test the email sending. It shows how to send the result of a query by email, but in this case I made the query very simple because I need to get the email working first.

-===========================================================================
-- Failed to initialize sqlcmd library with error number -2147467259
-- that was caused because I had left a piece of rubish code from a previous query
-- and internally it was saying: Invalid column name 'sintMarketID'.
-- but I could only see this using the profile.
--==========================================================================


set deadlock_priority high
set transaction isolation level repeatable read

declare @sub varchar(150)
declare @radhe int
select @sub = '-- Number of rows affected by Hare Krishna job run at ' 
               + CAST(convert(datetime, getdate(), 100)AS VARCHAR)
--print @sub


    EXEC msdb.dbo.sp_send_dbmail 
        @profile_name = 'DBA', -- Change profile name accordingly
        @recipients='mmiorelli@xxxxx.co.uk', -- change mail address accordingly
        @subject = @sub,
        @body_format = 'TEXT',
        @importance='High',
        @sensitivity='Private',
        @file_attachments=NULL,
        @execute_query_database='MSDB',
        @query_no_truncate=0,
        @query_result_header = 1,
        @mailitem_id= @Radhe OUTPUT,
        @query= '


SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
SET NOCOUNT ON

DECLARE @SQL VARCHAR(MAX)

SELECT @SQL = ''

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
select getdate()
''

EXEC(@SQL)

'

there are similar questions without a conclusion:

DatabaseMail process is shutting down

What is missing?

  • 1
    What's the output of msdb.dbo.sysmail_faileditems ? – Sean Gallardy Sep 26 '17 at 0:44
  • when I run select * from msdb.dbo.sysmail_faileditems I see all the emails there with failed as the sent_status – Marcello Miorelli Sep 26 '17 at 13:33
2

I checked which .Net versions I had installed

then I installed .NET 3.5 and all started to work nicely.

enter image description here

I believe it is poorly documented (or at least for me) the fact that DatabaseMail needs .NET 3.5.

SQL Server 2016 - Database Mail doesn't work without .NET 3.5

How can I see the current Database Mail configuration?

EXEC msdb.dbo.sysmail_help_configure_sp;
EXEC msdb.dbo.sysmail_help_account_sp;
EXEC msdb.dbo.sysmail_help_profile_sp;
EXEC msdb.dbo.sysmail_help_profileaccount_sp;
EXEC msdb.dbo.sysmail_help_principalprofile_sp;

EXEC msdb.dbo.sysmail_help_account_sp

this link below has also helped me specially when I need to copy the database mail settings from one server into another:

The database mail configuration saved into a temp table

I got the following script that helps troubleshooting databasemail basics.

Pay attention to the comments and run one step at a time.

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 =  '2017-09-28',
  @sent_status = 'failed';
  • Upvoted not for the .NET 3.5 part of the answer, which I hope is no longer valid for newer versions of SQL Server, but for the comprehensive list of troubleshooting queries. They are a very useful reference. – ShawnMartin May 24 at 17:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.