--1. 启用 SQL Server 2005 邮件功能。
use master
go
exec sp_configure 'show advanced options',1
go
reconfigure
go
exec sp_configure 'Database mail XPs',1
go
reconfigure
go
--2. 在 SQL Server 2005 中添加邮件帐户(account)
exec msdb..sysmail_add_account_sp
@account_name = 'DBmail' -- 邮件帐户名称(SQL Server 使用)
,@email_address = 'DBmail@mail.com' -- 发件人邮件地址
,@display_name = null
,@replyto_address = null
,@description = null
,@mailserver_name = 'smtp server' -- 邮件服务器地址
,@mailserver_type = 'SMTP' -- 邮件协议(SQL 2005 只支持 SMTP)
,@port = 25 -- 邮件服务器端口
,@username = ''
,@password = ''
,@use_default_credentials = 0
,@enable_ssl = 0
,@account_id = null
--3. 在 SQL Server 2005 中添加 profile
exec msdb..sysmail_add_profile_sp @profile_name = 'db_profile' -- profile 名称
,@description = 'db mail profile' -- profile 描述
,@profile_id = null
-- 在 SQL Server 2005 中映射 account 和 profile
exec msdb..sysmail_add_profileaccount_sp @profile_name = 'db_profile' -- profile 名称
,@account_name = 'DBmail' -- account 名称
,@sequence_number = 1 -- account 在 profile 中顺序
--5. 利用 SQL Server 2005 Database Mail 功能发送邮件。
exec msdb..sp_send_dbmail @profile_name = 'db_profile' -- profile 名称
,@recipients = 'test@mail.com' -- 收件人邮箱
,@subject = 'SQL Server 2005 Mail 测试' -- 邮件标题
,@body = 'Hello Mail!测试' -- 邮件内容
,@body_format = 'TEXT' -- 邮件格式