close

開啟 SSMS 工具中的物件總管展開[管理]結點,開啟 Database Mail 組態精靈,第一次安裝請選擇第一個選項設定Database Mail

image

 

 

由於設定前Database Mail尚未啟用,所以下一步會出現「無法使用Database Mail功能,您要啟用...」的對話窗,請選擇「是」以啟用Database Mail進入下ㄧ步

image

image

 

 

image

image

 

image

最後,重啟SQL Sserver Agent。

因為SQL Server Agent 會快取指定設定檔的資訊,所以只要設定檔有變更就得重啟SQL Server Agent才能生效哦!

image

 

或使用T-SQL語法發送信件:

EXEC msdb.dbo.sp_send_dbmail

@profile_name = 'DBA', --郵件設定檔名稱

@recipients = 'Sandy@localhost.com',

@body = '郵件內容測試',

@subject = '郵件主旨測試' ;

 

其他補充 :

 

--列出一或多個郵件設定檔的相關資訊。
EXECUTE msdb.dbo.sysmail_help_profile_sp
--列出一或多個Database Mail 設定檔的相關帳戶。
EXECUTE msdb.dbo.sysmail_help_profileaccount_sp
--列出Database Mail 帳戶的相關資訊(密碼除外)。
EXECUTE msdb.dbo.sysmail_help_account_sp

image

 


--列出Database Mail設定檔
SELECT * FROM msdb.dbo.sysmail_profile
--列出Database Mail帳戶
SELECT * FROM msdb.dbo.sysmail_account
--列出設定檔對應的帳戶
select * from msdb.dbo.sysmail_profileaccount

image

--列出主要的設定檔
select * from msdb.dbo.sysmail_principalprofile
--Mail Server相關設定
SELECT * FROM msdb.dbo.sysmail_server
SELECT * FROM msdb.dbo.sysmail_servertype

image

 


--列出Database Mail系統參數
SELECT * FROM msdb.dbo.sysmail_configuration

image

 

--查看所有訊息的狀態
SELECT * FROM msdb.dbo.sysmail_allitems ORDER BY send_request_date DESC
--查看傳送成功的訊息
SELECT * FROM msdb.dbo.sysmail_sentitems ORDER BY send_request_date DESC

image

 

--查看未傳送或正在重試狀態的訊息
SELECT * FROM msdb.dbo.sysmail_unsentitems ORDER BY send_request_date DESC
--查看傳送失敗的訊息
SELECT * FROM msdb.dbo.sysmail_faileditems ORDER BY send_request_date DESC

image

 

資料庫伺服器的郵件Log會紀錄再msdb資料庫中且隨時間會增加msdb的使用空間,

但維護計畫沒有內建功能可刪除過期的log紀錄

若有需求,可透過以下指令碼手動刪除,或將它設為排成自動刪除

DECLARE @ DeleteOlder DATETIME SET
@ DeleteOlder = DATEADD (MONTH , - 1 , CURRENT_TIMESTAMP )
EXECUTE msdb.dbo.sysmail_delete_mailitems_sp @sent_before = @DeleteOlder
EXECUTE msdb.dbo.sysmail_delete_log_sp @logged_before = @DeleteOlder

 

參考:http://sqlblog.com/blogs/tibor_karaszi/archive/2014/03/05/do-you-clean-up-your-database-mail-log-tables.aspx

 

 

 

補充:Mail Server 設定

網域名稱增加別名:localhost.com

image

 

若是要傳送到企業內部的mail Server,可設定 SMTP的轉送限制

image

確認SMTP的安全性設定

image

確定SQL Server Enging 的登入身分是否有再上圖的安全性設定中

image

其他參考:建立與組態 Database Mail,使用Transact-SQL為例

實作練習:建立 Database Mail 與 SQL Agent Mail,以 SQL Server 2008 為例
http://sharedderrick.blogspot.com/2011/01/database-mail-sql-agent-mail-sql-server.html
SQL Server 2008 介面區組態(Surface Area Configuration,SAC):啟用 xp_cmdshell
http://sharedderrick.blogspot.com/2009/04/sql-server-2008-surface-area.html
建立 Database Mail 設定檔
http://msdn.microsoft.com/zh-tw/library/ms187605.aspx
建立 Database Mail 帳戶
http://msdn.microsoft.com/zh-tw/library/ms190658.aspx
設定 Database Mail
http://msdn.microsoft.com/zh-tw/library/hh245116.aspx
sysmail_add_account_sp (Transact-SQL)
http://msdn.microsoft.com/zh-tw/library/ms182804.aspx
sysmail_add_profile_sp (Transact-SQL)
http://msdn.microsoft.com/zh-tw/library/ms188058.aspx
Database Mail 檢視 (Transact-SQL)
http://msdn.microsoft.com/zh-tw/library/ms188023
Gmail 外寄郵件 (SMTP) 伺服器 - 需要 TLS2 或安全資料傳輸層 (SSL):
http://support.google.com/mail/bin/answer.py?hl=zh-Hant&answer=13287
How to configure SQL Server Database Mail to send email using your Windows Live Mail Account or your GMail Account
http://blogs.msdn.com/b/suhde/archive/2009/07/12/how-to-configure-sql-server-database-mail-to-send-email-using-your-windows-live-mail-account-or-your-gmail-account.aspx
Setup SQL Server Database Mail to use a Gmail, Hotmail, Yahoo or AOL account
http://www.mssqltips.com/sqlservertip/2578/setup-sql-server-database-mail-to-use-a-gmail-hotmail-yahoo-or-aol-account/

arrow
arrow
    全站熱搜

    小草 發表在 痞客邦 留言(0) 人氣()