博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
基于SQL调用Com组件来发送邮件
阅读量:5291 次
发布时间:2019-06-14

本文共 6089 字,大约阅读时间需要 20 分钟。

这个需求是公司有个文控中心,如果有用增删改了文件信息希望可以发邮件通知到有权限的人。当然方式很多。

这里是用数据库作业来完成

JOB+Com,这里用的com组件是Jmail

当然你需要把com组件放到system32下面

下面是实现方式

CREATE PROC dbo.dcc_Send_EmailASBEGIN	SET NOCOUNT ON 		DECLARE @lasttime DATETIME --获取最后一次发送的时间	IF Not EXISTS(SELECT lastsendtime FROM dcc_lastsend)		begin			set @lasttime=GETDATE()		END	ELSE		BEGIN			SELECT @lasttime=lastsendtime FROM dcc_lastsend		END	--SELECT @lasttime	--SELECT * FROM dcc_changereaon WHERE createdate>=@lasttime	DECLARE @sendto VARCHAR(500)--发送邮件给谁	DECLARE @aliasname VARCHAR(500)--档案名称	DECLARE @filename VARCHAR(500)--名称	DECLARE @userid INT --用户ID	DECLARE @dccid INT --文件夹ID	DECLARE @dccfileid INT --文件ID	DECLARE @action VARCHAR(500) --操作类型 1 新增 2 修改 3删除	DECLARE @changeid int	DECLARE @reasons VARCHAR(8000) --原因	DECLARE @html VARCHAR(8000) ---html格式内容	DECLARE @table VARCHAR(8000) ---html格式内容	DECLARE @senduiserid VARCHAR(8000) --通知的用户	DECLARE @sendfileid VARCHAR(8000) --更改的文件用户	DECLARE @temp TABLE (id INT ,ACTION varchar(50), aliasname varchar(50),FILENAME varchar(50))	DECLARE @rcount INT 		SET @html=''--	SET @table=''	DECLARE @index INT--奇数还是偶数行	SET @index=0    DECLARE CusCursor CURSOR FOR --外层游标查询用户    SELECT id,DccMail FROM view_dcc_users WHERE DccMail<>'' AND IsLock=0    OPEN CusCursor     FETCH NEXT FROM CusCursor INTO @userid,@sendto    WHILE (@@FETCH_STATUS = 0)         BEGIN  					SET @table=''			SET @index=0			SET @html = ''					SET @rcount=0						DELETE @temp			INSERT INTO @temp (id,action,aliasname,filename)				SELECT a.id,[action],a.aliasname,a.filename FROM dcc_changereaon a					WHERE a.createdate>=@lasttime					AND dccid IN (SELECT folderid FROM dbo.dcc_rights WHERE userid=@userid AND enabled=1)									SET @rcount = @@ROWCOUNT			IF @rcount>0				BEGIN 					DECLARE cur CURSOR FOR SELECT * FROM @temp --内层游标查询用户有那些权限					OPEN cur 					SET @html='各位:
兹通知下述文件有变动,贵部可随时登录文控网页(www.gardenchinagroup.com:82) 查阅:
' FETCH NEXT FROM cur INTO @changeid,@action,@aliasname,@filename WHILE (@@FETCH_STATUS = 0) BEGIN INSERT INTO dbo.dcc_logs ( changeid, userid, createtime ) VALUES ( @changeid, -- changeid - int @userid, -- userid - int GETDATE() -- createtime - smalldatetime ) IF @index %2<>0 SET @table=@table+'
' ELSE SET @table=@table+'
' SET @index=@index+1 fetch next from cur into @changeid,@action,@aliasname,@filename END CLOSE cur DEALLOCATE cur SET @html=@html+@table+'
档案名称 名称 备注(标注新增/更新/取消)
'+@filename+' '+@aliasname+' '+@action+'
'+@filename+' '+@aliasname+' '+@action+'
' --PRINT @sendto EXEC myCommData.dbo.my_SendMail '文件新增/更新/取消通知',@html,'DCC',@sendto,'','fengmin.dg@gardenchinagroup.com,foxbuilder.dg@gardenchinagroup.com','joey.dg@gardenchinagroup.com',1 END --SELECT @html FETCH NEXT FROM CusCursor INTO @userid,@sendto END CLOSE CusCursor DEALLOCATE CusCursor IF Not EXISTS(SELECT lastsendtime FROM dcc_lastsend) --更新最后一次发送时间 begin INSERT INTO dbo.dcc_lastsend( lastsendtime )VALUES(GETDATE()) END ELSE BEGIN UPDATE dcc_lastsend SET lastsendtime=GETDATE() END ENDGO
CREATE PROC dbo.my_SendMail@Subject varchar(100),	 -- 主题@Body varchar(8000),	 -- 内容@Sender varchar(100),	 -- 发件人姓名@ToList varchar(2000),	 -- 发送邮件地址, 可以多个地址,用","隔开@ccList varchar(2000)='',	-- 抄送地址	@BccList varchar(2000)='',	-- 秘抄送地址	@ReturnEmail VARCHAR(500)='', -- 回执到指定邮箱@ReturnReceipt bit = 0	 --是否回执AS/*EXEC dbo.my_SendMail	@Subject = 'today is a luck day', -- varchar(100)@Body = 'today is a luck day 20130', -- varchar(8000)@Sender = 'system', -- varchar(100)@ToList = 'fengmin.dg@gardenchinagroup.com', -- varchar(2000)@ccList = 'fengmin.dg@gardenchinagroup.com', -- varchar(2000)@BccList='foxbuilder.dg@gardenchinagroup.com',@ReturnEmail='foxbuilder.dg@gardenchinagroup.com',@ReturnReceipt=1*/Declare @object int Declare @hr intEXEC @hr = sp_OACreate 'jmail.Message', @object OUTEXEC @hr = sp_OASetProperty @object, 'Silent' , 1EXEC @hr = sp_OASetProperty @object, 'Logging' , 1EXEC @hr = sp_OASetProperty @object, 'Charset' , 'GB2312'EXEC @hr = sp_OASetProperty @object, 'From' , @SenderEXEC @hr = sp_OASetProperty @object, 'ISOEncodeHeaders' ,0EXEC @hr = sp_OASetProperty @object, 'From' , 'mis.dg@gardenchinagroup.com'EXEC @hr = sp_OASetProperty @object, 'MailServerUserName' , 'mis.dg'EXEC @hr = sp_OASetProperty @object, 'MailServerPassWord' , 'Edp2469851'EXEC @hr = sp_OASetProperty @object, 'Subject' , @SubjectEXEC @hr = sp_OASetProperty @object, 'HTMLBody' , @BodyIF @ReturnReceipt=1BEGINEXEC @hr = sp_OASetProperty @object, 'ReturnReceipt' ,1 --是否需要回执DECLARE @backemail VARCHAR(2000)SET @backemail='AddNativeHeader (Disposition-Notification-To,'+@ReturnEmail+')'EXEC @hr = sp_OAMethod @object, @backemail,NULLENDdeclare @i intset @i=1declare @mail varchar(100), @addmail varchar(100),@bccmail varchar(100)-- 添加发件人set @mail = ''set @mail =dbo.my_SplitStr(@ToList, ',', @i)while not @mail is null begin if @mail<>''begin set @addmail= 'AddRecipient ("' + @mail + '")'EXEC @hr = sp_OAMethod @object, @addmail, null	end set @i=@i+1set @mail =dbo.my_SplitStr(@ToList, ',', @i)end-- 添加抄送set @mail = ''set @i =1 set @mail =dbo.my_SplitStr(@ccList, ',', @i)while not @mail is null begin if @mail<>''begin set @addmail= 'AddRecipientCC ("' + @mail + '")'EXEC @hr = sp_OAMethod @object, @addmail, null	end set @i=@i+1set @mail =dbo.my_SplitStr(@ccList, ',', @i)end --秘密抄送set @mail = ''set @i =1 set @mail =dbo.my_SplitStr(@BccList, ',', @i)while not @mail is null begin if @mail<>''begin set @bccmail= 'AddRecipientBCC ("' + @mail + '")'EXEC @hr = sp_OAMethod @object, @bccmail, null	end set @i=@i+1set @mail =dbo.my_SplitStr(@BccList, ',', @i)end-- 开始发送EXEC @hr = sp_OAMethod @object, 'Send("192.168.0.3")', null--判断出错IF @hr <> 0BEGINEXEC sp_OAGetErrorInfo @object ENDPRINT 'success'EXEC @hr = sp_OADestroy @objectGO  

  

转载于:https://www.cnblogs.com/keepsilence/p/3784091.html

你可能感兴趣的文章
Scripting Java #3:Groovy与invokedynamic
查看>>
2014-04-21-阿里巴巴暑期实习-后台研发-二面经验
查看>>
数据结构中线性表的基本操作-合并两个线性表-依照元素升序排列
查看>>
使用pager进行分页
查看>>
吐医疗器械研发可配置性需求的槽点
查看>>
UVA - 1592 Database
查看>>
机器翻译评价指标 — BLEU算法
查看>>
机器学习基石(9)--Linear Regression
查看>>
Min Stack
查看>>
从LazyPhp说起
查看>>
Fine Uploader文件上传组件
查看>>
Spring Boot与Spring的区别
查看>>
查看linux 之mysql 是否安装的几种方法
查看>>
javascript中的传递参数
查看>>
objective-c overview(二)
查看>>
python查询mangodb
查看>>
软件测试(基础理论一)摘
查看>>
CF327E Axis Walking
查看>>
关于eclipse maven引入第三方jar包后如何下载源代码
查看>>
no-sql数据库之redis
查看>>