首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 數據庫知識 >  >> SQL Server 資料庫 >> 關於SqlServer

SQL Server Agent 郵件會話設定完整教學 (Windows 2003 + Outlook 2003)

     編輯:關於SqlServer

目標

利用 SQL Server Agent 郵件會話建立操作員,監控作業完成狀態並發送郵件通知。

測試環境

  • OS:Windows Server 2003 / Windows XP
  • Outlook:2003
  • 測試信箱:yyy@xxx.com (POP3/SMTP)

步驟一:建立 Outlook 郵件設定檔

  1. 控制台 → 「郵件」 → 「顯示設定檔」 → 「新增」 → 命名 mySqlFile。
  2. 選「新增電子郵件帳戶」 → POP3。
  3. 填寫:
    使用者:yyy / yyy@xxx.com
    登入:yyy / 密碼
    收件:pop3.xxx.com / 寄件:smtp.xxx.com
  4. 「更多設定」→「寄件伺服器」→ 勾選「我的寄件伺服器需要驗證」→ 使用相同帳號密碼 → 「記住密碼」。
  5. 「測試帳戶設定」成功 → 完成。
  6. 啟動 Outlook 並收發測試,確認正常。

步驟二:設定 SQL Server Agent 服務登入身分

  1. 「服務」 → SQLSERVERAGENT → 右鍵「內容」→「登入」頁籤。
  2. 選「此帳戶」,填入目前登入 Windows 的帳號密碼 (需為本機管理員)。
    不可選「本機系統帳戶」,否則無法存取 Outlook 設定檔。
  3. 重新啟動 SQL Server Agent 服務。

步驟三:指定 Agent 使用郵件設定檔

  1. 企業管理員 → 伺服器 → 「管理」→「SQL Server Agent」→「郵件會話」。
  2. 「郵件設定檔」下拉選 mySqlFile → 「測試」。
  3. 出現「已用此設定檔成功啟動/停止郵件會話」→ 確定 → 重新啟動 Agent 服務。

步驟四:建立操作員並測試郵件

  1. 企業管理員 → 「操作員」→「新增操作員」。
  2. 「電子郵件名稱」填 aaa@bbb.com → 「測試」。
  3. 收件匣收到測試信即成功。

⚠️ 關鍵注意事項

  • Outlook 必須保持啟動狀態,Agent 才能透過 MAPI 發信。
  • 若 Agent 已佔用設定檔,Outlook 會報「無法開啟預設郵件資料夾」 → 停止 Agent → 開啟 Outlook → 重新啟動 Agent。
  • Windows 2008+/Outlook 2010+ 建議改用 Database Mail (原生 SMTP,無需 Outlook)。

現代替代方案:Database Mail (SQL 2005+)

-- 1. 啟用
EXEC sp_configure 'Database Mail XPs', 1; RECONFIGURE;

-- 2. 建立帳戶與設定檔
EXEC msdb.dbo.sysmail_add_account_sp @account_name='SMTP_Account', @email_address='dba@company.com', @mailserver_name='smtp.company.com', @port=25, @username='user', @password='pwd';
EXEC msdb.dbo.sysmail_add_profile_sp @profile_name='DBMail_Profile';
EXEC msdb.dbo.sysmail_add_profileaccount_sp @profile_name='DBMail_Profile', @account_name='SMTP_Account', @sequence_number=1;

-- 3. Agent 設定使用 Database Mail
EXEC msdb.dbo.sp_set_sqlagent_properties @databasemail_profile='DBMail_Profile';

-- 4. 發送測試
EXEC msdb.dbo.sp_send_dbmail @profile_name='DBMail_Profile', @recipients='test@company.com', @subject='Test', @body='OK';

Database Mail 無需 Outlook、支援 SSL/TLS、效能更好,為現代標準作法。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved