引自:
其实,我们可以在 SQL Server中创建 OLE 对象实例,调用IIS SMTP自带的发送组件来实现邮件发送。
我们建立这个存储过程,你需要修改的地方是,SmtpServer的名字
Create PROCEDURE sys_sendmail @From varchar(100) , @To varchar(100) ,
@Bcc varchar(500), @Subject varchar(400)=" ", @Body ntext =" "
AS
Declare @object int
Declare @hr int
EXEC @hr = sp_OACreate 'CDO.Message', @object OUT
EXEC @hr = sp_OASetProperty @object,
'Configuration.fields("").Value','2'
EXEC @hr = sp_OASetProperty @object,
'Configuration.fields("").Value',
'smtp.163.com'
--下面三条语句是smtp验证,如果服务器需要验证,则必须要这三句,你需要修改用户名和密码
EXEC @hr = sp_OASetProperty @object,
'Configuration.fields("").Value','1'
bitsCN.com中国网管联盟
EXEC @hr =
sp_OASetProperty @object,
'Configuration.fields("").Value','lihonggen0'
EXEC @hr = sp_OASetProperty @object,
'Configuration.fields("").Value','xxx'
EXEC @hr = sp_OAMethod @object, 'Configuration.Fields.Update', null
EXEC @hr = sp_OASetProperty @object, 'To', @To
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
RETURN @object
END
PRINT 'success'
EXEC @hr = sp_OADestroy @object
GO
注意:必须确保安装Smtp,可以访问CDO对象
--------------------next---------------------