Unable to clear SQL Server sysmail queue - sql-server

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.

Related

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.

SQL Server Job Failure Notifications on Set Intervals

I have a SQL Server job that sends out a notification every time it fails. I run this job every 5 minutes. If something the job needs has its state changed at 10:00 PM at night and the problem isn't discovered until the next morning, there would be over 100 e-mails sent with the same failure notification.
Is there anyway for you to throttle the number of e-mails sent? For example, I would like SQL Server to send out an e-mail notification on the hour if any of the 12 scheduled runs during the hour fails, but no more than one e-mail should be sent (even if there are multiple failures).
You could create a simple table with the job name and a bit column indicating whether an email has been sent successfully, then check that column as part of the alert.
DECLARE Notifications TABLE
(
ID int IDENTITY (1, 1) PRIMARY KEY NOT NULL,
JobName varchar(50),
MailSent bit DEFAULT 0
)
In SSMS select Database Mail under Management, right-click and Configure Database Mail. After that's complete send a test mail using the new profile to confirm that works.
Next, open the properties of the SQL Server Agent. On the Alert System page enable the mail profile. Restart the SQL Server Agent for the change to take effect.
Under the SQL Server agent create Operators to send messages to.
Finally, in your job select Notifications and set the Operator to send to when the job completes.

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

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