在SQL Server中,不使用“SQL 邮件”的情况下发送邮件
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
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
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/exchanchor/htms/msexchsvr_cdo_top.asp
posted on 2005-04-26 16:32 Goodspeed 阅读(3053) 评论(8) 编辑 收藏 举报