Search This Blog

Saturday, August 18, 2012

msdb.dbo.sp_send_dbmail SQL Server sp_send_dbmail

SQL Server > System Stored Procedures > sp_send_dbmail

Using sp_send_dbmail you can send an e-mail message to the specified recipients.
This stored procedure is in placed the msdb database.

Syntax and parameters:

sp_send_dbmail @profile_name , @recipients , @copy_recipients, @blind_copy_recipients ,  @from_address, @reply_to, @subject, @body, @body_format, @importance, @file_attachments
 
Example send email with file attached:
 
declare @_recipients nvarchar(max)=''
declare @_ord_id nvarchar(255)
declare @_subject nvarchar(max)
declare @_body nvarchar(max)

set @_subject = 'Order with id=11 was delivered.'; set @_body = 'Order with id=11 was delivered.' + CHAR(13) +CHAR(13) + 'PLEASE DON''T REPLY TO THIS EMAIL!';
set @_recipients = 'to@domain.com'

exec msdb.dbo.sp_send_dbmail @profile_name = 'smtp profile name',
@recipients = @_recipients, @from_address = 'order@domain.com', @subject = @_subject, @body = @_body, @body_format = 'TEXT', @file_attachments ='C:\File.txt';




Where to find the definition?
 
USE msdb;
GO sp_helptext 'dbo.sp_send_dbmail';
SELECT
   definition FROM    sys.sql_modules WHERE    object_id = OBJECT_ID('dbo.sp_send_dbmail');

Database Mail Configuration Wizard

To start the Database Mail Configuration Wizard, in Object Explorer, expand Management, right-click Database Mail, and then click Configure Database Mail.