首页 > 数据库 > SQL Server > 正文

sql server发送邮件

2024-08-31 00:51:54
字体:
来源:转载
供稿:网友

CREATE PROCEDURE usp_sendmail
(@server varchar(20), --服务器
 @From varchar(1000),
 @To varchar(1000),
 @Bcc varchar(500)='',
 @Subject nvarchar(400)='',
 @Body nvarchar(4000),
 @errnum int output
)
AS

Declare @object int
Declare @hr int

set @errnum=0 --正确的

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', @server

EXEC @hr = sp_OASetProperty @object, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/usemessageresponsetext").Value','1'
--EXEC @hr = sp_OASetProperty @object, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/LanguageCode").Value',' 2052'

--下面三条语句是smtp验证,如果服务器需要验证,则必须要这三句,你需要修改用户名和密码
EXEC @hr = sp_OASetProperty @object, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate").Value','0'
EXEC @hr = sp_OASetProperty @object, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendusername").Value',''
EXEC @hr = sp_OASetProperty @object, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendpassWord").Value',''

--EXEC @hr = sp_OASetProperty @object, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendusername").Value','lihonggen0'
--EXEC @hr = sp_OASetProperty @object, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendpassword").Value','xxx'

EXEC @hr = sp_OAMethod @object, 'Configuration.Fields.Update', null
EXEC @hr = sp_OASetProperty @object, 'To', @To
EXEC @hr = sp_OASetProperty @object, 'BodyPart.Charset','GB2312'
EXEC @hr = sp_OASetProperty @object, 'Bcc', @Bcc
EXEC @hr = sp_OASetProperty @object, 'From', @From
EXEC @hr = sp_OASetProperty @object, 'Subject', @Subject

EXEC @hr = sp_OASetProperty @object, 'TextBody',@Body
EXEC @hr = sp_OAMethod @object, 'Send', NULL

--判断出错
IF @hr <> 0
BEGIN
   EXEC sp_OAGetErrorInfo @object 
   set @errnum=@hr
   RETURN @object
END
--PRINT 'success'
EXEC @hr = sp_OADestroy @object

GO


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表