Microsoft SQL Server
dbmail
Sök…
Syntax
- sp_send_dbmail [[@profile_name =] 'profile_name'] [, [@recipients =] "mottagare [; ... 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 =] 'vikt'] [, [@sensitivity =] 'sensitivity'] [, [@file_attachments =] 'bilaga [; ... n] '] [, [@query =]' query '] [, [@execute_query_database =]' execute_query_database '] [, [@attach_query_result_as_file =] attach_query_result_as_file] [, [@query_attachment_filename =] quilen_ame] @query_result_header =] query_result_header] [, [@query_result_width =] query_result_width] [, [@query_result_separator =] 'query_result_separator'] [, [@ exclude_query_output =] exclude_query_output] [query_query [=] ] query_no_truncate] [, [@query_result_no_padding =] @query_result_no_padding] [, [@mailitem_id =] mailitem_id] [OUTPUT]
Skicka enkel e-post
Den här koden skickar ett enkelt text-e-postmeddelande till [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'
Skicka resultat av en fråga
Detta bifogar resultaten från frågan SELECT * FROM Users
och skickar den till [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;
Skicka HTML-e-post
HTML-innehåll måste skickas till 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>';
@html
variabeln med @body argument
. HTML-strängen kan också skickas direkt till @body
, även om den kan göra koden svårare att läsa.
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
Licensierat under CC BY-SA 3.0
Inte anslutet till Stack Overflow