SQL Server Agent Job Completion E-mail Notification - sql-server

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

Related

Database mail is not working in 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.

Unable to clear SQL Server sysmail queue

Last night due to some bad code, my SQL Server tried sending a million emails. After 10k the emails started going into queue. I immediately stopped the sysmail service using exec sysmail_stop_sp.
Then I deleted all entries from sysmail_allitems, sysmail_faileditems, sysmail_send_retries, sysmail_senditems and sysmail_unsentitems.
I also cleared sysmail_event_log.
Assuming that it would clear my mail queue I started the sysmail services using
exec sysmail_start_sp
But to my surprise the sysmail is still trying to send emails but it cannot find them in sysmail_allitems.
I checked the mail queue using msdb.dbo.sysmail_help_queue_sp and it shows queue with queue_type 'mail' showing those million emails queue.
I rechecked all the tables and they all are empty. Where is this queue is being fetched?
Though no email were sent but still the server tried each and every email which lasted for few hours.
Any idea where the queue is fetched from?
Stop SQL Server Agent
Set it to start manually (using SQL Server Configuration Manager)
Restart SQL Server when you can.
Try to execute:
DECLARE #GETDATE datetime
SET #GETDATE = GETDATE();
EXECUTE msdb.dbo.sysmail_delete_mailitems_sp #sent_before = #GETDATE;
GO
This should remove them all. The reason I mentioned stopping agent is so that it won't take a lock on them and stop you deleting them.
Set Agent back to starting auto.
Restart Agent
And hopefully there won't be a stream of emails coming out. You'll also have to check for any that are queued up in the mail server as well.
Hope that helps.

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.

difference xp_sendmail and sp_send_dbmail proc

Can I ask what is the difference between xp_sendmail and sp_send_dbmail proc? They are both send e-mail message, which may include a query result set attachment, to the specified recipients.....
What is the difference?
xp_sendmail requires a MAPI client installed, such as Outlook, on the server. This is the only option for SQL Server 2000 and before.
sp_send_dbmail is a simple SMTP solution, added for SQL Server 2005+
sp_send_dbmail is by far better.
Another difference between the two is that email message sent using sp_send_dbmail() will be rolled back (not sent) if the transaction is rolled back. This does not happen with email sent using xp_sendmail().
So, if you want the email message to be sent regardless of the end result of the transaction you'll need to use xp_sendmail().
I was sending emails to notify users if an SP was unable to complete it's processing. Of course, I was rolling back the transaction in that event. When I switched to sp_send_dbmail() the transactions that were being rolled back (the very ones I wanted to get an email notification from) stopped sending emails.
we don't control the calling code - it's closed and calls the sp in a transaction. We need a feature in SQL Server to send direct or flush the mail queue.

How do I get dbmail to process items from the queue for SQL Server 2005?

When I use the sp_send_dbmail stored procedure, I get a message saying that my mail was queued. However, it never seems to get delivered. I can see them in the queue if I run this SQL:
SELECT * FROM msdb..sysmail_allitems WHERE sent_status = 'unsent'
This SQL returns a 1:
SELECT is_broker_enabled FROM sys.databases WHERE name = 'msdb'
This stored procedure returns STARTED:
msdb.dbo.sysmail_help_status_sp
The appropriate accounts and profiles have been set up and the mail was functioning at one point. There are no errors in msdb.dbo.sysmail_event_log.
Have you tried
sysmail_stop_sp
then
sysmail_start_sp
I had the same problem and this is how I was able to resolve it.
Go to Sql Agent >> Properties >> Alert System >> Check the Enable box for DBMail and add a profile.
Restart Agent and it works since then.
Hope this helps,
_Ub
Could be oodles of things. For example, I've seen (yes, actually seen) this happen after:
Domain controller reboot
Exchange server reboot
Router outage
Service account changes
SQL Server running out of disk space
So until it happens again, I wouldn't freak out over it.

Resources