Quick Search for:  in language:    
SMTP,SQL,CDO,sends,email,over,with,Server,nee
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
SQL Stats

 Code: 44,501. lines
 Jobs: 125. postings

 How to support the site

 
Sponsored by:

 
You are in:
 
Login





Latest Code Ticker for SQL.
Beginners pack
By hardik shah on 1/11


Inline Views
By Thivya Prabakaran on 1/6


Create thousand Data Base users quickly
By N.D.I.Samantha on 1/1


Numbers from String
By Jeff Michelson on 12/30


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!

Affiliate Sites



 
 
   

Stored Procedures - send anonymous email over smtp

Print
Email
 

Submitted on: 5/7/2003 10:06:25 AM
By: Pange  
Level: Intermediate
User Rating: By 7 Users
Compatibility:SQL Server 2000

Users have accessed this article 6258 times.
 
(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 notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
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...
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/25/2003 12:54:55 PM:
EMAIL
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/8/2003 9:41:28 AM:
hi your product will be very good cause i like sending bulk mail
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/30/2003 1:32:25 AM:Jing 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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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 .
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this article has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular article, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | SQL Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.