Avatar of Coloplast
Coloplast
Flag for Denmark asked on

SQL DBMail not working

Hi Guys,
I have a Stored Procedure containing code that will send an email.
This works if I execute the stored procedure directly.
When I start the Stored Procedure from a SQL job the mail never arrives, actually it's never being sent.
I tried to make the account running the job SysAdmin and then it works, but of course this is not to be :-)
So it's a security issue, but which rights do I need to give the user runnning the job?
The MailUser Role in MSDB is not enough!
I'm running SQL 2008R2 on this system.
Thanks
Microsoft SQL Server

Avatar of undefined
Last Comment
Vitor Montalvão

8/22/2022 - Mon
jyothees venkat

Let's find out what the issue is. Try these steps first.

1.Right click on "SQL Server Agent"
2.Then "Properties"
3.Then go to "Alert System" section
4.Tick the "Enable mail profile" box
5.Then "Ok"
6.Restart the SQL Server Agent service
Jose Torres

Do you have a default "public" database mail profile
Manage Profile SecurityTake a look at this from BOL
Database Mail Profiles
lcohan

Your options are to use an existing SA account to run the SP as that account instead of the SQL Agent startup account as indeed  - that is a no-no from security point of view. Just put a command like this in the SQL job step and make sure that login has the sufficient rights:

 EXECUTE AS user = 'special_user'

EXECUTE YourProcerdure

REVERT

Alternatively you could create and use a Proxy account to run that SQL Job step

https://technet.microsoft.com/en-us/library/ms189064(v=sql.105).aspx
http://www.sql-server-performance.com/2010/sql-server-job-proxy-account/
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Vitor Montalvão

Coloplast, do you still need help with this question?
Coloplast

ASKER
I've requested that this question be deleted for the following reason:

None of the answers could solve tour issue.
Vitor Montalvão

Coloplast, you had 3 comments for your question and you didn't give any feedback to them. You should give the opportunity for Experts help you better.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Coloplast

ASKER
Sorry guys, I tried all of your suggestions but nothing is solving the issue.
The mail profile is a public profile.
Do you have any more suggestions?
Thanks
Vitor Montalvão

Did you add the user to the DatabaseMailUserRole in msdb?
More information in this MSDN article.
Coloplast

ASKER
Yes we did.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Vitor Montalvão

May the problem not be with the send mail but with lack of permissions to perform the other actions in the stored procedure?
Do you have any error information logged?
Coloplast

ASKER
I can't find any errors on the issue in any log.
Vitor Montalvão

The job completes successfully?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Coloplast

ASKER
Yes it does.
Vitor Montalvão

What about sysmail_event_log view? Can you see your email information there?
ASKER CERTIFIED SOLUTION
Vitor Montalvão

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.