Recherche…


Syntaxe

  • sp_send_dbmail [[@profile_name =] 'nom_profil'] [, [@recipients =] 'destinataires [; ... 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 =] 'importance'] [, [@sensitivity =] 'sensibilité'] [, [@file_attachments =] 'pièce jointe [; ... n] '] [, [@query =]' query '] [, [@execute_query_database =]' execute_query_database '] [, [@attach_query_result_as_file =] attach_query_result_as_file] [, [@query_attachment_filename =] query_attachment_filename] [, @query_result_header =] query_result_header] [, [@query_result_width =] query_result_width] [, [@query_result_separator =] 'query_result_separator'] [, [@exclude_query_output =] exclude_query_output] [, [@append_query_error =] append_query_error] [, [@query_no_truncate = ] query_no_truncate] [, [@query_result_no_padding =] @query_result_no_padding] [, [@mailitem_id =] mailitem_id] [OUTPUT]

Envoyer un simple email

Ce code envoie un simple email textuel à [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' 

Envoyer les résultats d'une requête

Cela joint les résultats de la requête SELECT * FROM Users et les envoie à [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; 

Envoyer un email HTML

Le contenu HTML doit être transmis à 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>';

Ensuite, utilisez la variable @html avec l' @body argument . La chaîne HTML peut également être transmise directement à @body , bien que cela puisse rendre le code plus difficile à lire.

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
Sous licence CC BY-SA 3.0
Non affilié à Stack Overflow