CREATE PROCEDURE sys_sendmail @To varchar(100) , @Bcc varchar(500), @Subject varchar(400)=" ", @Body varchar(4000) =" "
AS Declare @smtpserver varchar(50) --SMTP伺服器位址 Declare @smtpusername varchar(50) --SMTP伺服器使用者名稱 Declare @smtpuserpassword varchar(50) --SMTP伺服器密碼 set @smtpserver = 'smtp.163.com' set @smtpusername = 'yourname@163.com' --這裡設定成你的163信箱使用者名 set @smtpuserpassword = 'password' --這裡設定成你的163郵箱密碼 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', @smtpserver --下面三條語句是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',@smtpusername EXEC @hr = sp_OASetProperty @object, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendpassword").Value',@smtpuserpassword 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', @smtpusername 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 print 'failed' return @object END PRINT 'success' EXEC @hr = sp_OADestroy @object GO |