I have set up 3 database mail profiles in a SQL Server 2012 STD edition instance. One profile is for the DBA admin group and is the default. The other two are for an application. The app has a dev and a test database in the same instance. So I tried setting up logic in a proc to check the name of the database, and if it ends with DEV, then use the DEV profile, otherwise it uses the TEST profile.
I wanted to use a variable to send to sp_send_dbmail for the @profle_name. The 2 profiles are 'APP_DEV' and 'APP_TEST'. Each has an app group email assigned. I also have added the logic for additonal recipients which works fine.
Basically it's like this:
DECLARE @mail_profile_name sysname
DECLARE @subj varchar(80) = 'Testing....'
DECLARE @recips varchar(100) = 'mail1@abc.com;mail2@abc.com'
if this is the dev database....
set @mailProfileName = 'APP_DEV'
else
set @mailProfileName = 'APP_TEST'
exec msdb.dbo.sp_send_dbmail
@profile_name = @mailProfileName,
@recipients = @recips,
@body = @body,
@subject = @subj
I get this error:
Msg 14607, Level 16, State 1, Procedure sysmail_verify_profile_sp, Line 42 profile name is not valid
If I hardcode the profile name in the call to sp_send_dbmail, it works fine.
exec msdb.dbo.sp_send_dbmail
@profile_name = 'APP_DEV',
@recipients = @recips,
@body = @body,
@subject = @subj
I don't understand why the call to sysmail_verify_profile_sp from sp_send_dbmail is failing. Anyone have any ideas?