Important alert: (current site time 7/15/2013 6:57:41 PM EDT)
 

article

Stored Procedures - send anonymous email over smtp

Email
Submitted on: 5/7/2003 10:06:25 AM
By: Pange  
Level: Intermediate
User Rating: By 8 Users
Compatibility: SQL Server 2000
Views: 57687
(About the author)
 
     This SP sends an email over SMTP with SQL Server 2000! It need's enough rights and "CDO for Windows 2000"!

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				
Anonymous Email over SMTP with SQL Server 2000
This SP sends an email over SMTP with SQL Server 2000!
It need's enough rights and "CDO for Windows 2000"!
CREATE PROCEDURE sp_SMTPemail
(
@From as nvarchar(50),
@To as nvarchar(50),
@Subject as nvarchar(255),
@Body as text
)

AS

-- Declare
DECLARE @message int
DECLARE @config int
DECLARE @hr int
DECLARE @src varchar(255), @desc varchar(255)

EXEC @hr = sp_OACreate 'CDO.Message', @message OUT -- create the message object
EXEC @hr = sp_OACreate 'CDO.Configuration', @config OUT -- create the configuration object

-- Configuration Object
EXEC @hr = sp_OASetProperty @config, 'Fields(cdoSendUsingMethod)', 'cdoSendUsingPort' -- Send the message using the network
EXEC @hr = sp_OASetProperty @config, 'Fields(cdoSMTPServer)', 'your.server.com' -- SMTP Server
EXEC @hr = sp_OASetProperty @config, 'Fields(cdoSMTPServerPort)', 25 -- Server SMTP Port
EXEC @hr = sp_OASetProperty @config, 'Fields(cdoSMTPAuthenticate)', 'cdoAnonymous' -- Anonymous SMTP Authenticate
EXEC sp_OAMethod @config, 'Fields.Update'

-- Message Object
EXEC @hr = sp_OASetProperty @message, 'Configuration', @config -- set message.configuration = config
EXEC @hr = sp_OASetProperty @message, 'To', @To
EXEC @hr = sp_OASetProperty @message, 'From', @From
EXEC @hr = sp_OASetProperty @message, 'Subject', @Subject
EXEC @hr = sp_OASetProperty @message, 'TextBody', @Body
EXEC sp_OAMethod @message, 'Send()'

-- Destroys the objects
EXEC @hr = sp_OADestroy @message
EXEC @hr = sp_OADestroy @config

-- Errorhandler
IF @hr <> 0
BEGIN
EXEC sp_OAGetErrorInfo @message, @src OUT, @desc OUT
SELECT hr=convert(varbinary(4),@hr), Source=@src, Description=@desc
RETURN
END
GO

SQL Example to execute the SP:
EXEC sp_SMTPemail 'from@example.email.com', 'to@example.email.com', 'Subjecttext','Messagetext'
 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
5/7/2003 10:20:21 AM

Great Job! It works excellent...
(If this comment was disrespectful, please report it.)

 
5/8/2003 9:21:35 AM

es una pena que no sepa vuestro idioma para expresar muy agradecimiento a persona que ponen a disposicion lo que saben...
Gracias
(If this comment was disrespectful, please report it.)

 
5/25/2003 12:54:55 PM

EMAIL
(If this comment was disrespectful, please report it.)

 
7/8/2003 9:41:28 AM

hi your product will be very good cause i like sending bulk mail
(If this comment was disrespectful, please report it.)

 
7/30/2003 1:32:25 AMJing Polito

Hello, thanks for sharing this thing... but it seems like its not working for me... i already set my SMTP server... and when I run the sp i get this "The command(s) completed successfully." but I got no message (I addressed it to myself for verification). Thanks for your time. BTW, am not familiar with CDO for Windows 2000.
(If this comment was disrespectful, please report it.)

 
9/15/2003 11:34:53 AM

I was able to run it without any error. However, no message was received by the intended recepient. BTW, what is CDO for Win2K?
(If this comment was disrespectful, please report it.)

 
10/2/2003 9:50:13 AM

It does not work on my sql2000. If i use
sp_OAGetErrorInfo to test for error.I get a type mismatch error after setting the config property to @config
(If this comment was disrespectful, please report it.)

 
10/8/2003 11:13:14 AM

Thanks for sharing this
It only work if you have iis on the same server as your sql server. It does not use the smtp server setting at all.

(If this comment was disrespectful, please report it.)

 
12/23/2003 3:25:02 PM

How can i 'do if i'need call this store procedure to send a text field in a table .
(If this comment was disrespectful, please report it.)

 
12/23/2003 3:27:21 PM

how can i'do if i'need call this store procedure to send a text field from my database as a @body parameter, thanks

(If this comment was disrespectful, please report it.)

 
1/25/2005 1:42:33 PM

This has not worked for me... I don't know why. I found one on MS that worked.

http://support.microsoft.com/default.aspx?scid=kb;en-us;312839
(If this comment was disrespectful, please report it.)

 
2/11/2005 9:35:16 AM

I am having the same problem as most of these people, I get verification it was executed but no email gets sent
(If this comment was disrespectful, please report it.)

 
5/16/2005 2:45:12 AM

sir
I am trying to execute the same code given in the answer but it fails to work

config part returns non zero value

plz help
(If this comment was disrespectful, please report it.)

 
5/16/2005 2:47:18 AM

sir
I am trying to send mail using the code given in the page but it is not working

the config returns non zero value

plz help and pass the way

thanking you
(If this comment was disrespectful, please report it.)

 
5/1/2006 4:21:38 PMjayson

I tested it out and works great..I'm using sql 2000 on server 2003 flatform.
Nice job dude!!!
(If this comment was disrespectful, please report it.)

 
3/28/2007 10:34:17 AMjuned ahmed

trigers cursors for selling houses
(If this comment was disrespectful, please report it.)

 
4/1/2007 11:04:39 PMTejas

Hello, thanks for sharing this thing... but it seems like its not working for me... i already set my SMTP server... and when I run the sp i get this "The command(s) completed successfully." but I got no message (I addressed it to myself for verification). Thanks for your time. I am using SQL 2000 with 2003 server
(If this comment was disrespectful, please report it.)

 
2/29/2008 2:10:46 AMRolf

It seems like its not working! I already set my SMTP server and I'm a member of the sysadmin-Group... and when I run the sp I get the message "The command(s) completed successfully."
(If this comment was disrespectful, please report it.)

 
4/30/2008 11:57:20 AMgary

This works as expected, for those of you struggling - remember to enter the 4 parameters
(If this comment was disrespectful, please report it.)

 
7/2/2008 10:41:12 AMDavid

I am getting similar non results. I changed the smpt server name and made sure I am a member of all the sysadmin groups. I do not get an error but no email. I have seen this in several emails. Could someone post common occurences of what resolution was?
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.