適用於:SQL Server
Azure SQL 受控執行個體
本文說明如何使用 Transact-SQL 檢查 SQL Server 中使用 Database Mail 傳送的電子郵件訊息狀態。
- Database Mail 會保留傳出電子郵件訊息的複本,並將其顯示在
sysmail_allitems、sysmail_sentitems、sysmail_unsentitems、sysmail_faileditems檢視,以及msdb資料庫中。 - Database Mail 外部程式會記錄活動,並透過 Windows 應用程式事件記錄檔和資料庫中的
sysmail_event_logmsdb檢視來顯示記錄檔。 - 電子郵件訊息狀態可為下列四種之一: 「已傳送」、 「未傳送」、 「正在重試」及 「失敗」。
若要檢查電子郵件訊息的狀態,請對 msdb.dbo.sysmail_event_log 系統檢視執行查詢。
使用 Transact-SQL 檢視使用 Database Mail 傳送的電子郵件狀態
從
sysmail_allitems資料表中選取,並透過mailitem_id或sent_status來指定感興趣的訊息。若要檢查從外部程式傳回的電子郵件訊息狀態,請將
sysmail_allitems與sysmail_event_log依mailitem_id列進行連接。依預設,外部程式不會記錄已成功傳送的訊息之相關資訊。 若要記錄所有訊息,請使用 [Database Mail 組態精靈] 的 [設定系統參數]頁面,將記錄層級設為詳細資訊。
以下範例列出任何外部程式無法順利傳送而傳送至 danw 的電子郵件訊息相關資訊。 此陳述式會列出外部程式無法傳送之訊息的主旨、日期和時間,以及 Database Mail 記錄檔中的錯誤訊息。
USE msdb ;
GO
-- Show the subject, the time that the mail item row was last
-- modified, and the log information.
-- Join sysmail_faileditems to sysmail_event_log
-- on the mailitem_id column.
-- In the WHERE clause list items where danw was in the recipients,
-- copy_recipients, or blind_copy_recipients.
-- These are the items that would have been sent
-- to danw.
SELECT items.subject,
items.last_mod_date
,l.description
FROM dbo.sysmail_faileditems AS items
INNER JOIN dbo.sysmail_event_log AS l
ON items.mailitem_id = l.mailitem_id
WHERE items.recipients LIKE '%danw%'
OR items.copy_recipients LIKE '%danw%'
OR items.blind_copy_recipients LIKE '%danw%';
GO