在Sql Server 中调用Jmail组件发送邮件

预备知识

1.OLE自动化函数

OLE自动化使应用程序能够对另一个应用程序中实现的对象进行操作,或者将对象公开以便可以对其进行操作。自动化客户端是可对属于另一个应用程序的公开对象进行操作的应用程序,本文值得是Sql Server。公开对象的应用程序称为自动化服务器,又成为自动化组件,本文中即Jmail组件咯。客户端通过访问应用程序对象的属性和函数对这些对象进行操作。

在Sql Server使用Ole组件的途径是几个系统扩展存储过程sp_OACreate、sp_OADestroy、sp_OAGetErrorInfo、sp_OAMethod、sp_OASetProperty和sp_OAGetProperty,再次简单地介绍一下使用方法,详细资料参考Sql Server联机丛书。

OLE自动化对象的使用方法:

(1)调用sp_OACreate 创建对象。

格式:sp_OACreate clsid,objecttoken OUTPUT [ , context ]

参数:clsid——是要创建的OLE 对象的程序标识符(ProgID)。此字符串描述该OLE 对象的类,其形式,如'OLEComponent.Object',OLEComponent 是OLE 自动化服务器的组件名称,Object 是OLE 对象名,本文中使用的“JMail.Message”;

Objecttoken——是返回的对象标志,并且必须是数据类型为int 的局部变量。用于标识所创建的OLE 对象,并将在调用其它OLE 自动化存储过程时使用。本文中就是通过它来调用JMail.Message组件的属性和方法的。

Context——指定新创建的OLE 对象要在其中运行的执行上下文。本文不使用该参数,故不赘述。以下与此一致,所有方法属性的其他用法请参阅Sql Server联机文档。

(2)使用该对象。

(a)调用sp_OAGetProperty 获取属性值。

格式:_OAGetProperty objecttoken,propertyname [, propertyvalue OUTPUT]

参数:(前面出现过的参数,以下均省略。)

Propertyname——对象的属性名称;

Propertyvalue——返回的对象的属性值,该参数带OUTPUT属性,执行该操作后,你就可以从propertyvalue中得到属性的值了。

(b)调用sp_OASetProperty 将属性设为新值。

格式:sp_OASetProperty objecttoken, propertyname, propertyvalue

(c)调用sp_OAMethod 以调用某个方法。

格式:sp_OAMethod objecttoken, methodname [, returnvalue OUTPUT] [ , [ parametername = ] parametervalue [...n]]

参数:Returnvalue——调用方法的返回值,如果没有返回值,此参数设置为NULL;

Parametername——方法定义中的参数名称,也就是形参;

Parametervalue——参数值;

……n——表示,可以带很多参数,个数由方法定义限制;

(d)调用sp_OAGetErrorInfo 获取最新的错误信息。

格式:sp_OAGetErrorInfo [objecttoken ] [, source OUTPUT] [, description OUTPUT]

参数:Source——错误源;

Description——错误描述;

()调用sp_OADestroy 释放对象。

格式:sp_OADestroy objecttoken

2.操作方法

(1)软件准备

请先到http://www.dimac.net/或者国内提供组件下载的网站下载最新版的JMail组件,如果你得到的是安装版,执行weJMailx.exe即可,系统的配置安装程序会自动完成。如果只有一个JMail.dll文件,请按照下面的步骤安装:

(a)新建文本文件,输入如下命令:

regsvr32 /S Jmail.dll

net start w3svc

(b)将文本文件保存为bat批处理文件,以供调用。

或做成vbs脚本也行:(注:regsvr 32 /S, 这里的S让程序静默处理,不显示对话框)

Set WshShell=CreateObject("Wscript.Shell")

WshShell.Run "regsvr32 /S JMail.dll",0

WshShell.Run "net start w3svc",0

(c)此文件连同Jmail.dll一起拷贝到Sql Server数据库服务器的System32目录下,运气时双击即可。

