如何用SQL Server來自動傳送郵件

2021-06-18 02:23:43 字數 3934 閱讀 2332

前提: sql server所在的主機必須能上網……

如果有什麼意外發不出去, 請先檢查用 foxmail 客戶端軟體能不能發出去。

1. 準備乙個允許 'smtp'  郵件協議的郵箱, 163或者qq郵箱等都可以。 

下面以qq為例。

2. 在 sql server 中設定:

先在  msdb 庫中開啟 broke 選項:

select d.is_broker_enabled from sys.databases d where d.name='msdb'

--alter database msdb set new_broker with rollback immediate

alter database msdb set enable_broker

--1. 開啟高階配置

exec sp_configure 'show advanced options',1

reconfigure with override

goexec sp_configure 'database mail xps',1

reconfigure with override

go--2. 建立郵件帳戶資訊

declare @account_id int,@account_name nvarchar(128)

set @account_name = 'errormailaccount'

select @account_id = account_id from msdb..sysmail_account where name=@account_name

if (@account_id is not null)

begin

exec msdb..sysmail_delete_account_sp @account_id, @account_name

endexec msdb..sysmail_add_account_sp

@account_name = @account_name, -- 郵件帳戶名稱

@email_address = '***[email protected]', -- 發件人郵件位址

@display_name = '系統管理員', -- 發件人姓名

@replyto_address = null,

@description = null,

@mailserver_name = 'smtp.qq.com', -- 郵件伺服器位址

@mailserver_type = 'smtp', -- 郵件協議

@port = 25, -- 郵件伺服器端口

@username = '***[email protected]', -- 使用者名稱

@password = '???', -- 密碼

@use_default_credentials = 0,

@enable_ssl = 0,

@account_id = null

go--3.資料庫配置檔案

if exists(

select 1

from msdb..sysmail_profile

where name = n'erroremailprofile'

)begin

print '存在,先刪除'

exec msdb..sysmail_delete_profile_sp @profile_name = 'erroremailprofile'

endexec msdb..sysmail_add_profile_sp

@profile_name = 'erroremailprofile', -- profile 名稱

@description = '資料庫郵件配置檔案', -- profile 描述

@profile_id = null

go--4.使用者和郵件配置檔案相關聯

exec msdb..sysmail_add_profileaccount_sp

@profile_name = 'erroremailprofile', -- profile 名稱

@account_name = 'errormailaccount', -- account 名稱

@sequence_number = 1 -- account 在 profile 中順序

--5.傳送文字測試郵件

exec msdb..sp_send_dbmail

@profile_name = 'erroremailprofile',

@recipients = '***[email protected]', --收件人

@subject = 'test title this is test ',

@body = n'z中文郵件內容 中文郵件內容'

go

--檢視賬號

select * from msdb..sysmail_account

--檢視配置檔案

select * from msdb..[sysmail_profile]

--檢視是否已啟動資料庫郵件啟用

exec msdb.dbo.sysmail_help_status_sp;

--啟動資料庫郵件啟用

exec msdb.dbo.sysmail_start_sp;

--檢查郵件佇列的狀態

--如果郵件佇列的狀態不正常,

--請使用 sysmail_stop_sp 嘗試停止佇列,然後再使用 sysmail_start_sp 啟動佇列。

exec msdb.dbo.sysmail_help_queue_sp @queue_type = 'mail';

--檢視最近的郵件傳送情況

select top 5 m.mailitem_id

,m.profile_id

,p.name as profilename

,g.description as [log_desc]--郵件傳送有異常,這裡會有顯示

,m.sent_status

,case sent_status when 1 then 'successed' else 'failed' end as sent_status_desc

,m.subject

,m.body

,m.body_format

,m.send_request_date

from msdb..[sysmail_mailitems] as m

left join msdb..sysmail_profile as p on m.profile_id=p.profile_id

left join msdb..sysmail_log g on m.mailitem_id = g.mailitem_id

order by mailitem_id desc

--清除30天前的郵件和日誌

declare @deletebeforedate datetime

select @deletebeforedate = dateadd(d,-30, getdate())

exec msdb..sysmail_delete_mailitems_sp @sent_before = @deletebeforedate

exec msdb..sysmail_delete_log_sp @logged_before = @deletebeforedate

sql server資料庫郵件清除程式

資料庫郵件故障排除:常規步驟

如何用虛擬機器裝SQL Server

看來之前還是寫的太簡了,來問的同學有點多。所以還是詳細一些吧。裝好virtual box這個軟體,免費的 安裝vbox的過程就不多說了,預設或者指定安裝位置自己決定。注意,不推薦安裝最新版 最新版 5.0.14 最新版可能會遇到遇到一些問題 因此,我建議安裝5.0.10以下的版本。我自己就是有5.0...

BNF正規化 如何用C 實現自動推導

bnf 正規化 attack attack name attack name 字串 signatures signaturesignatures signature ip signature dir get signature opt sigature exp signature proto pro...

如何用 Python 做自動化測試

python 3 環境的部署搭建 selenium 的定位元素操作 對頁面元素的 8 中定位操作 自動化測試框架的設計架構 對框架的構建有屬於自己的思路 docker for jenkins 整合自動化測試 讓測試用例自己執行 本場 chat 可以讓喜歡自動化測試的讀者從入門到掌握企業自動化測試框架...