Let’s say you send an email from inside a SQL instance using something like the following:
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'SQLZEALOTS_Manager',
@recipients = 'Lkiran@xxx.com',
@body = 'This email Sent',
@subject = 'SqLZeaLoTs' ;
And recipient DOES successfully receive the email.
Now for one reason or another, they want the original email sent again (not an execution of msdb.dbo.sp_send_dbmail, but instead to basically re queue the original email to be sent again).
Step 1: Execute the following:
SELECT mailItem_id
FROM [msdb].[dbo].[sysmail_mailitems]
WHERE subject = 'SqLZeaLoTs'
ORDER BY sent_date desc
Copy the mailItem_id value for the specific email you want to requeue, in this example 2738274.
Use msdb
GO
DECLARE @rc INT
,@sendmailxml VARCHAR(max)
,@mailID INT = 2738274 -- This is from the mailItem_ID column in [msdb].[dbo].[sysmail_mailitems] for the mail you want to resend. 2738274 is just an example.
SET @sendmailxml = '<requests:SendMail xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schemas.microsoft.com/databasemail/requests RequestTypes.xsd" xmlns:requests="http://schemas.microsoft.com/databasemail/requests"><MailItemId>'
+ CONVERT(NVARCHAR(20), @mailID) + N'</MailItemId></requests:SendMail>'
-- Send the send request on queue.
EXEC @rc = sp_SendMailQueues @sendmailxml
IF @rc <> 0
BEGIN
RAISERROR(14627, 16, 1, @rc, 'send mail')
END
If the query completes successfully, it has queued the original email again and the original recipient will receive another email with exactly the same subject and body.
Step 2 has only been confirmed to work on SQL 2008 R2 SP3.
Note: If you want to try it for later SQL Server versions, you may need to verify the code of step 2 by following the steps.
Script out the system procedure: msdb.dbo.sp_send_dbmail and look for the section near the bottom that executes sp_SendMailQueues. The most important piece to verify is the “SET @sendmailxml…” statement. The reason for this is that sp_SendMailQueues sends the email to a queue that is monitored by a broker that calls DatabaseMail.exe. It is entirely possible that different versions of SQL may have different versions of DatabaseMail.exe that requires the XML to be formatted differently so you want to make sure that the XML is formatted for the version required.
Please try this out on your environment and feedback if you have any comments.
I’d like to grow my readership. If you enjoyed this blog post, please share it with your friends!