3.Sql Store Procedure的处理

Create Procedure sp_jmail_send

@sender varchar(100),

@sendername varchar(100)='',

@serveraddress varchar(255)='SMTP Server Address',

@MailServerUserName varchar(255)=null,

@MailServerPassword varchar(255)=null,

@recipient varchar(255),

@recipientBCC varchar(200)=null,

@recipientBCCName varchar(200)=null,

@recipientCC varchar(200)=null,

@recipientCCName varchar(100)=null,

@attachment varchar(100) =null,

@subject varchar(255),

@mailbody text

As

/*

该存储过程使用办公自动化脚本调用Dimac w3 JMail AxtiveX组件来代替Sql Mail发送邮件

该方法支持“服务器端身份验证”

*/

--声明w3 JMail使用的常规变量及错误信息变量

Declare @object int,@hr int,@rc int,@output varchar(400),@description varchar (400),@source varchar(400)

--创建JMail.Message对象

Exec @hr = sp_OACreate 'jmail.message', @object OUTPUT

--设置邮件编码

Exec @hr = sp_OASetProperty @object, 'Charset', 'gb2312'

--身份验证

If Not @MailServerUserName is null

Exec @hr = sp_OASetProperty @object, 'MailServerUserName',@MailServerUserName

If Not @MailServerPassword is null

Exec @hr = sp_OASetProperty @object, 'MailServerPassword',@MailServerPassword

--设置邮件基本参数

Exec @hr = sp_OASetProperty @object, 'From', @sender

Exec @hr = sp_OAMethod @object, 'AddRecipient', NULL , @recipient

Exec @hr = sp_OASetProperty @object, 'Subject', @subject

Exec @hr = sp_OASetProperty @object, 'Body', @mailbody

--设置其它参数

if not @attachment is null

exec @hr = sp_OAMethod @object, 'Addattachment', NULL , @attachment,'false'

print @attachment

If (Not @recipientBCC is null) And (Not @recipientBCCName is null)

Exec @hr = sp_OAMethod @object, 'AddRecipientBCC', NULL , @recipientBCC,@recipientBCCName

Else If Not @recipientBCC is null

Exec @hr = sp_OAMethod @object, 'AddRecipientBCC', NULL , @recipientBCC

If (Not @recipientCC is null) And (Not @recipientCCName is null)

Exec @hr = sp_OAMethod @object, 'AddRecipientCC', NULL , @recipientCC,@recipientCCName

Else If Not @recipientCC is null

Exec @hr = sp_OAMethod @object, 'AddRecipientCC', NULL , @recipientCC

If Not @sendername is null

Exec @hr = sp_OASetProperty @object, 'FromName', @sendername

--调用Send方法发送邮件

Exec @hr = sp_OAMethod @object, 'Send', null,@serveraddress

--捕获JMail.Message异常

Exec @hr = sp_OAGetErrorInfo @object, @source OUTPUT, @description OUTPUT

if (@hr = 0)

Begin

Set @output='错误源: '+@source

Print @output

Select @output = '错误描述: ' + @description

Print @output

End

Else

Begin

Print '获取错误信息失败!'

Return

End

---调用上面的Jmail Store Procedure 来发邮件---

Create Procedure SendMail

@Sender varChar(50)=null,

@strRecipients varChar(200),

@strSubject varChar(200),

@strMessage varChar(2000),

@sql varChar(50)=null

As

Declare @SplitStr varchar(1) --Split symbol

Declare @strTemp varchar(200) --Temp Multi-email address for split

Declare @email varchar(50) --email address

Declare @SenderAddress varChar(50)

Declare @Attach varChar(200)

Declare @DefaultSender varChar(50)

Declare @MailServer varChar(50)

Declare @User varChar(50)

Declare @Pass varChar(50)

Declare @SenderName varChar(50)

Declare @AttachDir varChar(100)

--Set Initial value

