Call now: (800) 766-1884  


 SQL Server Tips
 SQL Server Training

 SQL Server Consulting
 SQL Server Support
 SQL Server Remote DBA

 SQL Server Scripts
 Scripts Menu




  SQL Server Tips by Gama and Naughter

Applications of XP_SMTPSENDMAIL

One interesting application of XP_SMTPSENDMAIL is to get email addresses from a table and send an email to all of them. The sample databases Northwind and Pubs have several tables with contact information for employees, customers, etc. but, surprisingly, none has an email field. It really does not matter because we need real email addresses to send the emails, in case this code is used to test the XP, and verifying that they reached their destination. For testing purposes we will create a table named EmployeeInfo, with only one field named email:

CREATE TABLE EmployeeInfo(email varchar(50))

The table will get three records (these email addresses should be replaced with valid ones, even one email will do for the test):

INSERT EmployeeInfo(email) VALUES('')
INSERT EmployeeInfo(email) VALUES('')
INSERT EmployeeInfo(email) VALUES('')

Finally, a cursor will read the email address field from the table and feed it into XP_SMTPSENDMAIL:

SELECT email FROM EmployeeInfo
OPEN cur_email
FETCH NEXT FROM cur_email INTO @email
EXEC master..XP_SMTPSENDMAIL @From='',
@To=@email, @Subject='Friday meeting',
@Body='Meeting has been cancelled.'
FETCH NEXT FROM cur_email INTO @email
CLOSE cur_email
DEALLOCATE cur_email

This example, for minimalism, assumes that the sender, body and subject will be the same for all the emails. They could be taken from the table itself or from other source.

The above book excerpt is from:

Super SQL Server Systems
Turbocharge Database Performance with C++ External Procedures

ISBN: 0-9761573-2-2
Joseph Gama, P. J. Naughter  


Burleson Consulting Remote DB Administration







Burleson is the America's Team

Note: The pages on this site were created as a support and training reference for use by our staff of DBA consultants.  If you find it confusing, please exit this page.

Errata?  SQL Server technology is changing and we strive to update our SQL Server support information.  If you find an error or have a suggestion for improving our content, we would appreciate your feedback.  Just  e-mail:and include the URL for the page.

Burleson Consulting
SQL Server database support


Copyright 1996 -  2013 by Vaaltech Web Services. All rights reserved.

Hit Counter