Senden von E-Mails mit Database Mail - Amazon Relational Database Service

Die vorliegende Übersetzung wurde maschinell erstellt. Im Falle eines Konflikts oder eines Widerspruchs zwischen dieser übersetzten Fassung und der englischen Fassung (einschließlich infolge von Verzögerungen bei der Übersetzung) ist die englische Fassung maßgeblich.

Senden von E-Mails mit Database Mail

Sie verwenden den gespeicherten Prozess sp_send_dbmail, um E-Mails mit Database Mail zu versenden.

Verwendung

EXEC msdb.dbo.sp_send_dbmail @profile_name = 'profile_name', @recipients = 'recipient1@example.com[; recipient2; ... recipientn]', @subject = 'subject', @body = 'message_body', [@body_format = 'HTML'], [@file_attachments = 'file_path1; file_path2; ... file_pathn'], [@query = 'SQL_query'], [@attach_query_result_as_file = 0|1]';

Die folgenden Parameter sind erforderlich:

  • @profile_name – Der Name des Database Mail-Profils, von dem die Nachricht gesendet werden soll.

  • @recipients – Die durch Semikolons getrennte Liste der E-Mail-Adressen, an die die Nachricht gesendet werden soll.

  • @subject – Der Betreff der Nachricht.

  • @body – Der Text der Nachricht. Sie können auch eine angegebene Variable als Text verwenden.

Die folgenden Parameter sind optional:

  • @body_format— Dieser Parameter wird mit einer deklarierten Variablen verwendet, um E-Mails im HTML Format zu senden.

  • @file_attachments – Die durch Semikolons getrennte Liste von Nachrichtenanhängen. Dateipfade müssen absolute Pfade sein.

  • @query— Eine SQL auszuführende Abfrage. Die Abfrageergebnisse können als Datei angehängt oder im Text der Nachricht enthalten sein.

  • @attach_query_result_as_file – Ob das Abfrageergebnis als Datei angehängt werden soll. Setzen Sie auf 0 für nein, 1 für ja. Der Standardwert ist 0.

Beispiele

Die folgenden Beispiele veranschaulichen, wie Sie E-Mails versenden.

Beispiel das Senden einer Nachricht an einen einzelnen Empfänger
USE msdb GO EXEC msdb.dbo.sp_send_dbmail @profile_name = 'Notifications', @recipients = 'nobody@example.com', @subject = 'Automated DBMail message - 1', @body = 'Database Mail configuration was successful.'; GO
Beispiel das Senden einer Nachricht an mehrere Empfänger
USE msdb GO EXEC msdb.dbo.sp_send_dbmail @profile_name = 'Notifications', @recipients = 'recipient1@example.com;recipient2@example.com', @subject = 'Automated DBMail message - 2', @body = 'This is a message.'; GO
Beispiel — ein SQL Abfrageergebnis als Dateianhang zu senden
USE msdb GO EXEC msdb.dbo.sp_send_dbmail @profile_name = 'Notifications', @recipients = 'nobody@example.com', @subject = 'Test SQL query', @body = 'This is a SQL query test.', @query = 'SELECT * FROM abc.dbo.test', @attach_query_result_as_file = 1; GO
Beispiel des Sendens einer Nachricht im HTML Format
USE msdb GO DECLARE @HTML_Body as NVARCHAR(500) = 'Hi, <h4> Heading </h4> </br> See the report. <b> Regards </b>'; EXEC msdb.dbo.sp_send_dbmail @profile_name = 'Notifications', @recipients = 'nobody@example.com', @subject = 'Test HTML message', @body = @HTML_Body, @body_format = 'HTML'; GO
Beispiel des Sendens einer Nachricht mit einem Trigger, wenn ein bestimmtes Ereignis in der Datenbank auftritt
USE AdventureWorks2017 GO IF OBJECT_ID ('Production.iProductNotification', 'TR') IS NOT NULL DROP TRIGGER Purchasing.iProductNotification GO CREATE TRIGGER iProductNotification ON Production.Product FOR INSERT AS DECLARE @ProductInformation nvarchar(255); SELECT @ProductInformation = 'A new product, ' + Name + ', is now available for $' + CAST(StandardCost AS nvarchar(20)) + '!' FROM INSERTED i; EXEC msdb.dbo.sp_send_dbmail @profile_name = 'Notifications', @recipients = 'nobody@example.com', @subject = 'New product information', @body = @ProductInformation; GO