sql server发送邮件
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', nullEXEC @hr = sp_OASetProperty @object, 'To', @ToEXEC @hr = sp_OASetProperty @object, 'BodyPart.Charset','GB2312'EXEC @hr = sp_OASetProperty @object, 'Bcc', @BccEXEC @hr = sp_OASetProperty @object, 'From', @FromEXEC @hr = sp_OASetProperty @object, 'Subject', @Subject
EXEC @hr = sp_OASetProperty @object, 'TextBody',@BodyEXEC @hr = sp_OAMethod @object, 'Send', NULL
--判断出错IF @hr <> 0BEGIN EXEC sp_OAGetErrorInfo @object; set @errnum=@hr RETURN @objectEND--PRINT 'success'EXEC @hr = sp_OADestroy @object
GO