Important alert: (current site time 7/15/2013 11:27:33 PM EDT)
 

VB icon

Add E-Mail Job to SQL Server

Email
Submitted on: 6/4/2001 5:19:22 PM
By: Lewis E. Moten III  
Level: Intermediate
User Rating: By 9 Users
Compatibility: ASP (Active Server Pages)
Views: 28359
author picture
(About the author)
 
     This script helps start you off to learning how to automate some of your processes with SQL . For this example, I have chosen to send Email through SQL Server using vbScript. Messages are pulled from a table and deleted once they are sent. You need to create a table in the master database called "Email" and assign the fields "To", "From", "Subject" and "Body". The script will do the rest. If you "SA" account has a password, you will need to modify the script to relflect those changes.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code 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 code (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 code 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 code or code's description.
				
'**************************************
' Name: Add E-Mail Job to SQL Server
' Description:This script helps start you off to learning how to automate some of your processes with SQL . For this example, I have chosen to send Email through SQL Server using vbScript. Messages are pulled from a table and deleted once they are sent. You need to create a table in the master database called "Email" and assign the fields "To", "From", "Subject" and "Body". The script will do the rest. If you "SA" account has a password, you will need to modify the script to relflect those changes.
' By: Lewis E. Moten III
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=6695&lngWId=4'for details.'**************************************

<%
Dim lObjConn
Dim vbs
Dim lStrSQL
Set lObjConn = Server.CreateObject("ADODB.Connection")
lObjConn.Open _
	"Provider=SQLOLEDB.1;" & _
	"Data Source=LOCALHOST;" & _
	"Initial Catalog=msdb;" & _
	"User ID=sa;" & _
	"Password=;"
lStrSQL = "sp_add_job " & _
	"@job_name = 'SendMailJob'," & _
	"@enabled = 1," & _
	"@description = 'Sends e-mail messages'," & _
	"@start_step_id = 1," & _
	"@category_name = '[Uncategorized (Local)]'"
	
Set lObjRs = lObjConn.Execute(lStrSQL)
vbs = GetScript("LOCALHOST", "Master", "")
vbs = Replace(vbs, "'", "''")
lStrSQL = "sp_add_jobstep " & _
	"@job_name = 'SendMailJob', " & _
	"@step_id = 1, " & _
	"@step_name = 'Find and Send Mail', " & _
	"@subsystem = 'ACTIVESCRIPTING', " & _
	"@command = '" & vbs & "'"
lObjConn.Execute lStrSQL
lStrSQL = "sp_add_jobschedule " & _
	"@job_name = 'SendMailJob', " & _
	"@name = 'Every 10 Minutes', " & _
	"@enabled = 1, " & _
	"@freq_type = 4, " & _
	"@freq_interval = 1, " & _
	"@freq_subday_type = 0x4, " & _
	"@freq_subday_interval = 10"
lObjConn.Execute lStrSQL
lStrSQL = ""
Set lObjConn = Nothing
Function GetScript(ByRef pStrDataSource, ByRef pStrInitialCatalog, ByRef pStrSAPassword)
	GetScript = _
		"Dim lObjConn" & vbCrLf & _
		"Dim lObjRs" & vbCrLf & _
		"Dim lStrSQL" & vbCrLf & _
		"Dim lObjMailer" & vbCrLf & _
		vbCrLf & _
		"Const adOpenForwardOnly = 0" & vbCrLf & _
		"Const adLockPessimistic = 2" & vbCrLf & _
		"Const adCmdText = 1" & vbCrLf & _
		vbCrLf & _
		"lStrSQL = ""SELECT [From], [To], [Subject], [Body] FROM [Email]""" & vbCrLf & _
		vbCrLf & _
		"Set lObjConn = CreateObject(""ADODB.Connection"")" & vbCrLf & _
		"Set lObjRs = CreateObject(""ADODB.Recordset"")" & vbCrLf & _
		"lObjConn.Open _" & vbCrLf & _
		"	""Provider=SQLOLEDB.1;"" & _" & vbCrLf & _
		"	""Data Source=" & pStrDataSource & ";"" & _" & vbCrLf & _
		"	""Initial Catalog=" & pStrInitialCatalog & ";"" & _" & vbCrLf & _
		"	""User ID=sa;"" & _" & vbCrLf & _
		"	""Password=" & pStrSAPassword & ";""" & vbCrLf & _
		vbCrLf & _
		"lObjRs.Open lStrSQL, lObjConn, adOpenForwardOnly, adLockPessimistic, adCmdText" & vbCrLf & _
		vbCrLf & _
		"While Not lObjRs.EOF" & vbCrLf & _
		"	Set lObjMailer = CreateObject(""CDONTS.NewMail"")" & vbCrLf & _
		"	lObjMailer.From		= lObjRs(0) & """"" & vbCrLf & _
		"	lObjMailer.To		= lObjRs(1) & """"" & vbCrLf & _
		"	lObjMailer.Subject	= lObjRs(2) & """"" & vbCrLf & _
		"	lObjMailer.Body		= lObjRs(3) & """"" & vbCrLf & _
		"	lObjMailer.Send" & vbCrLf & _
		"	lObjRs.Delete" & vbCrLf & _
		"	lObjRs.MoveNext" & vbCrLf & _
		"	Set lObjMailer = Nothing" & vbCrLf & _
		"Wend" & vbCrLf & _
		vbCrLf & _
		"lObjRs.Close" & vbCrLf & _
		"lObjConn.Close" & vbCrLf & _
		vbCrLf & _
		"Set lObjRs = Nothing" & vbCrLf & _
		"Set lObjConn = Nothing"
End Function
%>
done . . .<BR><BR>
You may wish to open SQL Enterprise Manger and find the
"Management" folder under your database. Find "SQL Server Agent" with
a child node called "Jobs". A new job called "SendMailJob" should be
present.


Other 102 submission(s) by this author

 


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 code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
6/6/2001 10:38:53 PMDave Buckner

This works great, thanks!
(If this comment was disrespectful, please report it.)

 
2/20/2002 12:33:05 AMRonnie Staxborn

I don't understand this code, Is there any way to use this code if I want to have mail remainder to people who have signed up for a mail a certain day. The day before they will get a notice by mail about the coming event the next day. There are about 30 members. Is there any good why to use this code for it?
(If this comment was disrespectful, please report it.)

 
5/30/2002 12:18:55 PMFelipe

I'd like to try your code, but where do I get the SP like sp_add_job,sp_add_jobschedule...
They are not in the master DB.

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

 
5/30/2002 1:06:12 PMLewis Moten

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

 
8/13/2002 2:18:38 PMPeter

I used your code but do not know where to save the file, and how to be able to retrieve information from SQL server to email. Is there anyway i can have a code to be able to retrieve a job from SQl server to email...Please help me, i'm new with asp

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

 
10/7/2002 10:35:19 AM

I have a problem in that the job runs and succeeds but i still not receiving emails. I have created the table and the columns. The script runs and then deletes the info from the email database but does not send the email.

please help

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

 
5/14/2003 4:27:15 AM

Sorryy but I have to graduate and I have a big problem (peraphs you have already to do this )??

I would like to know, how to send an email to user account at a certain date that is read from a database.

I use application write in asp that read from a database ACCESS

Have I to use a DTS (components into SQL Server??)

Please, help me!!!

Sorry for my english but ...

Enzo

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

 
1/19/2004 1:24:12 AM

How to run on this code? I have noticed that this is an asp file.Should I run the default.asp??
(If this comment was disrespectful, please report it.)

 
3/6/2006 1:31:19 PMemail job to sql server

email job to sql server
(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 code, please click here instead.)
 

To post feedback, first please login.