建立 Database Mail 設定檔

您可以使用 [Database Mail 組態精靈] 或 Transact-SQL,建立 Database Mail 公用和私人設定檔。

  • 開始之前:必要條件、安全性

  • 使用下列項目建立 Database Mail 私人設定檔:Database Mail 組態精靈、Transact-SQL

  • 使用下列項目建立 Database Mail 公用設定檔:Database Mail 組態精靈、Transact-SQL

開始之前

必要條件

替該設定檔建立一個或多個 Database Mail 帳戶。 如需有關建立 Database Mail 帳戶的詳細資訊,請參閱<建立 Database Mail 帳戶>。

安全性

公用設定檔可以讓任何可存取 msdb 資料庫的使用者使用該設定檔來傳送電子郵件。 使用者或角色可以使用私人設定檔。 為角色授與設定檔的存取權限時,會建立能夠更輕鬆維護的架構。 您必須是 msdb 資料庫中之 DatabaseMailUserRole 的成員,而且至少可以存取一個 Database Mail 設定檔,才能傳送郵件。

權限

建立設定檔帳戶以及執行預存程序的使用者,應該是系統管理員 (sysadmin) 固定伺服器角色的成員。

[回到頁首]

使用 Database Mail 組態精靈

建立 Database Mail 設定檔

  • 在 [物件總管] 中,連接到想要在其上設定 Database Mail 的 SQL Server 執行個體,並展開伺服器樹狀目錄。

  • 展開 [管理] 節點。

  • 按兩下 Database Mail,開啟 [Database Mail 組態精靈]。

  • [選取組態工作] 頁面上,選取 [管理 Database Mail 帳戶和設定檔] 選項,並按 [下一步]

  • [管理設定檔和帳戶] 頁面上,選取 [建立新設定檔] 選項,並按 [下一步]

  • [新增設定檔] 頁面上,指定設定檔名稱、描述,以及加入要併入設定檔中的帳戶,並按 [下一步]

  • [完成精靈] 頁面上,檢閱要執行的動作,並按一下 [完成] 以完成新設定檔的建立。

  • 若要設定 Database Mail 私人設定檔:

    • 開啟 [Database Mail 組態精靈]。

    • [選取組態工作] 頁面上,選取 [管理 Database Mail 帳戶和設定檔] 選項,並按 [下一步]

    • [管理設定檔和帳戶] 頁面上,選取 [管理設定檔安全性] 選項,並按 [下一步]

    • [私人設定檔] 索引標籤中,選取想要設定之設定檔的核取方塊,並按 [下一步]

    • [完成精靈] 頁面上,檢閱要執行的動作,並按一下 [完成] 完成設定檔的設定。

  • 若要設定 Database Mail 公用設定檔:

    • 開啟 [Database Mail 組態精靈]。

    • [選取組態工作] 頁面上,選取 [管理 Database Mail 帳戶和設定檔] 選項,並按 [下一步]

    • [管理設定檔和帳戶] 頁面上,選取 [管理設定檔安全性] 選項,並按 [下一步]

    • [公用設定檔] 索引標籤中,選取想要設定之設定檔的核取方塊,並按 [下一步]

    • [完成精靈] 頁面上,檢閱要執行的動作,並按一下 [完成] 完成設定檔的設定。

[回到頁首]

使用 Transact-SQL

建立 Database Mail 私人設定檔

  • 連接到 SQL Server 執行個體。

  • 若要建立新的設定檔,請執行系統預存程序 sysmail_add_profile_sp (Transact-SQL),如下所示:

    EXECUTE msdb.dbo.sysmail_add_profile_sp

    @profile\_name = 'Profile Name'

    @description = 'Desciption'

    其中,@profile_name 是設定檔的名稱,而 @description 是設定檔的描述。 這個參數是選擇性的。

  • 針對每個帳戶,執行預存程序 sysmail_add_profileaccount_sp (Transact-SQL),如下所示:

    EXECUTE msdb.dbo.sysmail_add_profileaccount_sp

    @profile\_name = 'Name of the profile'

    @account\_name = 'Name of the account'

    @sequence\_number = 'sequence number of the account within the profile. '

    其中,@profile_name 是設定檔的名稱,@account_name 是要加入至設定檔之帳戶的名稱,而 @sequence\_number 可決定帳戶在設定檔中的使用順序。

  • 針對使用此設定檔傳送郵件的每個資料庫角色或使用者,授與設定檔的存取權。 作法是執行預存程序 sysmail_add_principalprofile_sp (Transact-SQL),如下所示:

    EXECUTE msdb.sysmail_add_principalprofile_sp

    @profile\_name = 'Name of the profile'

    @ principal_name = 'Name of the database user or role'

    @is\_default = 'Default Profile status '

    其中,@profile_name 是設定檔的名稱,@principal_name 是資料庫使用者或角色的名稱,而 @is\_default 可決定此設定檔是否為資料庫使用者或角色的預設值。

