如何用sql server来自动发送邮件
前提: sql server所在的主机必须能上网……
如果有什么意外发不出去, 请先检查用 foxmail 客户端软件能不能发出去。
1. 准备一个允许 ‘smtp’ 邮件协议的邮箱, 163或者qq邮箱等都可以。
下面以qq为例。
2. 在 sql server 中设置:
[sql] exec sp_configure 'show advanced options',1 reconfigure with override go exec sp_configure 'database mail xps',1 reconfigure with override go
–2.创建邮件帐户信息
exec msdb..sysmail_add_account_sp
@account_name = 'etlerrormaillog', -- 邮件帐户名称
@email_address = '223@qq.com', -- 发件人邮件地址
@display_name = '系统管理员', -- 发件人姓名
@replyto_address = null,
@description = null,
@mailserver_name = 'smtp.qq.com', -- 邮件服务器地址
@mailserver_type = 'smtp', -- 邮件协议
@port = 25, -- 邮件服务器端口
@username = '223@qq.com', -- 用户名
@password = '??????', -- 密码
@use_default_credentials = 0,
@enable_ssl = 0,
@account_id = null
go
–3.配置文件
if exists(
select name
from msdb..sysmail_profile
where name = n'etlerrorprofilelog'
)
begin
exec msdb..sysmail_delete_profile_sp @profile_name = 'etlerrorprofilelog'
end
exec msdb..sysmail_add_profile_sp
@profile_name = 'etlerrorprofilelog', -- profile 名称
@description = '数据库邮件配置文件', -- profile 描述
@profile_id = null
go
–4.用户和邮件配置文件相关联
exec msdb..sysmail_add_profileaccount_sp
@profile_name = 'etlerrorprofilelog', -- profile 名称
@account_name = 'etlerrormaillog', -- account 名称
@sequence_number = 1 -- account 在 profile 中顺序
--5.发送文本测试邮件
exec msdb..sp_send_dbmail
@profile_name = 'etlerrorprofilelog',
@recipients = '223@qq.com', --收件人
@subject = 'test title this is test ',
@body = n'z中文邮件内容 中文邮件内容'
go