Sqlserver
 SQL >> Datenbank >  >> RDS >> Sqlserver

„profile name is not valid“ beim Mailversand aus SQL Server beheben

Ein Fehler profile name is not valid beim Versenden von Mails bedeutet fast immer, dass der an @profile_name übergebene Wert keinem vorhandenen Database-Mail-Profil entspricht.

Das Szenario

Angenommen, Sie führen dies aus, um einen Administrator über einen fehlgeschlagenen Auftrag des SQL Server Agent zu informieren:

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

und SQL Server antwortet:

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

Die Meldung ist eindeutig, die Ursache einfach: Database Mail versendet über Profile statt über Konten, und ein Profil namens DB Admin Profile existiert nicht.

Die fehlenden Bausteine nachliefern

Benötigt werden ein msdb-Benutzer, ein Database-Mail-Konto, ein Profil, das Konto im Profil und der Benutzer mit Zugriff darauf:

-- 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 = 'example@sqldat.com', @replyto_address = 'example@sqldat.com', @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;

Ersetzen Sie die Angaben durch Ihre eigenen und stellen Sie sicher, dass der genannte SMTP-Server tatsächlich funktioniert. Ist das erledigt, sollte der ursprüngliche Aufruf von sp_send_dbmail erfolgreich sein.