SQL Server 使用觸發(fā)器(trigger)發(fā)送電子郵件步驟詳解
sql 使用系統(tǒng)存儲(chǔ)過程 sp_send_dbmail 發(fā)送電子郵件語法:
sp_send_dbmail [ [ @profile_name = ] 'profile_name' ] [ , [ @recipients = ] 'recipients [ ; ...n ]' ] [ , [ @copy_recipients = ] 'copy_recipient [ ; ...n ]' ] [ , [ @blind_copy_recipients = ] 'blind_copy_recipient [ ; ...n ]' ] [ , [ @subject = ] 'subject' ] [ , [ @body = ] 'body' ] [ , [ @body_format = ] 'body_format' ] [ , [ @importance = ] 'importance' ] [ , [ @sensitivity = ] 'sensitivity' ] [ , [ @file_attachments = ] 'attachment [ ; ...n ]' ] [ , [ @query = ] 'query' ] [ , [ @execute_query_database = ] 'execute_query_database' ] [ , [ @attach_query_result_as_file = ] attach_query_result_as_file ] [ , [ @query_attachment_filename = ] query_attachment_filename ] [ , [ @query_result_header = ] query_result_header ] [ , [ @query_result_width = ] query_result_width ] [ , [ @query_result_separator = ] 'query_result_separator' ] [ , [ @exclude_query_output = ] exclude_query_output ] [ , [ @append_query_error = ] append_query_error ] [ , [ @query_no_truncate = ] query_no_truncate ] [ , [ @mailitem_id = ] mailitem_id ] [ OUTPUT ]
下面開始配置 sql 發(fā)送電子郵件:
步驟一:
-- 啟用 sql server 郵件的功能 exec sp_configure 'show advanced options',1 go reconfigure; go exec sp_configure 'Database Mail XPs',1 go reconfigure; go
如果上面的語句執(zhí)行失敗,也可以使用下面的語句。
-- 啟用 sql server 郵件的功能 exec sp_configure 'show advanced options', 1 go reconfigure with override go exec sp_configure 'Database Mail XPs', 1 go reconfigure with override go
使用下面的語句查看數(shù)據(jù)庫郵件功能是否開啟成功和數(shù)據(jù)庫配置信息:
-- 查詢數(shù)據(jù)庫的配置信息 select * from sys.configurations -- 查看數(shù)據(jù)庫郵件功能是否開啟,value 值為1表示已開啟,0為未開啟 select name,value,description, is_dynamic,is_advanced from sys.configurations where name like '%mail%'
步驟二:
if exists(SELECT * FROM msdb..sysmail_account WHERE NAME='test') --判斷郵件賬戶名為 test 的賬戶是否存在 begin EXEC msdb..sysmail_delete_account_sp @account_name='test' -- 刪除郵件賬戶名為 test 的賬戶 end exec msdb..sysmail_add_account_sp --創(chuàng)建郵件賬戶 @account_name = 'test' -- 郵件帳戶名稱 ,@email_address = '980095349@qq.com' -- 發(fā)件人郵件地址 ,@display_name = 'Brambling' -- 發(fā)件人姓名 ,@replyto_address = null -- 回復(fù)地址 ,@description = null -- 郵件賬戶描述 ,@mailserver_name = 'smtp.qq.com' -- 郵件服務(wù)器地址 ,@mailserver_type = 'SMTP' -- 郵件協(xié)議 ,@port = 25 -- 郵件服務(wù)器端口 ,@username = '980095349@qq.com' -- 用戶名 ,@password = 'xxxxxx' -- 密碼 ,@use_default_credentials = 0 -- 是否使用默認(rèn)憑證,0為否,1為是 ,@enable_ssl = 1 -- 是否啟用 ssl 加密,0為否,1為是 ,@account_id = null -- 輸出參數(shù),返回創(chuàng)建的郵件賬戶的ID
PS:如果使用的是QQ郵箱,記得要把參數(shù) @enable_ssl 的值設(shè)置為 1 。不然后面會(huì)報(bào)服務(wù)器錯(cuò)誤,這個(gè)錯(cuò)誤搞了我好久,最后終于找到原因了。
步驟三:
if exists(SELECT * FROM msdb..sysmail_profile where NAME = N'SendEmailProfile') --判斷名為 SendEmailProfile 的郵件配置文件是否存在 begin exec msdb..sysmail_delete_profile_sp @profile_name = 'SendEmailProfile' --刪除名為 SendEmailProfile 的郵件配置文件 end exec msdb..sysmail_add_profile_sp -- 添加郵件配置文件 @profile_name = 'SendEmailProfile', -- 配置文件名稱 @description = '數(shù)據(jù)庫發(fā)送郵件配置文件', -- 配置文件描述 @profile_id = NULL -- 輸出參數(shù),返回創(chuàng)建的郵件配置文件的ID
步驟四:
-- 郵件賬戶和郵件配置文件相關(guān)聯(lián) exec msdb..sysmail_add_profileaccount_sp @profile_name = 'SendEmailProfile', -- 郵件配置文件名稱 @account_name = 'test', -- 郵件賬戶名稱 @sequence_number = 1 -- account 在 profile 中的順序,一個(gè)配置文件可以有多個(gè)不同的郵件賬戶
好了,到這里 sql 發(fā)送郵件的配置就基本結(jié)束了。下面創(chuàng)建一個(gè)觸發(fā)器實(shí)現(xiàn)用戶注冊成功后,發(fā)送郵件給用戶。
首先創(chuàng)建一個(gè)表:
-- 創(chuàng)建一個(gè)表 create table T_User ( UserID int not null identity(1,1) primary key, UserNo nvarchar(64) not null unique, UserPwd nvarchar(128) not null , UserMail nvarchar(128) null ) go
然后創(chuàng)建一個(gè) insert 類型的 after 觸發(fā)器:
create trigger NewUser_Send_Mail on T_User after insert as declare @UserNo nvarchar(64) declare @title nvarchar(64) declare @content nvarchar(320) declare @mailUrl nvarchar(128) declare @count int select @count=COUNT() from inserted select @UserNo=UserNo,@mailUrl=UserMail from inserted if(@count>0) begin set @title='注冊成功通知' set @content='歡迎您'+@UserNo+'!您已成功注冊!通知郵件,請勿回復(fù)!' exec msdb.dbo.sp_send_dbmail @profile_name='SendEmailProfile', -- 郵件配置文件名稱 @recipients=@mailUrl, -- 郵件發(fā)送地址 @subject=@title, -- 郵件標(biāo)題 @body=@content, --郵件內(nèi)容 @body_format='text' -- 郵件內(nèi)容的類型,text 為文本,還可以設(shè)置為 html end go
下面就來測試一下吧:
-- 新添加一條數(shù)據(jù),用以觸發(fā) insert 觸發(fā)器 insert into T_User(UserNo,UserPwd,UserMail) values('demo1','123456','1171588826@qq.com')
執(zhí)行上面的語句之后,大概兩三秒鐘,就會(huì)收到郵件了(如果沒有出現(xiàn)錯(cuò)誤的話)。如果沒有收到郵件可以使用下面的語句查看郵件發(fā)送情況。
use msdb go select * from sysmail_allitems -- 郵件發(fā)送情況,可以用來查看郵件是否發(fā)送成功 select * from sysmail_mailitems -- 發(fā)送郵件的記錄 select * from sysmail_event_log -- 數(shù)據(jù)庫郵件日志,可以用來查詢是否報(bào)錯(cuò)
use msdb go --為角色名為 dba 的角色賦予發(fā)送數(shù)據(jù)庫郵件的權(quán)限 create user dba for login dba go exec dbo.sp_addrolemember @rolename = 'DatabaseMailUserRole', @membername = 'dba' go use msdb go --為角色名為 dba 的角色賦予配置文件發(fā)送郵件的權(quán)限 exec sysmail_add_principalprofile_sp @principal_name = 'dba', -- 角色名稱 @profile_name = 'SendEmailProfile', -- 配置文件名稱 @is_default = 1 -- 對于角色所擁有的配置文件的順序,一個(gè)數(shù)據(jù)庫角色可以有多個(gè)配置文件的權(quán)限
如果所使用的登陸數(shù)據(jù)庫會(huì)話的角色沒有發(fā)送數(shù)據(jù)庫郵件的權(quán)限,那么也會(huì)報(bào)錯(cuò)。所以上面是賦予角色發(fā)送數(shù)據(jù)庫郵件的權(quán)限 sql 語句。
以上所述是小編給大家介紹的SQL Server 使用觸發(fā)器(trigger)發(fā)送電子郵件,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
MSSQL 檢查所使用的語句是否符合標(biāo)準(zhǔn)
標(biāo)準(zhǔn)SQL和T-SQL之間有很多區(qū)別——太多了,這里就不說了。還有,如果你在SQL Server上工作, 那么使用這些私有的擴(kuò)展是有好處的。2009-11-11sqlserver CONVERT()函數(shù)用法小結(jié)
文章分析總結(jié)了關(guān)于CONVERT()函數(shù)在操作日期時(shí)的一些常見的用法分析下面來看看2012-09-09SQL Server創(chuàng)建用戶只能訪問指定數(shù)據(jù)庫和視圖的操作步驟
本文介紹了如何在SQLServer中創(chuàng)建用戶并限定其訪問權(quán)限,包括創(chuàng)建用戶、設(shè)置用戶映射、設(shè)置只能訪問指定的數(shù)據(jù)表或視圖、給指定表或視圖賦予具體權(quán)限和檢查權(quán)限等步驟,這種設(shè)置可以防止用戶訪問到過多不必要的過程表和過程視圖,提高數(shù)據(jù)安全性2024-10-10SQL Server 中查看SQL句子執(zhí)行所用的時(shí)間
在MSSQL Server中通過查看SQL語句執(zhí)行所用的時(shí)間,來衡量SQL語句的性能。2009-07-07