'Failure to execute msdb.dbo.sp_send_dbmail
I am getting this error:
Msg 229, Level 14, State 5, Procedure sp_send_dbmail, Line 1
The EXECUTE permission was denied on the object 'sp_send_dbmail', database 'msdb', schema 'dbo'.
The relevant part of the code:
/****** Object: StoredProcedure [dbo].[dbo.STATUSCHANGE_EMAILALERT] ******/
EXEC msdb.dbo.sp_send_dbmail
@recipients = '[email protected]', -- Group Email
@subject = 'Employee Status Update',
@profile_name ='[email protected]', -- Setup the profile name group
@body = @body,
@body_format = 'HTML';
Solution 1:[1]
Found nice and easy fix that worked for me here:
If your SQL applications can’t send email using database mail (I assume you already have DBMail Account and Profile setup), there are two things to set:
- SQL MANAGEMENT STUDIO > MANAGEMENT > DATABASE MAIL > right click and select CONFIGURE… > select MANAGE PROFILE SECURITY > SQL MANAGEMENT
- put a check on PUBLIC option
- click on DEFAULT PROFILE and set it to YES
- STUDIO > DATABASES > SYSTEM DATABASES > right click on MSDB and select NEW QUERY > then enter > grant execute on sp_send_dbmail to public and click OK
Solution 2:[2]
To send Database mail, users must be a user in the msdb database and a member of the DatabaseMailUserRole database role in the msdb database. To add msdb users or groups to this role use SQL Server Management Studio or execute the following statement for the user or role that needs to send Database Mail:
EXEC msdb.dbo.sp_addrolemember @rolename = 'DatabaseMailUserRole'
,@membername = '<user or role name>';
GO
Solution 3:[3]
Grant execute permission on sp_send_dbmail to the user executing the stored procedure, or add them to the role msdb.DatabaseMailUser .
Solution 4:[4]
Ok, just to add to this topic since this was really good information, but still didn't completely solve my problem. If I ran the query in SSMS, it worked once I was granted permission to execute the sp_send_dbmail procedure in msdb. However, when a job was running as my user, it would still fail.
Read through a lot of stuff to get to the conclusion that you need to make sure the sid for the owner in your DB matches the owner sid in the master DB:
--To get owner SID recorded in the master database for the current database
SELECT owner_sid FROM sys.databases WHERE database_id=DB_ID()
--To get the owner SID recorded for the current database owner
SELECT sid FROM sys.database_principals WHERE name=N'dbo'
Even though I had given access to the msdb and execute rights on the sp_send_dbmail, it was still having issues related to the database being untrustworthy and that the owner sids didn't match. Consequently, I had to the Trustworthy on for the database I was running in and fix the ownership issue:
ALTER DATABASE my_db SET TRUSTWORTHY ON;
ALTER AUTHORIZATION ON Database::my_db TO [domain\user];
I had to go through a lot of ferreting around to finally find this write-up which is much more enlightening.
Solution 5:[5]
I accidentally changed the "Run As" option for the SQL Agent Job - Step from "[User]" to "(Not Specified)". That caused my Job to start working.
Solution 6:[6]
I found here the right solution for me.
You need to add your user (let's say mailuser) to msdb and give this user rights to run dbmail:
USE msdb;
CREATE USER mailuser FOR LOGIN mailuser;
EXEC sp_addrolemember 'DatabaseMailUserRole', 'mailuser'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Saurabh R S |
| Solution 3 | Alex K. |
| Solution 4 | TChilders |
| Solution 5 | Mark Nitsche |
| Solution 6 | mar k |