Set @DefaultSender='Default Sender'

Set @MailServer='Mail Server'

Set @User='SMTP User'

Set @Pass='SMTP Password'

Set @SenderName='Sender Name'

Set @AttachDir='E:/Data/Jmail_Attach/'+Replace(Replace(Replace(Convert(varChar(19),GetDate(),120),'-',''),' ',''),':','')+'.txt'

--Set Email Address Split semicolon ;

set @SplitStr=';'

Set @strTemp=@strRecipients+@SplitStr+'end'

Set @strTemp=Replace(@strTemp,',',';')

--Check sql Sentence

If (@Sql is Null) Or (len(@Sql)=0)

Set @AttachDir=Null

Else

Begin

Declare @CmdStr varChar(200)

Set @CmdStr='bcp "'+@Sql+'" queryout '+@AttachDir+' -c'

EXEC master..xp_cmdshell @CmdStr

End

while CharIndex(@SplitStr,@strTemp,1)<>0

Begin

Set @email=left(@strTemp,CharIndex(@SplitStr,@strTemp,1)-1)

Set @strTemp=right(@strTemp,len(@strTemp)-len(@email)-1)

If (@Sender Is Null) Or (Len(@Sender)=0)

Set @SenderAddress=@DefaultSender

Else

Set @SenderAddress=@Sender

Print @email

--Call sp_jmail_send Send Mail

EXEC sp_jmail_send @sender=@SenderAddress,@sendername=@SenderName,

@serveraddress=@MailServer,@MailServerUserName=@User,@MailServerPassword=@Pass,

@recipient=@email,@subject=@strSubject,@mailbody=@strMessage,@attachment=@AttachDir

End

事实上,我们用第一个sp_jmail_send 存储过程就够了,后面的一个SendMail 的存储过程是在前一个的基础上做的扩展。根据需求来改变,你可以做更多的扩展。

注意:如果在执行存储过程的时候发生如下错误:

Msg 15281, Level 16, State 1, Procedure sp_OACreate, Line 1

SQL Server blocked access to procedure 'sys.sp_OACreate' of component 'Ole Automation Procedures' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'Ole Automation Procedures' by using sp_configure. For more information about enabling 'Ole Automation Procedures', see "Surface Area Configuration" in SQL Server Books Online.

Msg 15281, Level 16, State 1, Procedure sp_OASetProperty, Line 1

SQL Server blocked access to procedure 'sys.sp_OASetProperty' of component 'Ole Automation Procedures' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'Ole Automation Procedures' by using sp_configure. For more information about enabling 'Ole Automation Procedures', see "Surface Area Configuration" in SQL Server Books Online.

Msg 15281, Level 16, State 1, Procedure sp_OASetProperty, Line 1

SQL Server blocked access to procedure 'sys.sp_OASetProperty' of component 'Ole Automation Procedures' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'Ole Automation Procedures' by using sp_configure. For more information about enabling 'Ole Automation Procedures', see "Surface Area Configuration" in SQL Server Books Online.

Msg 15281, Level 16, State 1, Procedure sp_OASetProperty, Line 1

SQL Server blocked access to procedure 'sys.sp_OASetProperty' of component 'Ole Automation Procedures' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'Ole Automation Procedures' by using sp_configure. For more information about enabling 'Ole Automation Procedures', see "Surface Area Configuration" in SQL Server Books Online.

我们需要修改:

Start -> programs ->MS sql 2005 -> configuration tools -> surfce area configuration -> Surface area conf for features ->

Ole automation -> select the Enable check box and saved

