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 ='******@126.com' , -- 發件人郵件地址
@DISPLAY_NAME ='系統管理員', -- 發件人姓名
@REPLYTO_ADDRESS =NULL,
@DESCRIPTION = NULL,
@MAILSERVER_NAME = 'SMTP.126.COM', -- 郵件服務器地址
@MAILSERVER_TYPE = 'SMTP', -- 郵件協議
@PORT =25, -- 郵件服務器端口
@USERNAME = '******@126.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='******@qq.com', --收件人
@subject='Test title this is test ',
@body=N'z中文郵件內容 中文郵件內容'
go
我在本機設置成功后,又在服務器上設置了一遍,但服務器上無論如何都不能發送郵件,郵件發送狀態描述里顯示(我使用的SMTP服務器是smtp.gmail.com):
由於郵件服務器故障,無法將郵件發送給收件人。 (使用帳戶 1 (2010-05-24T09:57:05) 發送郵件。 異常郵件: 無法將郵件發送到郵件服務器。 (操作已超時。)。 )
或
由於郵件服務器故障,無法將郵件發送給收件人。 (使用帳戶 1 (2010-05-24T16:14:16) 發送郵件。 異常郵件: 無法連接到郵件服務器。 (由於目標機器積極拒絕,無法連接。 74.125.127.109:587)。 )
最后檢查才發現,原來是服務器上的McAfee阻止了SMTP端口外連:
2010-5-24 16:15:16 被端口阻擋規則阻擋 C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\Binn\DatabaseMail.exe 防病毒標准保護:禁止群發郵件蠕蟲發送郵件 74.125.127.109:587
由此,設置好McAfee后即可正常發送郵件了。