Buscar..


Sintaxis

  • sp_send_dbmail [[@profile_name =] 'profile_name'] [, [@recipients =] 'recipients [; ... n] '] [, [@copy_recipients =]' copy_recipient [; ... n] '] [, [@blind_copy_recipients =]' blind_copy_recipient [; ... n] '] [, [@from_address =]' from_address '] [, [@reply_to =]' reply_to '] [, [@subject =]' subject '] [, [@body =]' body ' ] [, [@body_format =] 'body_format'] [, [@importance =] 'importancia'] [, [@sensitivity =] 'sensibilidad'] [, [@file_attachments =] 'archivo adjunto [; ... n] '] [, [@query =]' query '] [, [@execute_query_database =]' execute_query_database '] [, [@attach_query_result_as_file =] attach_query_result_as_archivo_ @query_result_header =] query_result_header] [, [@quical_result_wult.width =] query_result_width] [, [@query_result_separator =] 'persona_ascarilla_comultada_gamino_particulo_Personal_Parque_gaminos_Parque_reparador_Personal_Personal_Personal_Personal_Personal_Personal_Parque de la mano]: ] query_no_truncate] [, [@query_result_no_padding =] @query_result_no_padding] [, [@mailitem_id =] mailitem_id] [OUTPUT]

Enviar email simple

Este código envía un correo electrónico de solo texto a [email protected]

EXEC msdb.dbo.sp_send_dbmail  
    @profile_name = 'The Profile Name',  
    @recipients = '[email protected]',  
    @body = 'This is a simple email sent from SQL Server.',  
    @subject = 'Simple email' 

Enviar los resultados de una consulta

Esto adjunta los resultados de la consulta SELECT * FROM Users y los envía a [email protected]

EXEC msdb.dbo.sp_send_dbmail  
    @profile_name = 'The Profile Name',  
    @recipients = '[email protected]',  
    @query = 'SELECT * FROM Users',  
    @subject = 'List of users',  
    @attach_query_result_as_file = 1; 

Enviar correo electrónico HTML

El contenido HTML se debe pasar a sp_send_dbmail

SQL Server 2012
DECLARE @html VARCHAR(MAX);
SET @html = CONCAT
(
    '<html><body>',
    '<h1>Some Header Text</h1>',
    '<p>Some paragraph text</p>',
    '</body></html>'
)
SQL Server 2012
DECLARE @html VARCHAR(MAX);
SET @html =
    '<html><body>' +
    '<h1>Some Header Text</h1>' +
    '<p>Some paragraph text</p>' +
    '</body></html>';

Luego usa la variable @html con el @body argument . La cadena HTML también se puede pasar directamente a @body , aunque puede hacer que el código sea más difícil de leer.

EXEC msdb.dbo.sp_send_dbmail 
    @recipients='[email protected]',  
    @subject = 'Some HTML content',  
    @body = @html,  
    @body_format = 'HTML';  


Modified text is an extract of the original Stack Overflow Documentation
Licenciado bajo CC BY-SA 3.0
No afiliado a Stack Overflow