Database Mail을 사용하여 이메일 메시지 보내기 - Amazon Relational Database Service

Database Mail을 사용하여 이메일 메시지 보내기

sp_send_dbmail 저장 프로시저를 사용하여 Database Mail을 통해 이메일 메시지를 보낼 수 있습니다.

사용량

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]';

다음 파라미터는 필수 파라미터입니다.

  • @profile_name – 메시지를 보낼 Database Mail 프로파일의 이름입니다.

  • @recipients – 메시지를 보낼 이메일 주소의 세미콜론으로 구분된 목록입니다.

  • @subject – 메시지의 제목입니다.

  • @body – 메시지의 본문입니다. 선언된 변수를 본문으로 사용할 수도 있습니다.

다음 파라미터는 선택적입니다.

  • @body_format – 이 파라미터는 HTML 형식으로 이메일을 보내도록 선언된 변수와 함께 사용됩니다.

  • @file_attachments – 메시지 첨부 파일의 세미콜론으로 구분된 목록입니다. 파일 경로는 절대 경로여야 합니다.

  • @query – 실행할 SQL 쿼리입니다. 쿼리 결과는 파일로 첨부되거나 메시지 본문에 포함될 수 있습니다.

  • @attach_query_result_as_file – 쿼리 결과를 파일로 첨부할지 여부를 나타냅니다. 아니요(No)인 경우 0, 예(Yes)인 경우 1로 설정합니다. 기본값은 0입니다.

예시

다음 예에서는 이메일 메시지를 보내는 방법을 보여 줍니다.

예 한 명의 수신자에게 메시지 전송
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
예 여러 수신자에게 메시지 전송
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
예 SQL 쿼리 결과를 첨부 파일로 전송
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
예 HTML 형식으로 메시지 전송
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
예 데이터베이스에서 특정 이벤트가 발생할 때 트리거를 사용하여 메시지 전송
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