I have a query that will impersonate as a specific domain user to send email with attachment. This impersonation is required as it needs to read a file at some other server to be able to attach the file to the email.
The query is as follows
EXECUTE AS login='domain\user' -- To impersonate EXEC msdb.dbo.sp_send_dbmail @profile_name = 'profile_name', @recipients = @recipient, @copy_recipients = @cc, @blind_copy_recipients = @bcc, @subject = @subj, @body = @mail_body, @body_format = 'HTML', @from_address = 'email@example.com', @file_attachments = @files; REVERT
Sometimes I get error when running the query with error message as:
Could not obtain information about Windows NT group/user 'domain\user', error code 0x54b.
The weird thing is that, if I execute this query multiple times, it will work and it will send out the email.
What could possibly be wrong?
SQL Server version: 11.0.2100