Fix “profile name is not valid” When Sending Mail from SQL Server

If you try to send mail from SQL Server, but you get a “profile name is not valid” it’s probably because you haven’t specified a valid profile for the @profile_name argument.

Example

For example, let’s say you’re using the following T-SQL code to send a notification email to the administrator whenever a SQL Server Agent job fails:

EXEC msdb.dbo.sp_send_dbmail  
    @profile_name = 'DB Admin Profile',  
    @recipients = '[email protected]',  
    @body = 'Your favorite SQL Server Agent job just failed',  
    @subject = 'SQL Server Agent Job: FAILED';

But you get the following error:

Msg 14607, Level 16, State 1, Procedure msdb.dbo.sysmail_verify_profile_sp, Line 42
profile name is not valid

This error specifically tells us that the “profile name is not valid”.

To send email with Database Mail you need to specify a profile instead of a user account directly. In this case, I specified a @profile_name of DB Admin Profile, but such a profile doesn’t actually exist.

Before I can send mail, what I need to do is add a user to the msdb database, create a database mail account, then create a profile and add the database mail account to that profile. I then need to add the user to the profile.

All up, it will look something like this:

-- Switch to the msdb database
USE msdb;

-- Create a user on the msdb database
CREATE USER Marge FOR LOGIN Marge;

-- Create a Database Mail account  
EXECUTE msdb.dbo.sysmail_add_account_sp  
    @account_name = 'DB Admin',  
    @description = 'Mail account for admin emails.',  
    @email_address = '[email protected]',  
    @replyto_address = '[email protected]',  
    @display_name = 'DB Automated Mailer',  
    @mailserver_name = 'smtp.example.com',
    @port = 25;  
  
-- Create a Database Mail profile  
EXECUTE msdb.dbo.sysmail_add_profile_sp  
    @profile_name = 'DB Admin Profile',  
    @description = 'Profile for admin emails.';  
  
-- Add the account to the profile  
EXECUTE msdb.dbo.sysmail_add_profileaccount_sp  
    @profile_name = 'DB Admin Profile',  
    @account_name = 'DB Admin',  
    @sequence_number = 1;
  
-- Grant user access to the Database Mail profile
EXECUTE msdb.dbo.sysmail_add_principalprofile_sp
    @profile_name = 'DB Admin Profile',
    @principal_name = 'Marge',
    @is_default = 1;

Obviously, you’ll need to replace the various details with your own. This also assumes that you specify a mail server that works.

Once done, you should be ready to send mail.