SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
ALTER PROCEDURE usp_SendMail @To varchar(100) ,@Subject varchar(400)=' ', @Body varchar(8000) =' '
AS
Declare @object int
Declare @hr int
EXEC @hr = sp_OACreate 'CDO.Message', @object OUT
EXEC @hr = sp_OASetProperty @object, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendusing").Value','2'
EXEC @hr = sp_OASetProperty @object, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpserver").Value', 'SMTP' --SMTP地址
--下面三條語句是smtp驗證,如果伺服器需要驗證,則必須要這三句,你需要修改使用者名稱和密碼
EXEC @hr = sp_OASetProperty @object, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate").Value','1'
EXEC @hr = sp_OASetProperty @object, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendusername").Value','your email address' --你的郵件地址
EXEC @hr = sp_OASetProperty @object, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendpassword").Value','yourpassword' --郵箱密碼
EXEC @hr = sp_OAMethod @object, 'Configuration.Fields.Update', null
EXEC @hr = sp_OASetProperty @object, 'To', @To
EXEC @hr = sp_OASetProperty @object, 'Bcc', 'goodspeedwang@yahoo.com.cn'
EXEC @hr = sp_OASetProperty @object, 'From', 'Goodspeed <goodspeed@idg-rbi.com.cn>'
EXEC @hr = sp_OASetProperty @object, 'Subject', @Subject
EXEC @hr = sp_OASetProperty @object, 'BodyFormat', 'MailFormat.Text'
EXEC @hr = sp_OASetProperty @object, 'TextBody', @Body -- Text格式
SET @Body = REPLACE(@Body,CHAR(13),'<br />')
EXEC @hr = sp_OASetProperty @object, 'HtmlBody',@Body --HTML格式的郵件
EXEC @hr = sp_OAMethod @object, 'Send', NULL
--判斷出錯
IF @hr <> 0
BEGIN
EXEC sp_OAGetErrorInfo @object
RETURN @object
END
PRINT 'success'
EXEC @hr = sp_OADestroy @object
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
更多cdo的資訊就訪問
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/exchanchor/htms/msexchsvr_cdo_top.asp