Отправка почты с использованием SQL Server 2000 - PullRequest
0 голосов
/ 26 октября 2009

Дубликат: Автоматическая SMTP (не MAPI) электронная почта с использованием планировщика заданий SQL Server


Я хочу отправлять почту с помощью хранимой процедуры с использованием SQL Server 2000 с использованием компонента Windows SMTP. Я не хочу, чтобы сторонний компонент. Имя моего SMTP-сервера = 'mail.met.net'. Как отправлять почту с помощью SQL Server 2000?

CREATE PROCEDURE [dbo].[sp_send_mail]
  @from varchar(500) ,
  @password varchar(500) ,
  @to varchar(500) ,  
  @subject varchar(500),
  @body varchar(4000) ,
  @bodytype varchar(10),
  @output_mesg varchar(10) output,
  @output_desc varchar(1000) output
AS
DECLARE @imsg int
DECLARE @hr int
DECLARE @source varchar(255)
DECLARE @description varchar(500)

EXEC @hr = sp_oacreate 'cdo.message', @imsg out

--SendUsing Specifies Whether to send using port (2) or using pickup directory (1)
EXEC @hr = sp_oasetproperty @imsg,
'configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendusing").value','2'

--SMTP Server
EXEC @hr = sp_oasetproperty @imsg, 
  'configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpserver").value', 
  'mail.met.net' 

--UserName
EXEC @hr = sp_oasetproperty @imsg, 
  'configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendusername").value', 
  @from 

--Password
EXEC @hr = sp_oasetproperty @imsg, 
  'configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendpassword").value', 
  @password 

--UseSSL
EXEC @hr = sp_oasetproperty @imsg, 
  'configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl").value', 
  'True' 

--PORT 
EXEC @hr = sp_oasetproperty @imsg, 
  'configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport").value', 
  '465' 

--Requires Aunthentication None(0) / Basic(1)
EXEC @hr = sp_oasetproperty @imsg, 
  'configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate").value', 
  '1' 

EXEC @hr = sp_oamethod @imsg, 'configuration.fields.update', null
EXEC @hr = sp_oasetproperty @imsg, 'to', @to
EXEC @hr = sp_oasetproperty @imsg, 'from', @from
EXEC @hr = sp_oasetproperty @imsg, 'subject', @subject

-- if you are using html e-mail, use 'htmlbody' instead of 'textbody'.

EXEC @hr = sp_oasetproperty @imsg, @bodytype, @body
EXEC @hr = sp_oamethod @imsg, 'send', null

SET @output_mesg = 'Success'

-- sample error handling.
IF @hr <>0 
 SELECT @hr
 BEGIN
  EXEC @hr = sp_oageterrorinfo null, @source out, @description out
  IF @hr = 0
  BEGIN
   --set @output_desc = ' source: ' + @source
   set @output_desc =  @description
  END
 ELSE
 BEGIN
  SET @output_desc = ' sp_oageterrorinfo failed'
 END
 IF not @output_desc is NULL
   SET @output_mesg = 'Error'
END
EXEC @hr = sp_oadestroy @imsg
GO

1 Ответ

0 голосов
/ 05 ноября 2009

Вы когда-нибудь пробовали использовать CDO? Вам не нужно ничего устанавливать Вот пример в DTS ActiveX, вы можете преобразовать его в T-SQL с помощью sp_OAMethod и т. Д .:

'**********************************************************************
'  Visual Basic ActiveX Script
'************************************************************************

Function Main()
    Set objMessage = CreateObject("CDO.Message") 
    objMessage.Subject = "Testing" 
    objMessage.From = "email" 
    objMessage.To = "email"
    objMessage.ReplyTo = "email" 
    objMessage.TextBody = "Sample Text" 
    objMessage.AddAttachment "Attachment File"

    objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 

    'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "SMTPServer"

    'Server port (typically 25)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 

    objMessage.Configuration.Fields.Update

    objMessage.Send
    Main = DTSTaskExecResult_Success
End Function
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...