Pages

Sunday, June 3, 2012

Sending SMTP email from SQL Server using CDO

Sending SMTP email from SQL Server using CDO

exec sendMail_With_CDOMessage
'SMTP server name Or SMTP IP'
, '1'
,'25'
,'SMTP User name'
,'SMTP User Password'
,'From which email'
,'To which email'
,'Email Subject'
,'Email Body Message'
,'Email attachment from the SQL with full window path <optional>'


Make sure that your SMTP is confugure to have relay option from the sending server.
SMTP can be configure on the server itself with IIS. Or you can use external SMTP server to send email.

It is very useful when you want to send some automated message from SQL server itself based on certain task completion or failure.

You need to give grant permisions on following stored procedures. Following procedures are present in Master Database so you need to give grant permision to your user in master database.
sp_oacreate ,sp_OADestroy,sp_OAGetErrorInfo ,sp_OASetProperty,sp_OAMethod 

CREATE PROCEDURE sendMail_With_CDOMessage
@smtpserver nvarchar(120)
,@smtpauthenticate nvarchar(120)
,@smtpserverport nvarchar(120)
,@sendusername nvarchar(120)
,@sendpassword nvarchar(120)
,@From nvarchar(120)
,@To nvarchar(120)
,@Subject nvarchar(120)=" "
,@Body nvarchar(4000) =" "
,@attachment nvarchar(400) = null
/*********************************************************************
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:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cdosys/html/_cdosys_messaging.asp
***********************************************************************/

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
if @hr <> 0
print 'CDO.Message Create failed'

--***************Configuring the Message Object ******************
-- This is to configure a remote SMTP server.
-- http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cdosys/html/_cdosys_schema_configuration_sendusing.asp
EXEC @hr = sp_OASetProperty @iMsg, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendusing").Value','2'
if @hr <> 0
print 'sendusing sp_OASetProperty failed'

-- 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("http://schemas.microsoft.com/cdo/configuration/smtpserver").Value', @smtpserver
if @hr <> 0
print 'smtpserver sp_OASetProperty failed'

exec @hr = sp_oasetproperty @imsg, 'configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate").value', @smtpauthenticate
if @hr <> 0
print 'smtpauthenticate sp_OASetProperty failed'

EXEC @hr = sp_OASetProperty @iMsg, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport").Value', @smtpserverport
if @hr <> 0
print 'smtpserverport sp_OASetProperty failed'

EXEC @hr = sp_OASetProperty @iMsg, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendusername").Value', @sendusername
if @hr <> 0
print 'sendusername sp_OASetProperty failed'

EXEC @hr = sp_OASetProperty @iMsg, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendpassword").Value', @sendpassword
if @hr <> 0
print 'sendpassword sp_OASetProperty failed'



-- Save the configurations to the message object.
EXEC @hr = sp_OAMethod @iMsg, 'Configuration.Fields.Update', null
if @hr <> 0
print 'Configuration.Fields.Update sp_OAMethod failed'

-- Set the e-mail parameters.
EXEC @hr = sp_OASetProperty @iMsg, 'To', @To
if @hr <> 0
print 'To sp_OASetProperty failed'
EXEC @hr = sp_OASetProperty @iMsg, 'From', @From
if @hr <> 0
print 'From sp_OASetProperty failed'
EXEC @hr = sp_OASetProperty @iMsg, 'Subject', @Subject
if @hr <> 0
print 'Subject sp_OASetProperty failed'

--adding an attachment: pwf
IF @attachment IS NOT NULL
EXEC @hr = sp_OAMethod @iMsg,'AddAttachment', NULL, @attachment
if @hr <> 0
print 'AddAttachment sp_OASetProperty failed'


-- If you are using HTML e-mail, use 'HTMLBody' instead of 'TextBody'.
EXEC @hr = sp_OASetProperty @iMsg, 'TextBody', @Body
if @hr <> 0
print 'TextBody sp_OASetProperty failed'

EXEC @hr = sp_OAMethod @iMsg, 'Send', NULL
if @hr <> 0
print 'Send sp_OASetProperty failed'


-- Sample error handling.
IF @hr <>0
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

 

No comments:

Post a Comment