Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Script to check if mail profile is enabled in SQL Server

Does anyone have a script (TSQL or PowerShell) to check if mail profile is enabled on a SQL Server?

I noticed we are not receiving mails from a bunch of production servers, because mail profile NOT being enabled.

I need a script to run across all our prod boxes to check if this feature is enabled. I did some research, but failed to find a script. Thank you so much.

enter image description here

like image 345
RaviLobo Avatar asked Oct 24 '25 09:10

RaviLobo


2 Answers

I asked a similar question here on dba.stackexchange.com.

From that help, I created this script which I have on GitHub which i set up a job to alert me when this is changed.

Here's the part you seem to care about:

/* 
   Confirm the Database Mail account and profile is configured correctly 
*/ 

DECLARE @DatabaseMail VARCHAR(255);  

SELECT   
    ProfileName = smp.name  
    ,AccountName = sma.name  
    ,AccountFromAddress = sma.email_address  
    ,AccountReplyTo = sma.replyto_address  
    ,SMTPServer = sms.servername  
    ,SMTPPort = sms.port  
FROM msdb.dbo.sysmail_account sma  
    INNER JOIN msdb.dbo.sysmail_profileaccount smpa ON sma.account_id = smpa.account_id  
    INNER JOIN msdb.dbo.sysmail_profile smp ON smpa.profile_id = smp.profile_id  
    INNER JOIN msdb.dbo.sysmail_server sms ON sma.account_id = sms.account_id;

/*  
    Confirm SQL Server Agent is configured to use Database Mail correctly  
*/  
DECLARE @res TABLE  
(  
    Value VARCHAR(255)  
    , Data VARCHAR(255)  
);  
INSERT INTO @res  
EXEC master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE', N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent', N'UseDatabaseMail';  
INSERT INTO @res  
EXEC master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE', N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent', N'DatabaseMailProfile';  
IF (  
        SELECT COUNT(*)  
        FROM @res r  
        WHERE r.Value = 'UseDatabaseMail' AND r.Data = 1  
    ) = 1 AND   
    (  
        SELECT COUNT(*)  
        FROM @res r  
        WHERE r.Value = 'DatabaseMailProfile' AND r.Data IS NOT NULL  
    ) = 1  
SET @DatabaseMail = 'Configured'  
ELSE  
SET @DatabaseMail = 'Not Configured';  

select @DatabaseMail

Or, the limited results on what the profile is, and if you have it enabled at the agent level:

DECLARE @res TABLE  
(  
    Value VARCHAR(255)  
    , Data VARCHAR(255)  
);  

INSERT INTO @res  
EXEC master.dbo.xp_instance_regread
         @rootkey      = N'HKEY_LOCAL_MACHINE'
       , @key          = N'Software\Microsoft\MSSQLServer\SQLServerAgent'
       , @value_name   = N'DatabaseMailProfile';

INSERT INTO @res  
EXEC sys.xp_instance_regread
         @rootkey      = N'HKEY_LOCAL_MACHINE'
       , @key          = N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent'
       , @value_name   = N'UseDatabaseMail'

SELECT *
FROM @res;
like image 111
S3S Avatar answered Oct 25 '25 23:10

S3S


Here's the final code. I removed the additional info from scsimon's code.

DECLARE @DatabaseMail VARCHAR(255);  

/*  
    Confirm SQL Server Agent is configured to use Database Mail correctly  
*/  
DECLARE @res TABLE  
(  
    Value VARCHAR(255)  
    , Data VARCHAR(255)  
);  
INSERT INTO @res  
EXEC master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE', N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent', N'UseDatabaseMail';  
INSERT INTO @res  
EXEC master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE', N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent', N'DatabaseMailProfile';  
IF (  
        SELECT COUNT(*)  
        FROM @res r  
        WHERE r.Value = 'UseDatabaseMail' AND r.Data = 1  
    ) = 1 AND   
    (  
        SELECT COUNT(*)  
        FROM @res r  
        WHERE r.Value = 'DatabaseMailProfile' AND r.Data IS NOT NULL  
    ) = 1  
SET @DatabaseMail = 'Configured'  
ELSE  
SET @DatabaseMail = 'Not Configured';  

select @DatabaseMail EnableMailProfile 
like image 33
RaviLobo Avatar answered Oct 25 '25 22:10

RaviLobo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!