在Sql Server 中调用Jmail组件发送邮件相关推荐

  1. 在SQL Server中调用.NET程序集

    使用到这东西完全是个巧合和无奈之举.不小心在数据库中插入了一些HttpUtility.UrlEncodeUnicode之后的数据.数据库里的一些字段成了%uxxxx%uxxxx这样的结构. 搜索了半天 ...

  2. 为什么vs2005内置Web application server中调用COM组件时正常,在IIS中运行时组件创建失败?...

    如题. 在vs2005内置服务器中运行正常,发布到IIS中运行时出现组件创建失败的错误.错误页面如 下: 不知道有没有遇到过这种情况的兄弟?麻烦指点一二.谢谢!

  3. 使用sql server+jmail组件发送邮件

    使用sql server+jmail组件发送邮件 预备知识     1.OLE自动化函数     OLE自动化使应用程序能够对另一个应用程序中实现的对象进行操作,或者将对象公开以便可以对其进行操作.自 ...

  4. 十步优化SQL Server中的数据访问

    故事开篇:你和你的团队经过不懈努力,终于使网站成功上线,刚开始时,注册用户较少,网站性能表现不错,但随着注册用户的增多,访问速度开始变慢,一些用户开始发来邮件表示抗议,事情变得越来越糟,为了留住用户, ...

  5. SQL Server中的CLR编程——用.NET为SQL Server编写存储过程和函数

    很早就知道可以用.NET为SQL Server2005及以上版本编写存储过程.触发器和存储过程的,不过之前开发的系统要么因为历史原因用的是SQL2000要么根本用不着在SQL Server中启用CLR ...

  6. SQL Server中的执行计划

    介绍 (Introduction) In this article, I'm going to explain what the Execution Plans in SQL Server are a ...

  7. sql server的搜索_在SQL Server中进行全文本搜索

    sql server的搜索 介绍 (Introduction) In most cases, we will use clustered and non-clustered indexes to he ...

  8. 在SQL Server中的数据库之间复制表的六种不同方法

    In this article, you'll learn the key skills that you need to copy tables between SQL Server instanc ...

  9. 如何在SQL Server中处理过多的SOS_SCHEDULER_YIELD等待类型值

    The SQL Server SOS_SCHEDULER_YIELD is a fairly common wait type and it could indicate one of two thi ...

最新文章

  1. Google的面试题长啥样?看完被吊打!
  2. 深度学习巨头Yoshua Bengio清华演讲: 深度学习通往人类水平人工智能的挑战
  3. Linux 基础知识(十)DNS服务器主从复制,子域授权
  4. 汇编语言 ADC指令和SBB指令
  5. 《大话数据结构》第2章 算法基础 2.8 函数的渐近增长
  6. imagenet2012数据集
  7. linux centos7 配置ftp,Linux Centos7配置ftp服务器
  8. 计算机能不装显卡么,电脑不装显卡能玩CF吗
  9. 全志F1c100s主线linux入坑记录 (2)芯片超频
  10. pandas计算环比与同比
  11. android 颜色渐变扩散,Android 颜色渐变(gradient)的实现总结
  12. JAVAWEB NOTE 3
  13. delphi7及控件安装
  14. 阿里开发手册 学习 记忆 理解 表达 融会贯通
  15. 前端大串讲,狂神,狂神和飞哥
  16. 范围变更管控案例_项目范围管理案例之范围确认案例
  17. python小游戏 走迷宫小游戏设计与实现
  18. 前端项目的总结——为什么要组件化?
  19. java集合类深度解析
  20. IT培训班真的有用吗?IT培训包就业是真的吗?

热门文章

  1. 途牛android源码,途牛,Android 开发工程师,一面,攒人品
  2. Mapbox使用详解
  3. JAVA随机生成6位数,不足补0
  4. lambda表达式——Stream管道流的map操作
  5. matlab 车身阻尼比曲线,汽车阻尼比及振动响应的分析
  6. 跟我一起来学弹性云服务器ECS【华为云至简致远】
  7. R语言ggplot2可视化:使用patchwork包将两个ggplot2可视化结果横向构成新的结果可视化组合图(使用|符号)
  8. PHP中常用数学、日期、字符串函数
  9. 分布式数据库稳定性资料整理
  10. 大众点评的大数据实践转