Technical Article

Sending mail using CDONTS and Sql Server

,

This stored procedure can be used to send mail through CDONTS and SQL Server. It resides in the Master database and accepts email addresses and subjects with a maximum of 50 characters and a body with a maximum of 500 characters. (You can increase or decrease these according to your requirements.) For this stored procedure to run, CDONTS must be installed on the database server.

              

Use Master
If(exists(select name from sysobjects where name='sp_MailProc' and type='P')) 
        drop procedure sp_MailProc 
Go 

Create Procedure sp_MailProc @mailFrom varchar(50), @mailTo varchar(50), @mailSubject varchar(50), @mailbody varchar(500)

As 

Declare @cdoresult int, @cdo int, @i1 tinyint, @i2 tinyint, @j1 tinyint, @j2 tinyint, @k1 tinyint, @k2 tinyint 
DECLARE @source varchar(255), @descripton varchar(255),@lenMail tinyint 

--Checking some basic validations for mail:from 
Set @i1=charindex('@',@mailFrom) 
Set @i2=charindex('@',@mailFrom,@i1+1) 
Set @j1=charindex('.',@mailFrom,@i1) 
Set @j2=charindex('.',@mailFrom)+1 
Set @k1=charindex(',',@mailFrom) 
Set @k2=charindex(' ',@mailFrom) 

Set @lenMail=len(@mailFrom) 
If NOT((@i1>0) and (@j1>(1+1)) and (@k1=0) and (@i2=0) and (@k2=0) and (@lenMail-@j2 >=2) and (@lenMail-@j2<=3)) 
Begin 
        Print('Please enter valid mail:from Email Id') 
        Return(1) 
End 

--Checking some basic validations for mail:to 
Set @i1=charindex('@',@mailTo) 
Set @i2=charindex('@',@mailTo,@i1+1) 
Set @j1=charindex('.',@mailTo,@i1) 
Set @k1=charindex(',',@mailTo) 
Set @k2=charindex(' ',@mailTo) 

Set @lenMail=len(@mailTo) 
If NOT((@i1>0) and (@j1>(1+1)) and (@k1=0) and (@i2=0) and (@k2=0)) 
Begin 
        Print('Please enter valid mail:to Email Id') 
        Return(1) 
End 

--Creating the new mail object from CDO NTS component 
Exec @cdoresult = sp_OACreate 'CDONTS.NewMail', @cdo out 
If(@cdoresult<>0) 
Begin 
   Exec sp_OAGetErrorInfo @cdo, @source OUT, @descripton OUT 
   Select hr=convert(varbinary(4),@cdoresult), Source=@source, Description=@descripton 
   return(1) 
End 

Exec @cdoResult  = sp_OASetProperty @cdo, 'BodyFormat', 0 
If(@cdoresult<>0) 
Begin 
   Exec sp_OAGetErrorInfo @cdo, @source OUT, @descripton OUT 
   Select hr=convert(varbinary(4),@cdoresult), Source=@source, Description=@descripton 
   return(1) 
End 

Exec @cdoResult  = sp_OASetProperty @cdo, 'MailFormat', 0                               
If(@cdoresult<>0) 
Begin 
   Exec sp_OAGetErrorInfo @cdo, @source OUT, @descripton OUT 
   Select hr=convert(varbinary(4),@cdoresult), Source=@source, Description=@descripton 
   return(1) 
End 

--Sending the mail 
Exec @cdoresult = sp_OAMethod @cdo, 'Send', NULL, @mailFrom, @mailTo, @mailSubject, @mailBody, 2                        

If(@cdoresult<>0) 
Begin 
   Exec sp_OAGetErrorInfo @cdo, @source OUT, @descripton OUT 
   Select hr=convert(varbinary(4),@cdoresult), Source=@source, Description=@descripton 
   return(1) 
End 

--Destroy the mail object 
Exec @cdoresult = sp_OADestroy @cdo 

return(0) 
Go

Rate

3.33 (3)

You rated this post out of 5. Change rating

Share

Share

Rate

3.33 (3)

You rated this post out of 5. Change rating