?
Solved

sending email from Sql server 2005 stored procedure

Posted on 2010-09-13
7
Medium Priority
?
298 Views
Last Modified: 2012-05-10
I want to send email alert while running the sql server stored procedure in 2005.
This sp generates the exception table .I want to include that table in that email.
0
Comment
Question by:petersti
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 13

Accepted Solution

by:
Wizilling earned 2000 total points
ID: 33668047
0
 
LVL 4

Expert Comment

by:timexist
ID: 33668168
there are serial steps involved in this email process.
step 1, setup database email profile.
step 2, enable database email.
step 3, save your exception in a table.
step 4, send out email include certain data fields and records of exception table.

so which step you are not very clear about?

for step 1, step 2
check this:
http://www.kodyaz.com/articles/sql2005-database-mail.aspx


and some code for sending the email.

to attach the table you use a loop to build the body.



create   PROCEDURE [dbo].[AP_SEND_SMTP_MAIL]
	@From varchar(255) = null,
	@To varchar(8000) = null,
	@Subject varchar(100)= ' ',
	@Body varchar(4000) = ' ',
	@Attachment varchar(5000)= null,
	@SMTP_server varchar(16) = null,
	@SMTP_port varchar(16) = '25',
	@SMTP_user varchar(32) = null,
	@SMTP_password varchar(64) = null,
	@db_mail_profile sysname = null
WITH EXECUTE AS 'dbo'
AS


Declare	@errmsg varchar(500),  @report_path varchar(255), @return_status int, @cmd varchar(255), @outVar INT
Declare @sql varchar(4000) 
Declare @servername varchar(200) 

-- you must pass a @from value
If @From is null and @db_mail_profile is null		
  Begin
	select @errmsg = 'No From Value Specified'
	RAISERROR(@errmsg, 11, 2) WITH SETERROR
	return -101
  End

If @to is null
  Begin
	select @errmsg = 'No To Value Specified'
	RAISERROR(@errmsg, 11, 2) WITH SETERROR
	return -101
  End


If @smtp_server is null and @db_mail_profile is null
  Begin
	select @errmsg = 'Either an SMTP Server or a database mail profile must be specified'
	RAISERROR(@errmsg, 11, 2) WITH SETERROR
	return -101
  End

If @db_mail_profile is not null
  Begin


 	select @servername = @@servername
	select @sql = 'EXEC msdb.dbo.sp_send_dbmail '
	   + '@profile_name =  ' + '''' + @db_mail_profile + '''' + ','
	   + '@recipients   =  ' + '''' + @to + '''' +','
	   + '@subject	   =  ' + '''' + @subject + '''' + ','
	   + '@body		   =  ' + '''' + @body + '''' + ','
	   + '@file_attachments	=  ' + '''' + @attachment + ''''

	select @sql = 'sqlcmd -E -S ' + @servername + ' -q' + ' "' + @sql + '"'


	exec @return_status = master..xp_cmdShell @sql


	If @return_status > 0
	  Begin
		return -101
	  End


  End
Else
  Begin

	Declare @iMsg int
	Declare @hr int
	Declare @source varchar(255)
	Declare @description varchar(500)
	Declare @output varchar(1000)
	EXEC @hr = sp_OACreate 'CDO.Message', @iMsg OUT

	EXEC @hr = sp_OASetProperty @iMsg, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendusing").Value','2'

	-- This is to configure the Server Name or IP address.
	EXEC @hr = sp_OASetProperty @iMsg, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpserver").Value', @smtp_server

	-- configure the smtp_port 
	If @smtp_port is not null
		EXEC @hr = sp_OASetProperty @iMsg, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport").Value', @smtp_port
		
	-- configure the smtp_authentication 
	If @smtp_user is not null
	  Begin
		EXEC @hr = sp_OASetProperty @iMsg, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate").Value','1'
		EXEC @hr = sp_OASetProperty @iMsg, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendusername").Value', @smtp_user
		EXEC @hr = sp_OASetProperty @iMsg, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendpassword").Value', @smtp_password
	  End



	-- Save the configurations to the message object.
	EXEC @hr = sp_OAMethod @iMsg, 'Configuration.Fields.Update', null


	-- Set the e-mail parameters.
	EXEC @hr = sp_OASetProperty @iMsg, 'To', @To
	EXEC @hr = sp_OASetProperty @iMsg, 'From', @From
	EXEC @hr = sp_OASetProperty @iMsg, 'Subject', @Subject



	-- Add an attachment to the message
	If @attachment is not null
		--EXEC @hr = sp_OAMethod @iMsg, 'AddAttachment', null, @attachment		
	  Begin
		Select @outVar  = NULL
		EXEC @hr = sp_OAMethod @iMsg, 'AddAttachment', @outVar OUT, @attachment
	  End

	--
	-- Send E-Mail using 'TextBody'.
	--
	EXEC @hr = sp_OASetProperty @iMsg, 'TextBody', @Body
	EXEC @hr = sp_OAMethod @iMsg, 'Send', NULL

	--
	-- Error handling.
	--
	IF @hr <>0
	  Begin

		EXEC @hr = sp_OAGetErrorInfo NULL, @source OUT, @description OUT

		select @errmsg = 'SMTP Error:  Source: '+ IsNull(@source, 'null') + '  Description:  ' + IsNull(@description, 'null')
		RAISERROR(@errmsg, 11, 2) WITH SETERROR
		EXEC @hr = sp_OADestroy @iMsg
		return -101

	  End

	-- Clean up the objects created.
	EXEC @hr = sp_OADestroy @iMsg
  End

Open in new window

0
 

Author Comment

by:petersti
ID: 33668327
hi Wizilling:,

I am getting the following error when I use the msdb.dbo.sp_send_dbmail

SQL Server blocked access to procedure 'dbo.sp_send_dbmail' of component 'Database Mail XPs' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'Database Mail XPs' by using sp_configure. For more information about enabling 'Database Mail XPs', see "Surface Area Configuration" in SQL Server Books Online.
0
10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

 
LVL 4

Expert Comment

by:timexist
ID: 33668343
create   PROCEDURE [dbo].[AP_SEND_SMTP_MAIL]
      @From varchar(255) = null,
      @To varchar(8000) = null,
      @Subject varchar(100)= ' ',
      @Body varchar(4000) = ' ',
      @Attachment varchar(5000)= null,
      @SMTP_server varchar(16) = null,
      @SMTP_port varchar(16) = '25',
      @SMTP_user varchar(32) = null,
      @SMTP_password varchar(64) = null,
      @db_mail_profile sysname = null
WITH EXECUTE AS 'dbo'
AS

"WITH EXECUTE AS 'dbo'" is the key.




0
 
LVL 4

Expert Comment

by:timexist
ID: 33668355
You can enable the Database Mail feature by pressing the "Yes" button or by running the below sql sp_configure mail script



run this script to enable your 'Database Mail XPs'

USE Master
GO
sp_configure 'show advanced options', 1
GO
reconfigure with override
GO
sp_configure 'Database Mail XPs', 1
GO
reconfigure
GO
sp_configure 'show advanced options', 0
GO

Open in new window

0
 

Author Comment

by:petersti
ID: 33691658
I am unable to send email.Because I am getting error mail server is not connecting.
0
 

Author Closing Comment

by:petersti
ID: 33921377
Part of my problem
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are some very powerful Dynamic Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a di…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question