title: msdb.dbo.sp_send_dbmail does not execute within a stored procedure
body: <p>I've used the following code to set up deadlock monitoring:</p>
  
  <p><a href="http://weblogs.sqlteam.com/mladenp/archive/2008/07/18/Immediate-deadlock-notifications-without-changing-existing-code.aspx" rel="nofollow">http://weblogs.sqlteam.com/mladenp/archive/2008/07/18/Immediate-deadlock-notifications-without-changing-existing-code.aspx</a></p>
  
  <p>and it works fine.
  The problem I have is that msdb.dbo.sp_send_dbmail doesn't seem to work.
  If I manually execute from my session, <strong>It works fine</strong>, but from within the stored procedure, the following error occurs:</p>
  
  <p>2011-08-23 16:42:45.28 spid219s    The activated proc [dbo].[usp_ProcessNotification] running on queue Wayne.dbo.DeadLockNotificationsQueue output the following:  'Error WHILE receiving Service Broker message FROM queue DeadLockNotificationsQueue.
                          DATABASE Name: Wayne; Error number: 229; Error Message: The EXECUTE permission was denied on the object 'sp_send_dbmail', database 'msdb', schema 'dbo'.'</p>
  
  <p>I also ran the following:</p>
  
  <p>EXEC msdb.dbo.sp_addrolemember @rolename = 'DatabaseMailUserRole'
      ,@membername = '';</p>
  
  <p>with my logon as the membername but it didn't seem to help.</p>
  
  <p>What do I need to do to rectify this?</p>
  
  <p>Thanks.</p>
  
  <p>Wayne.</p>