Quantcast
Channel: Forum SQL Server Database Engine
Viewing all articles
Browse latest Browse all 15889

Send a email with Agent job MSSQL

$
0
0

hello

I need help

I'm trying to send an email using the MSSQL agent work when trying to send mail to the domain user initiating agent services DOAIN MSSQL \ SqlService

error message

Msg 22050, Level 16, State 1, Line 0
Error formatting query, probably invalid parameters
Msg 14661, Level 16, State 1, Procedure sp_send_dbmail, Line 504
Query execution failed: Msg 15404, Level 16, State 19, Server MTCSERVER1, Line 1
Could not obtain information about the group or user Windows NT 'MTCENTER \ SqlService', error code 0x54b.

I run with SQL authentication user and sends it without problems

this is the query 

DECLARE @recipients1 AS VARCHAR(max);  
 SET @recipients1 = 'Alonso.fernandez@redit.com'
 EXEC msdb.dbo.sp_send_dbmail  
 @profile_name = 'MTC Gmail',  
 @blind_copy_recipients = @recipients1,  
 @query_result_separator= ' ',  
 @subject = 'PRUEBA REDIT',  
 @query_result_header= 1,  
 @importance= 'High',  
 @sensitivity= 'Confidential',  
 @query_result_no_padding = 1,  
 @body = 'Select Name sys job',
 @attach_query_result_as_file = 0,  
 @query = ' select name from msdb.sys.databases'

the problem is with the parameter @query 

if the option is executed by
EXECUTE AS USER = 'dbo'

and then all the query ejecute fine only if executed in the database MSDB

if created as stored procedure send the same error  

CREATE PROCEDURE [dbo].[sp_PRUEBAREDIT_1]    
AS    
BEGIN    
  
DECLARE @recipients1 AS VARCHAR(max);  
 SET @recipients1 = 'Alonso.fernandez@redit.com'
 EXEC msdb.dbo.sp_send_dbmail  
 @profile_name = 'MTC Gmail',  
 @blind_copy_recipients = @recipients1,  
 @query_result_separator= ' ',  
 @subject = 'PRUEBA REDIT',  
 @query_result_header= 1,  
 @importance= 'High',  
 @sensitivity= 'Confidential',  
 @query_result_no_padding = 1,  
 @body = 'Select Name sys job',
 @attach_query_result_as_file = 0,  
 @query = ' select name from msdb.sys.databases'  
    
END

the domain user that starts the agent job has sysadmin permissions in SQL and windows Server 

MSSQL server 2008R2 SP1 

Windows Server 2008 R2

could help with this problem

thanks 


Viewing all articles
Browse latest Browse all 15889

Trending Articles