Thursday, September 8, 2011

Procedure to send the Mail using SqlServer


We can use this procedure to send the mail from backend

create Procedure sp_SendMail   
 @mailBody varchar(8000),   
 @mailSubject varchar(2000),   
 @mailTo varchar(max),   
 @mailCCList varchar(max)   
As   
 --Set the profile name   
 Declare @emailProfileName as nvarchar(50)   
 select @emailProfileName=[value] from SystemConfig where [Name]='SenderAccountProfileName'   
   
 --configure mail profile if not already there   
 if Not Exists(SELECT profile_id, * FROM msdb.dbo.sysmail_profile WHERE name =@emailProfileName)   
  exec ConfigureMail   
   
   
 --Send Email   
 if(Len(RTrim(@mailTo)))>0   
 begin   
  EXEC msdb.dbo.sp_send_dbmail    
   @profile_name = @emailProfileName,   
   @recipients = @mailTo,   
    @copy_recipients = @mailCCList, 
   @subject = @mailSubject,   
   @body = @mailbody,   
   @body_format = 'HTML' ;   
 end   
    

No comments:

Post a Comment