Solved

SQL server Database Mail Error-SQL Server 2008 R2

Posted on 2016-09-29
11
22 Views
Last Modified: 2016-10-22
Hi there,
  I am receiving the following error when i try to run the Agent Job which tries to send an email.

Executed as user: UserXXX. The EXECUTE permission was denied on the object 'sp_send_dbmail', database 'msdb', schema 'dbo'. [SQLSTATE 42000] (Error 229).  The step failed.

Just for the test purpose, I have assigned the user-UserXXX to sysadmin privileges but still no luck.

I am running the job step Advanced->"Run as user -UserXXX' to execute this Job.

I have manually added the user UserXXX to the msdb database role 'DatabaseMailUserRole' also.

Have checked the sp_send_dbmail has the grant permission and the GranteeName- 'DatabaseMailUserRole'

Please let me know how to solve this issue?

thanks
Deepak
0
Comment
Question by:Deepak Kumar
  • 6
  • 5
11 Comments
 
LVL 45

Expert Comment

by:Vitor Montalvão
ID: 41821302
Did you enable the Database Mail?
You can check that by running the following command:
exec sp_configure 'Database Mail XPs'

Open in new window

0
 
LVL 45

Expert Comment

by:Vitor Montalvão
ID: 41821308
If isn't enabled you can enable it by using the Database Mail Configuration Wizard:
DatabaseMailConfigure.PNG
0
 

Author Comment

by:Deepak Kumar
ID: 41821330
Hi Vitor,
Thank you for your reply! When i ran the exec sp_configure 'Database Mail XPs'.I got the output.

name      minimum      maximum      config_value      run_value
Database Mail XPs      0      1      1      1

Please Note: Already database mail is working for other jobs which sends the DB mail.
I have configured this job to run as different User-UserXXX  using Advanced option in Job steps, because of this, I am getting this error.

thanks
Deepak
0
 
LVL 45

Expert Comment

by:Vitor Montalvão
ID: 41821346
Already database mail is working for other jobs which sends the DB mail.
What this job has different from the others? Only the user? The way it's sending emails? Other things?
0
 

Author Comment

by:Deepak Kumar
ID: 41821359
The user has been changed -"Run as user" and Job Owner "to different user which again has sysadmin privileges".
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Deepak Kumar
ID: 41823244
Hi Vitor,
  I have executed this command which was found in forum , after which the job was executed as the user which i gave in "Run as user"

GRANT EXECUTE ON object::sp_send_dbmail TO DatabaseMailUserRole.

but i have few questions on the internal calls by SQL server when the db mail job had happened.

EXEC @rc = msdb.dbo.sysmail_verify_profile_sp
EXEC @retValue = sp_SendMailMessage @contract_name, @message_type, @message_data
EXEC @rc = sp_SendMailQueues @sendmailxml
EXEC @rc = msdb.dbo.sysmail_help_configure_value_sp @parameter_name = N'DatabaseMailExeMinimumLifeTime',
EXEC msdb.dbo.sysmail_help_configure_value_sp @parameter_name = N'ReadFromConfigurationFile',
                                                  @parameter_value = @parameter_value OUTPUT

Few of the above mentioned queries were captured in SQL profiler where the Login Name still shows as 'SA'

I have made the job owner to other user and also the run as "different user" but still i can see the internal queries were executed as 'SA'

Can you tell if i disable the 'SA' login what would the Login Name for this internal calls as opposed to 'SA' now?

thanks
Deepak
0
 
LVL 45

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 500 total points
ID: 41823257
Those are undocumented stored procedures and only users with sysadmin role can execute them. With that said this might mean that internally those SPs are using SA to run.

Documented SP is sp_send_dbmail and request less privileges.
0
 

Author Comment

by:Deepak Kumar
ID: 41827517
Hi Vitor,
I have executed the Same job with the SA enbaled once and SA login disabled once and still found that the undocumented stored procedures related to DB mail are logged as 'SA' in  Login Name.

My question on this,
1)Is this the intended behaviour of SQL server to execute these undocumented SP's under the 'SA' Login?

2)Even after the 'SA' login is disabled, why the login name shows as 'SA' for these internal SP's?

Please help me on this as I must explain it to client with brief information.

thanks
Deepak
0
 
LVL 45

Expert Comment

by:Vitor Montalvão
ID: 41827528
I don't know the answer for question 1 since I never worked with those SPs.
For question 2, SA sometimes can mean SQL Server account.
0
 

Accepted Solution

by:
Deepak Kumar earned 0 total points
ID: 41847693
Hi Vitor,
   I have found why the System internall calls are logged as 'SA'. The main reason for this is due to the ownership of the msdb database which has the internal stored procedures.

I have tested it by changing the msdb database(in test environment) to other user who have sysadmin privileges . To my surprise those calls were called with the new database owner.

Anyway thanks vitor for your help!!

thanks
Mohan
0
 

Author Closing Comment

by:Deepak Kumar
ID: 41855063
I have given the test on my own to find out the real cause.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now