Archive for 28th August 2008

MSSQL 2005/2008 – sp_send_dbmail 을 사용하기 위한 기본 설정 간단가이드

MSSQL 2005에서 지원되는 dbmail을 사용하기 위한 방법 을 그냥 나열

일단 준비단계
——————————————————
EXECUTE msdb.dbo.sysmail_add_account_sp
@account_name = ‘계정이름 – hugyou’,
@description = ‘계정설명 – 전지전능하지 못한 hugyou’,
@email_address = ‘메일주소 – [email protected]’,
@display_name = ‘보여질 이름 – 메일 받을때 발신인에 찍힐 이름’,
@username=’메일주소 – [email protected] or other’,
@password=’비밀번호 – 비번’,
@mailserver_name = ‘SMTP 서버 주소 – smtp.smtp.com’
go

EXECUTE msdb.dbo.sysmail_add_profile_sp
@profile_name = ‘계정이름’,
@description = ‘Profile used for 계정이름’
go

EXECUTE msdb.dbo.sysmail_add_profileaccount_sp
@profile_name = ‘계정이름’,
@account_name = ‘계정이름’,
@sequence_number = 1
go

EXECUTE msdb.dbo.sysmail_add_principalprofile_sp
@profile_name = ‘계정이름’,
@principal_name = ‘public’,
@is_default = 1 ;
go

——————————————————

이제 보내봅시다
——————————————————
declare @body1 varchar(100)
set @body1 = ‘서버 :’+@@servername+ ‘ 첫 메일!’
EXEC msdb.dbo.sp_send_dbmail @recipients=’보낼메일 주소 [email protected] 등등..’,
@profile_name = ‘계정이름’,
@subject = ‘메일 테스트’,
@body = @body1,
@body_format = ‘HTML’ ;
——————————————————

다른 옵션은 books online의 sp_send_dbmail 을 찾아보시면 쿼리 결과를 그대로 보내는 법 등이 나와있음 입니다 🙂

추가 만약 SQL 2008 이라면 Database Mail XPs 를 활성화 시키는데 에로사항이 꽃 필 수 있기에..
아래 처럼 그냥 쿼리 실행으로 간단하게!
——————————————————
USE master
GO
sp_configure ‘remote admin connection’, 1
GO
sp_configure ‘show advanced options’, 1
GO
reconfigure with override
GO
sp_configure ‘Database Mail XPs’, 1
GO
reconfigure with override
GO
sp_configure ‘show advanced options’, 0
GO
sp_configure ‘remote admin connection’, 1
GO

——————————————————

MSSQL – 회원들의 email provider를 뽑고 싶을때

다음에 또 치기 싫으니-_-;;
올려놔야지.. ㅋ

select    substring(email, (charindex(‘@’, email)+1), (len(email)-charindex(‘@’, email)+1)) as ‘mail providers’,
    count([id]) as ‘회원수’
from    [회원DB] with (NOLOCK)
group    by substring(email, (charindex(‘@’, email)+1), (len(email)-charindex(‘@’, email)+1))

이걸로 큰 귀찮음을 덜 수 있다면 다행!