下列範例會建立 Database Mail 帳戶、建立 Database Mail 私人設定檔,然後將帳戶加入至設定檔,並將設定檔的存取權授與 msdb 資料庫中的 DBMailUsers 資料庫角色。

-- Create a Database Mail account
EXECUTE msdb.dbo.sysmail_add_account_sp
    @account_name = 'AdventureWorks Administrator',
    @description = 'Mail account for administrative e-mail.',
    @email_address = 'dba@Adventure-Works.com',
    @replyto_address = 'danw@Adventure-Works.com',
    @display_name = 'AdventureWorks Automated Mailer',
    @mailserver_name = 'smtp.Adventure-Works.com' ;

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

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

-- Grant access to the profile to the DBMailUsers role
EXECUTE msdb.dbo.sysmail_add_principalprofile_sp
    @profile_name = 'AdventureWorks Administrator Profile',
    @principal_name = 'ApplicationUser',
    @is_default = 1 ;

[回到頁首]

建立 Database Mail 公用設定檔

  • 連接到 SQL Server 執行個體。

  • 若要建立新的設定檔,請執行系統預存程序 sysmail_add_profile_sp (Transact-SQL),如下所示:

    EXECUTE msdb.dbo.sysmail_add_profile_sp

    @profile\_name = 'Profile Name'

    @description = ' Desciption'

    其中,@profile_name 是設定檔的名稱,而 @description 是設定檔的描述。 這個參數是選擇性的。

  • 針對每個帳戶,執行預存程序 sysmail_add_profileaccount_sp (Transact-SQL),如下所示:

    EXECUTE msdb.dbo.sysmail_add_profileaccount_sp

    @profile\_name = 'Name of the profile'

    @account\_name = 'Name of the account'

    @sequence\_number = 'sequence number of the account within the profile. '

    其中,@profile_name 是設定檔的名稱,@account_name 是要加入至設定檔之帳戶的名稱,而 @sequence\_number 可決定帳戶在設定檔中的使用順序。

  • 若要授與公用存取權,請執行預存程序 sysmail_add_principalprofile_sp (Transact-SQL),如下所示:

    EXECUTE msdb.sysmail_add_principalprofile_sp

    @profile\_name = 'Name of the profile'

    @ principal_name = 'public0'

    @is\_default = 'Default Profile status '

    其中,@profile_name 是設定檔的名稱,@principal_name 指出這是公用設定檔,而 @is\_default 可決定此設定檔是否為資料庫使用者或角色的預設值。

下列範例會建立 Database Mail 帳戶、建立 Database Mail 私人設定檔,然後將帳戶加入設定檔,並授與設定檔的公用存取權。

-- Create a Database Mail account

EXECUTE msdb.dbo.sysmail_add_account_sp
    @account_name = 'AdventureWorks Public Account',
    @description = 'Mail account for use by all database users.',
    @email_address = 'db_users@Adventure-Works.com',
    @replyto_address = 'danw@Adventure-Works.com',
    @display_name = 'AdventureWorks Automated Mailer',
    @mailserver_name = 'smtp.Adventure-Works.com' ;

-- Create a Database Mail profile

EXECUTE msdb.dbo.sysmail_add_profile_sp
    @profile_name = 'AdventureWorks Public Profile',
    @description = 'Profile used for administrative mail.' ;

-- Add the account to the profile

EXECUTE msdb.dbo.sysmail_add_profileaccount_sp
    @profile_name = 'AdventureWorks Public Profile',
    @account_name = 'AdventureWorks Public Account',
    @sequence_number =1 ;

-- Grant access to the profile to all users in the msdb database

EXECUTE msdb.dbo.sysmail_add_principalprofile_sp
    @profile_name = 'AdventureWorks Public Profile',
    @principal_name = 'public',
    @is_default = 1 ;

[回到頁首]