在Sql Server 中調用Jmail組件發送郵件

 Sql Server 中調用Jmail組件發送郵件

預備知識

    1OLE自動化函數

    OLE自動化使應用程序能夠對另一個應用程序中實現的對象進行操作,或者將對象公開以便可以對其進行操作。自動化客戶端是可對屬於另一個應用程序的公開對象進行操作的應用程序,本文值得是Sql Server。公開對象的應用程序稱爲自動化服務器,又成爲自動化組件,本文中即Jmail組件咯。客戶端通過訪問應用程序對象的屬性和函數對這些對象進行操作。

Sql Server使用Ole組件的途徑是幾個系統擴展存儲過程sp_OACreatesp_OADestroysp_OAGetErrorInfosp_OAMethodsp_OASetPropertysp_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目錄下,運氣時雙擊即可。

 

 

3Sql 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

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章