Azure SQL에서 이메일 보내기

 

·         Version : Azure SQL

 

Azure SQL Database Managed Instance Azure 클라우드에서 대부분의 SQL Server 기능을 사용할  있다이번 포스트에서는 Managed Instance에서 메일 프로필을 설정하고 이메일을 보내는 방법에 대해서 알아본다.

 

먼저 이메일 서버에 액세스하는데 필요한 로그인/암호 정보와 함께 실제 이메일을 보낼 전자 메일 서버의 주소가 포함된 이메일 계정 정보를 설정한다아래 스크립트를 참고하여 설정을 사용자에게 맞게 변경  실행 한다.

-- Create a Database Mail account

EXECUTE msdb.dbo.sysmail_add_account_sp

    @account_name = '...',

    @description = '...',

    @email_address = '$(loginEmail)',

    @display_name = '...',

    @mailserver_name = '$(mailserver)' ,

    @username = '$(loginEmail)' , 

    @password = '$(password)'

 

-- Create a Database Mail profile

EXECUTE msdb.dbo.sysmail_add_profile_sp

    @profile_name = 'AzureManagedInstance_dbmail_profile',

    @description = '...' ;

 

-- Add the account to the profile

EXECUTE msdb.dbo.sysmail_add_profileaccount_sp

    @profile_name = 'AzureManagedInstance_dbmail_profile',

    @account_name = '...',

    @sequence_number = 1;

 

여기서 중요한것은 하나의 이메일 프로필만 만들  있으며 프로필명은 반드시 AzureManagedInstance_dbmail_Profile 이라고 해야 한다이렇게 하지 않으면 Managed Instance 프로필을 사용하여 이메일을 보낼  없다.

 

이메일 서버 설정이 완료되었으면 sp_configure에서 Database Mail XPs 옵션을 활성화 한다.

EXEC sp_configure 'show advanced options', 1; 

GO 

RECONFIGURE; 

GO 

EXEC sp_configure 'Database Mail XPs', 1; 

GO 

RECONFIGURE 

GO

 

이제 sp_send_dbmail  sp_notify_operator 프로시저를 사용하여 전자 메일을 보내 구성을 테스트할  있다전자 메일을 보내는 방법은 아래 스크립트를 참고 한다.

DECLARE @body VARCHAR(4000) = 'The email is sent with msdb.dbo.sp_send_dbmail from ' +@@SERVERNAME;

EXEC msdb.dbo.sp_send_dbmail

        @profile_name = 'AzureManagedInstance_dbmail_profile',

        @recipients = '$(email)',

        @body = @body,

        @subject = 'Azure SQL Instance - test email' ;

 

운영자에게 이메일을 보내기 위해서는 운영자 정보를 추가해야 한다아래 스크립트를 참고하여 운영자 이메일을 설정하여 그룹을 생성한다.

EXEC msdb.dbo.sp_add_operator

          @name = N'SQL DevOp',

          @enabled = 1,

          @email_address = N'$(email)',

          @weekday_pager_start_time = 080000,

          @weekday_pager_end_time = 170000,

          @pager_days = 62 ;

 

운영자 등록이 완료되었으며 아래 스크립트를 참고하여 설정이 정상적으로 되었는지 운영자에게 이메일 알림을 전송해본다.

DECLARE @body VARCHAR(4000) = 'The email is sent using sp_notify_operator from ' +@@SERVERNAME;

EXEC msdb.dbo.sp_notify_operator

              @profile_name = N'AzureManagedInstance_dbmail_profile',

              @name = N'SQL DevOp',

              @subject = N'Azure SQL Instance - Test Notification',

              @body = @body;

 

아래 예시는 Managed Instance에서 작업이 성공하거나 실패할  이메을 통해 운영자에게 알릴  있다.

EXEC msdb.dbo.sp_update_job

              @job_name=N'My job name',

              @notify_level_email=2,

              @notify_level_page=2,

              @notify_email_operator_name=N'SQL DevOp'

 

 

[참고자료]

https://blogs.msdn.microsoft.com/sqlserverstorageengine/2018/08/31/sending-emails-in-azure-sql-managed-instance/

 

 

2018-09-04 / Sungwook Kang / http://sqlmvp.kr

 

Azure SQL,  Managed Instance, SQL, database mail, XPs Mail, sp_send_dbmail, sp_notify_operator



출처: https://sqlmvp.tistory.com/1282?category=663684 [Database Lab]




profile

강성욱 / jevida@naver.com
Microsoft SQL Server MVP
Blog : http://sqlmvp.kr
Facebook : http://facebook.com/sqlmvp

Kakao Talk : SQLMVP

Line : jevida


현재 LA에 거주하고 있으며 SQL에 관심있는 분이면 언제든 친추 환영합니다.