SqlServer 資料庫郵件,sqlserver資料庫

來源:互聯網
上載者:User

SqlServer 資料庫郵件,sqlserver資料庫

SQLserver 內建的郵件發送功能,可以很方便將資料庫中的資訊發送給相關人員。如 可以結合監控事件將警告資訊發給管理員,可以結合作業定時發送報表給相關人員等。


資料庫郵件配置很簡單,以下以圖文配置:


1. 管理——資料庫郵件(右鍵)——設定資料庫郵件


2. 開啟資料庫郵件設定精靈,選擇第一個“通過執行以下人物來安裝資料庫郵件


3. 首先“建立設定檔”,填寫“設定檔名”,再點擊 添加 SMTP賬戶 


4. 建立郵件賬戶

帳戶名稱:資料庫中看到的郵件賬戶名稱

說明:(可選項)

電子郵件地址:資料庫中寄送電子郵件的電子郵件地址。

顯示名稱:此帳戶發送的電子郵件上顯示的名稱。 (可選項)

回覆電子郵件:當收件者回複資料庫中的郵件時,回複將發送到該賬戶中。 (可選項)

伺服器名稱:寄送電子郵件所用的 SMTP 伺服器的名稱或 IP 位址。 通常此格式類似於 smtp.<your_company>.com。

連接埠號碼:此帳戶的 SMTP 伺服器的連接埠號碼。 大多數 SMTP 伺服器使用連接埠 25。

此伺服器要求安全連線(SSL):使用安全通訊端層加密通訊。

使用資料庫引擎服務憑據的Windows 身分識別驗證:使用為 SQL Server 資料庫引擎服務配置的憑據產生指向 SMTP 伺服器的串連(如區域網路內部署有SMTP 伺服器)。

基本驗證:指定 SMTP 伺服器要求的使用者名稱和密碼(使用外部的SMTP 伺服器)。

使用者名稱:登入 SMTP 伺服器所用的使用者名稱。 

密碼:登入 SMTP 伺服器所用的密碼。

確認密碼:再次輸入密碼以進行確認。

匿名驗證:在沒有登入憑據的情況下,將郵件發送至 SMTP 伺服器(如區域網路內部署有SMTP 伺服器)。


5. 選擇“公用設定檔”

公用設定檔對 msdb 資料庫中的DatabaseMailUserRole 資料庫角色的所有成員都可用。它們允許 DatabaseMailUserRole 角色的所有成員使用該設定檔寄送電子郵件。

專用設定檔為 msdb 資料庫中的安全性主體而定義。它們僅允許指定的資料庫使用者、角色和 sysadmin 固定伺服器角色的成員來使用該設定檔寄送電子郵件。


6. “配置系統參數


7. 接下來一步步點擊確定完成!~



8. 現在開始測試!~管理——資料裡郵件(右鍵)——發送測試電子郵件


9. 填寫“收件者”的電子郵件,點擊 “發送測試電子郵件”


10. 測試郵件很快收到了,成功!~O(∩_∩)O~



現在以SQL指令碼配置郵件:

--1.啟用資料庫郵件exec sp_configure 'show advanced options',1reconfigure with overridegoexec sp_configure 'database mail xps',1reconfigure with overridego--2.建立郵件帳戶exec  msdb.dbo.sysmail_add_account_sp@account_name = N'kk_139',--帳戶名稱@description = N'user kk',--說明@email_address = N'*******6411@139.com',--郵件地址@display_name = N'AD08',--顯示名稱@replyto_address = N'*****0961@qq.com',--回覆電子郵件@mailserver_type = N'smtp',--郵件協議@mailserver_name = N'smtp.139.com',--郵件伺服器名稱@port =25,--郵件伺服器連接埠@enable_ssl =0,--是否加密@use_default_credentials =0,--身分識別驗證模式@username = N'*******6411@139.com',--使用者名稱@password = N'************',--密碼@account_id = nullgo--3.建立設定檔exec msdb.dbo.sysmail_add_profile_sp@profile_name = N'DefaultProfile',--新設定檔的名稱@description = N'預設設定檔',--新設定檔的說明(可選)@profile_id = nullgo--4.帳戶與設定檔關聯exec msdb.dbo.sysmail_add_profileaccount_sp    @profile_name = N'DefaultProfile',--設定檔名稱    @account_name = N'kk_139',--帳戶名稱    @sequence_number = 1--順序go--5.發送郵件exec msdb.dbo.sp_send_dbmail@profile_name = N'DefaultProfile',--profile名稱@recipients = N'*****5479@qq.com',--收件者@subject = N'資料庫郵件測試',--郵件標題@body = N'AD08 上的測試電子郵件。'--郵件內容go--刪除設定檔EXEC msdb.dbo.sysmail_delete_profile_sp @profile_name=N'DefaultProfile', @force_delete=True--刪除賬戶EXEC msdb.dbo.sysmail_delete_account_sp @account_name=N'kk_139'--相關查詢exec msdb.dbo.sysmail_help_profile_spexec msdb.dbo.sysmail_help_account_spexec msdb.dbo.sysmail_help_profileaccount_spexec msdb.dbo.sysmail_help_configure_spselect * from msdb.dbo.sysmail_accountselect * from msdb.dbo.sysmail_profile select * from msdb.dbo.sysmail_profileaccount select * from msdb.dbo.sysmail_configurationselect * from msdb.dbo.sysmail_allitemsselect * from msdb.dbo.sysmail_event_log


參考:

資料庫郵件

SQL Server 建立資料庫郵件

配置SQL Server 2012資料庫郵件




相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.