Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

stored procedure for email

Status
Not open for further replies.

oakpark5

Programmer
Sep 2, 2004
81
0
0
US
Any ideas on setting up a stored procedure or exe. that can tell an exchnage server to send an email. Let me explain the problem. We set up a site where a user can request a password from a website. The website looks up the password on a sql server and then sends the password via email through an exchange server. Well it ended up in a big argument about security becasue the network admin doesnt want to open up the exchange server to the web server. So my thought was to set up a stored procedure or exe that when the query for the password runs it starts a stored procedure or script that sends or tells the exchange server to send an email with the password in it. If anyone can hepl me on this i would be very thankful.

Software Engineer
Team Lead
damn bill gates, he has everything
 
Hi Oakpark5.

You'll find plenty of references to CDOSYS in the SQL forums here at Tek-Tips. The CDOSYS objects allow e-mail to be sent via Exchange but originating from your SQL server. We use a proc like this (the comments explain things pretty well):

Code:
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE [dbo].[spSMP_send_cdosysmail] 
   @From varchar(100) ,
   @To varchar(100) ,
   @Subject varchar(100)=" ",
   @Body varchar(4000) =" "
/*********************************************************************

This stored procedure takes the above parameters and sends an e-mail. 
All of the mail configurations are hard-coded in the stored procedure. 
Reference to the CDOSYS objects are at the following MSDN Web site:
[URL unfurl="true"]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cdosys/html/_cdosys_messaging.asp[/URL]

***********************************************************************/ 
   AS
   Declare @iMsg int
   Declare @hr int
   Declare @source varchar(255)
   Declare @description varchar(500)
   Declare @output varchar(1000)

--************* Create the CDO.Message Object ************************
   EXEC @hr = sp_OACreate 'CDO.Message', @iMsg OUT

--***************Configuring the Message Object ******************
-- This is to configure a remote SMTP server.
-- [URL unfurl="true"]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cdosys/html/_cdosys_schema_configuration_sendusing.asp[/URL]
   EXEC @hr = sp_OASetProperty @iMsg, 'Configuration.fields("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/sendusing").Value','2'[/URL]
-- This is to configure the Server Name or IP address. 
-- Replace MailServerName by the name or IP of your SMTP Server.
   EXEC @hr = sp_OASetProperty @iMsg, 'Configuration.fields("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserver").Value',[/URL] 'MailServerName' 

-- Save the configurations to the message object.
   EXEC @hr = sp_OAMethod @iMsg, 'Configuration.Fields.Update', null

-- Set the e-mail parameters.
   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, 'TextBody', @Body
   EXEC @hr = sp_OAMethod @iMsg, 'Send', NULL

-- Sample error handling.
   IF @hr <> 0 
     select @hr
     BEGIN
       EXEC @hr = sp_OAGetErrorInfo NULL, @source OUT, @description OUT
       IF @hr = 0
         BEGIN
           SELECT @output = '  Source: ' + @source
           PRINT  @output
           SELECT @output = '  Description: ' + @description
           PRINT  @output
         END
       ELSE
         BEGIN
           PRINT '  sp_OAGetErrorInfo failed.'
           RETURN
         END
     END

-- Do some error handling after each step if you need to.
-- Clean up the objects created.
   EXEC @hr = sp_OADestroy @iMsg
   


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

I always stick the proc in the master database.

Good luck.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top