Sql-Server

使用 T-SQL 創建數據庫郵件返回“主體名稱無效”

  • March 8, 2018

配置數據庫郵件太長了。

所以我想創建一個自動為我創建的腳本

DECLARE @YourEmail NVARCHAR(50) SET @YourEmail = 'MyEmail@mydomain.com' --Put here your E-mail
DECLARE @YourPassword NVARCHAR(50) SET @YourPassword = 'MyPassword' --Put here your E-mail
DECLARE @YourSMTPSserver NVARCHAR(50) SET @YourSMTPSserver = 'MySMTPserver' --Put here your SMTP Server
DECLARE @YourPort int SET @YourPort = '25' --Put here your SMTP port

-- Create a Database Mail account  
EXECUTE msdb.dbo.sysmail_add_account_sp  
   @account_name = 'SQL_Spring_Cleaning_Account',  
   @description = 'Mail account for SQL_Spring_Cleaning.',  
   @email_address = @YourEmail,  
   @replyto_address = @YourEmail,
   @display_name = 'SQL_Spring_Cleaning Mailer',  
   @mailserver_name = @YourSMTPSserver, 
   @port = @YourPort,
   @use_default_credentials = 0,
   @username = @YourEmail,
   @password = @YourPassword;


-- Create a Database Mail profile  
EXECUTE msdb.dbo.sysmail_add_profile_sp  
   @profile_name = 'SQL_Spring_Cleaning_Profile',  
   @description = 'Profile used for SQL_Spring_Cleaning mail.' ; 

-- Add the account to the profile  
EXECUTE msdb.dbo.sysmail_add_profileaccount_sp  
   @profile_name = 'SQL_Spring_Cleaning_Profile',  
   @account_name = 'SQL_Spring_Cleaning_Account',  
   @sequence_number =1 ; 

-- Grant access to the profile to the DBMailUsers role  
EXECUTE msdb.dbo.sysmail_add_principalprofile_sp  
   @profile_name = 'SQL_Spring_Cleaning_Profile',  
   @principal_name = 'ASPIRES3\franc',  
   @is_default = 0 ; 

但不幸的是,SSMS 回復了我:

Msg 14607, Level 16, State 1, Procedure sysmail_verify_principal_sp, Line 49 [Batch Start Line 0]
principal name is not valid

錯誤明顯線上

@principal_name = 'ASPIRES3\franc', 

但我不明白那有什麼問題

您使用的名稱是 msdb 數據庫中的使用者嗎?

$$ @principal_name = $$‘principal_name’ 用於關聯的 msdb 數據庫中的數據庫使用者或角色的名稱。principal_name 是 sysname,預設值為 NULL。必須指定 principal_id 或 principal_name。“public”的 principal_name 使該配置文件成為公共配置文件,授予對數據庫中所有主體的訪問權限。

引用自:https://dba.stackexchange.com/questions/199681