從SQL Server 自動發送電子郵件
此問題尋求使用Transact-SQL (T-SQL) 從SQL Server 發送電子郵件的解決方案,特別是當收件者電子郵件地址儲存在資料庫表中時。目標是透過循環遍歷表格並向每個地址發送電子郵件來自動化電子郵件發送過程。
解決方案步驟:
1.設定:
透過執行下列指令設定 SQL Server 權限:
sp_CONFIGURE 'show advanced', 1 GO RECONFIGURE GO sp_CONFIGURE 'Database Mail XPs', 1 GO RECONFIGURE GO
2。傳送電子郵件:
要以程式設計方式傳送電子郵件,請使用 sp_send_dbmail預存程序:
sp_send_dbmail @profile_name='yourprofilename', @recipients='[email protected]', @subject='Test message', @body='This is the body of the test message. Congrates Database Mail Received By you Successfully.'
3.循環遍歷表:
要將電子郵件傳送給表中儲存的多位收件人,請使用循環:
DECLARE @email_id NVARCHAR(450), @id BIGINT, @max_id BIGINT, @query NVARCHAR(1000) SELECT @id=MIN(id), @max_id=MAX(id) FROM [email_adresses] WHILE @id<=@max_id BEGIN SELECT @email_id=email_id FROM [email_adresses] set @query='sp_send_dbmail @profile_name=''yourprofilename'', @recipients='''+@email_id+''', @subject=''Test message'', @body=''This is the body of the test message. Congrates Database Mail Received By you Successfully.''' EXEC @query SELECT @id=MIN(id) FROM [email_adresses] where id>@id END
以上是如何使用 T-SQL 自動從 SQL Server 傳送電子郵件?的詳細內容。更多資訊請關注PHP中文網其他相關文章!