Database mail is not working in SQL Server - sql-server

I have created a proc to send mail with attachment (attachment is from query result)
while running manually it working fine
Scheduled proc using Job. Where in job it is not sending any mail. Also job is not failing
Checked in sysmail_allitems but there is no entry for job mails but for manual run I can find entry.
I suspect some permission issue on service account which runs job.

Sending emails through the database requires some special privileges, it's likely that the account the Job executes under doesn't have them.
Check which SQL account is executing the job, then execute:
EXECUTE AS LOGIN = 'TheSQLLoginHere';
EXEC mySendMailSP;
You will see the missing privileges there. You can either supply them or change the account for that Job.

Related

What permissions do you need to run jobs via SQL Agent?

I've googled my issue all weekend, but can't seem to solve the problem
So basically, I have a query, when I run it manually in a query window, it works fine and my job executes, resulting in an email being sent. However, when I set up a schedule, I get the following error:
If you can't read it, the error message says,
"The job failed. The job was invoked by Schedule 9. The last step to run was step 1."
Then underneath it says,
"Executed as user 'adminName(not sa)'. Failed to initialize sqlcmd library with error number -2147467259 (SQLSTATE 42000) (Error 22050). The step failed"
So I log in with user sa, and apparently this user needs to have SQL Agent permissions in the msdb database. According to this screenshot, sa is the owner:
So since sa is the owner, it would mean it has all the SQL Agent related permissions, right? Furthermore, the code I'm using executes from the CompanyDB, so I think perhaps this may be the issue. If I look at properties, under sa, I get this window:
So the CompanyDB is unchecked for user sa, so in order to allow sa to query CompanyDB in SQL Agent, what permissions would it need? I'm assuming this is the issue, but I'm not really sure, I can't seem to figure it out, any help is greatly appreciated.
Jobs should have dbAdmin permissions. That being said, it depends on whether you wrote to the db, update, or just send the email.
The better option would be to create a webservice and call it when you want to send the email.

How to tell when a SSRS Data Driven Subscription has finished running in SQL Server 2014

I have a report running against a Data Driven subscription in SSRS. The subscription runs a report and produces PDFs - about 1000 of them. The process takes about 2 minutes to complete.
I have been kicking this off manually using the following SQL:
EXEC msdb.dbo.sp_start_job #job_name = '<job_name>'
This works, but what I would like to know is when the job has finished. According to what I have read so far, I should be able to run:
exec msdb.dbo.sp_help_job
This lists my job, but it always has a status of 4 (Idle), even while I can see that reports are being produced.
How can I tell when the job has completed and all my reports have been produced?
MSDB shouldn't contain informtaion on the reporting server. The reporting server is seperate from Sql Server Management Server and will only tell you if the job ran or not not what happened in the job. If you have access to the DB I don't know how you have it set up but I have a subscriptions table that I can check with email sent and when it was sent. IF you don't have that you can go onto the reportserver web site and check the subscription and check the status and it should have a date of when it was last sent.
The only way you can access the information in Sql Server Management Studio is by queryng the DB and its tables assuming it is setup correctly.

Which User will be invoking SQL Server Agent Job when using a Windows Task Scheduler to automate the Job to run daily?

I am using SQL Server 2008 to create a job to calculate some values from an external db and store to another db table. A task scheduler has been created for automating this to run daily and update results to the 2nd table. The job is getting failed with SQL message ID 3621 and severity 14. I googled this message id and got to know that it is due to insufficient privileges for the user.
By default which user will be invoking the SQL job by the task scheduler?
If you open the Task properties, you can see what account it is running as:

SQL Server Agent Job Completion E-mail Notification

I've created a Database Mail account (tested and sends with no problem), created an operator, set up an alert on the job I want to monitor, and restarted my SQL Server Agent. But I still am not alerted when the job in question completes. Is there a step that is missing?
Look at your Database Mail log and make sure you don't have any errors sending there. Make sure your internal relay mail servers allow sending from the SQL server's IP address. Also run EXEC msdb.dbo.sysmail_help_status_sp and make sure the dbmail queue status is "Started" or "Receives_Occuring". If the queue is in a Stopped status, restart it with EXEC msdb.dbo.sysmail_stop_sp and EXEC msdb.dbo.sysmail_start_sp
Open the job, go to notifications and select Email, choose the operator you created, and the condition "When the job fails". That should do it.
Was able to get notifications to start sending after a server restart

Problems with sending notification emails from SQL Server 2008

I have a problem setting up email notifications in SQL Server 2008.
I am using SQL Server 2008 to build a data cube. This process consists of several jobs, that are all scheduled to run each night at a specific time. To see whether a job was running without any failure, it is possible to set up email notification.
(source: smilinginthesun.de)
(source: smilinginthesun.de)
Unfortunately, no email is sent even though I think I have set up database mail correctly.
All guides I can found just explains, how to set up database mail (eg. http://blog.sqlauthority.com/2008/08/23/sql-server-2008-configure-database-mail-send-email-from-sql-database/), but thats it.
If I sent a mail to test functionality (via test email button or from T-SQL script) it works just fine and the email arrives. But when using the notification feature in a job, it doesn't even create a sysmail_mailitems or sysmail_log entry.
I have also send an email using sp_notify_operator, successfully. So the operator set up seems to work, too. (Thanks, Joe Stefanelli, for the hint.)
There is a great workaround written for SQL Server 2005, that works for me, too: http://www.howtogeek.com/howto/database/sending-automated-job-email-notifications-in-sql-server-with-smtp/, but why isn't it possible to just use that build-in functionality?
Does anyone knows if there is anything else to do, to make that email notification task work properly? Thanks in advise.
I am late but I saw your post having the same problem.
The solution is to enable a mail profile for alerts.
For this:
Right click on "SQL Server Agent"
Then "Properties".
Then go to "Alert System" section.
Tick the "Enable mail profile" box.
Then "OK".
Restart the SQL Server Agent service.
To send email notifications from agent jobs in SQL Server Agent, the alarm system must be activated. We did that. After restarting the agent, the Database Mail system should be used. Unfortunately, this does not work. The agent was still trying to reach a Mapi profile (as in SQL 2000).
The following registry value wasn't set: "UseDatabaseMail"=dword:00000001
After setting it to 1, the database mail system was used.
You can do that in SQL server, directly:
EXEC master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE', N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent', N'UseDatabaseMail', N'REG_DWORD', 1
I've tried what ZeuG. indicated and still didn't have success... then I restarted the SQL Agent. That's what made the job email notification work for me.
For me, SQL email worked fine when I right-clicked Database Mail and said Send Test Email. However, after setting up alerts for all severities and running RAISERROR (N'Test alert!', 20, 1) WITH LOG;, no email came through. The issue is the service account that SQL Server Agent runs under. Apparently, the virtual account NT Service\SQLSERVERAGENT (or whatever your default may be) does not have permissions to access objects on the network and this includes e-mail.
To fix, change the service to use a local account under the Users group, restart SQL Server agent, and then try running a command that throws an error like the RAISERROR statement above. You should now receive the email.

Resources