반응형

/**********************************************************************************************
-- Title : [2k5] 데이터베이스 메일 보내기(sp_send_dbmail)
-- Reference : dBRang.com, sqlworld.pe.kr
-- Key word : sysmail_add_account_sp sysmail_add_profile_sp sysmail_add_profileaccount_sp
--                 sysmail_add_principalprofile_sp sp_send_dbmail
**********************************************************************************************/
/*
-- 1. 환경 설정
*/
use master
go
sp_configure 'show advanced options', 1
go

sp_configure 'database mail xps',1
reconfigure
go
 
/*
-- 2. 노출영역 설저
*/
-- 구성도구 -> SQL server 노출 영역 구성 -> 기능에 대한 노출 영역 구성
-- -> Database Engine -> 데이터베이스 메일 -> 데이터베이스 메일 저장 프로시저 사용 체크
-- SQL server 에이전트 재실행
 
/*
-- 3. 계정 및 프로필 세팅
*/
-- smtp 계정에 대한 정보를 저장할 새 데이터베이스 메일 계정 생성
execute msdb.dbo.sysmail_add_account_sp
  @account_name = 'ttt_account_name'
, @description = 'ttt_description'
, @email_address = 'ttt@ttt.co.kr'  -- 보내는 사람 이메일
, @display_name = '디비랑' -- 보내는 사람 이름
, @username='ttt@ttt.co.kr'
, @password='p@ssw0rd'
, @mailserver_name = 'mail.ttt.co.kr'; -- 메일 서버 주소
go

-- 새 데이터베이스 메일 프로필 생성
execute msdb.dbo.sysmail_add_profile_sp
  @profile_name = 'ttt_profile_name'
, @description = 'ttt_description';
go

-- 데이터베이스 메일 프로필에 데이터베이스 메일 계정 추가
execute msdb.dbo.sysmail_add_profileaccount_sp
  @profile_name = 'ttt_profile_name'
, @account_name = 'ttt_account_name'
, @sequence_number = 1;
go

-- 데이터베이스 사용자 또는 역할에 데이터베이스 메일 프로필 사용 권한 부여
execute msdb.dbo.sysmail_add_principalprofile_sp
  @profile_name = 'ttt_profile_name'
, @principal_name = 'public'
, @is_default = 1 ;
go
 
/*
-- 4. 메일보내기
*/
declare @body_str varchar(2000);

set @body_str =  '메일이 ' + @@servername + '로 부터 발송되었습니다.'
set @body_str = @body_str + ''
set @body_str = @body_str + '발송 시간 : ' + cast(getdate() as varchar(20)) + ''
set @body_str = @body_str + '발송 머신 : ' + host_name() + ''
set @body_str = @body_str + '발송 유저 : ' + suser_sname()
set @body_str = @body_str + ''
set @body_str = @body_str + '감사합니다.'

exec msdb.dbo.sp_send_dbmail
  @recipients='ttt@naver.com' -- 받는 사람 이메일
, @subject = '데이터베이스 메일 보내기 테스트'
, @body = @body_str
, @body_format = 'html' ;
go

exec msdb.dbo.sp_send_dbmail
  @profile_name = 'ttt_profile_name'
, @recipients = 'ttt@naver.com'
, @query = 'select count(*) from adventureworks.production.workorder
            where duedate > ''2004-04-30''
            and  datediff(dd, ''2004-04-30'', duedate) < 2'
, @subject = 'dbmail 테스트 by 쿼리'
, @body = '쿼리 결과를 텍스트 파일로 첨부하여 발송하는 테스트'
, @attach_query_result_as_file = 1; --쿼리 결과가 텍스트 파일로 첨부되어 발송됨.
go
 
/*
-- 5. 지우기
*/
execute msdb.dbo.sysmail_delete_principalprofile_sp
  @principal_name = 'public'
, @profile_name = 'ttt_profile_name';
go

execute msdb.dbo.sysmail_delete_profileaccount_sp
  @profile_name = 'ttt_profile_name'
, @account_name = 'ttt_account_name';
go

execute msdb.dbo.sysmail_delete_profile_sp
  @profile_name = 'ttt_profile_name';
go

execute msdb.dbo.sysmail_delete_account_sp
  @account_name = 'ttt_account_name';
go

반응형

+ Recent posts