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.
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)Requires Free Membership to View
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
Reader Feedback
Glaucio A. writes: "I have already used a procedure like this one and have trouble with messages bigger than 4000 bytes using SQL Server 2000. Does anyone have some idea how to workaround this limit?"
For More Information
- What do you think about this tip? E-mail the Editor at tdichiara@techtarget.com with your feedback.
- The Best SQL Server Web Links: tips, tutorials, scripts, and more.
- Have an SQL Server tip to offer your fellow DBA's and developers? The best tips submitted will receive a cool prize--submit your tip today!
- Ask your technical SQL Server questions--or help out your peers by answering them--in our live discussion forums.
- Check out our Ask the Experts feature: Our SQL, Database Design, Oracle, SQL Server, DB2, metadata, and data warehousing gurus are waiting to answer your toughest questions.
This was first published in May 2002

Join the conversationComment
Share
Comments
Results
Contribute to the conversation