2012-11-09 40 views
33

如何使用T-SQL發送電子郵件,但電子郵件地址是否存儲在表中?我想循環瀏覽表格並能夠發送電子郵件。我找不到這樣做,到目前爲止的一個很​​好的例子..如何從SQL Server發送電子郵件?

感謝您的幫助

+0

我檢查了鏈接,但沒有看到關於電子郵件什麼.. – moe

+0

感謝,現在鏈接似乎正在工作 – moe

+3

你不應該使用SQL服務器作爲應用程序服務器。設置電子郵件提醒您是一回事,但將其用作電子郵件分銷商是另一回事。當你**可以**這樣做時,如果你應該**應該這樣做,你可能需要進行合併。 – SQLMason

回答

52

步驟1)創建配置文件和帳戶

您需要創建一個配置文件和帳戶使用配置數據庫郵件嚮導,可通過管理節點中的數據庫郵件節點的配置數據庫郵件上下文菜單進行訪問。該向導用於管理帳戶,配置文件和數據庫郵件全局設置。

步驟2)

RUN:

sp_CONFIGURE 'show advanced', 1 
GO 
RECONFIGURE 
GO 
sp_CONFIGURE 'Database Mail XPs', 1 
GO 
RECONFIGURE 
GO 

步驟3)

USE msdb 
GO 
EXEC 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.' 

要遍歷表

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<[email protected]_id 
BEGIN 
    SELECT @email_id=email_id 
    FROM [email_adresses] 

    set @query='sp_send_dbmail @profile_name=''yourprofilename'', 
         @recipients='''[email protected]_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 

發佈此以下鏈接http://ms-sql-queries.blogspot.in/2012/12/how-to-send-email-from-sql-server.html

+0

感謝Ruzbeh,但我需要從我的表中獲取電子郵件列表。我怎樣才能通過表循環,並從表中獲取每封電子郵件併發送電子郵件?您只顯示的方法使用一個固定的電子郵件。我期待着您的回覆。謝謝 – moe

+1

我編輯了答案..檢查它..在這裏id是你的地址表中的標識列 –

+1

應該@ @ max_id = MIN(id)'是@ @ max_id = MAX(id)'?否則,似乎沒有電子郵件將被髮送。 – Jimmy

3

這裏的是一個如何從表中串連電子郵件地址到一個單一的@recipients參數爲例:

CREATE TABLE #emailAddresses (email VARCHAR(25)) 

INSERT #emailAddresses (email) VALUES ('[email protected]') 
INSERT #emailAddresses (email) VALUES ('[email protected]') 
INSERT #emailAddresses (email) VALUES ('[email protected]') 

DECLARE @recipients VARCHAR(MAX) 
SELECT @recipients = COALESCE(@recipients + ';', '') + email 
FROM #emailAddresses 

SELECT @recipients 

DROP TABLE #emailAddresses 

產生的@recipients將是:

[email protected]; [email protected]; [email protected]

1

有時而沒有找到sp_send_dbma直接。您可以使用'msdb.dbo.sp_send_dbmail'來嘗試 (在Windows Server 2008 R2上正常工作並且經過測試)

1

爲了使SQL Server發送電子郵件通知,您需要從管理,數據庫郵件。

1)用戶單擊鼠標右鍵獲得郵件配置文件菜單,並選擇配置數據庫郵件

2)按照下面的任務選擇首開(建立一個數據庫,郵件),按next 注:如果SMTP沒有配置,請在第二個屏幕是指在下面的網址

http://www.symantec.com/business/support/index?page=content&id=TECH86263

3)填寫個人資料的名稱,並添加SMTP賬戶,然後按未來

4)選擇郵件帳戶(公共或私營),然後按下一

5)改變相關的發送郵件選項的參數,類型和按next 6)按完成

我們使SQL服務器發送一封電子郵件,如果行動X發生,你可以通過觸發器或工作(這是常見的方式不是唯一的)。

1)您可以創建SQL Server代理作業,然後右鍵單擊操作員和檢查郵件(填充例如電子郵件),然後按確定這一權利之後單擊作業,然後選擇新的工作 並填寫所需的信息作爲以及來自步驟,名稱等等和來自通知選項卡的選擇您所做的配置文件。

2)觸發器請參考下面的例子。

AS 
declare @results varchar(max) 
declare @subjectText varchar(max) 
declare @databaseName VARCHAR(255) 
SET @subjectText = 'your subject' 
SET @results = 'your results' 
-- write the Trigger JOB 
EXEC msdb.dbo.sp_send_dbmail 
@profile_name = 'SQLAlerts', 
@recipients = '[email protected]', 
@body = @results, 
@subject = @subjectText, 
@exclude_query_output = 1 --Suppress 'Mail Queued' message 
GO 
3

您可以使用數據庫郵件從SQL Server本地發送電子郵件。這是通知系統管理員關於錯誤或其他數據庫事件的好工具。您也可以使用它將報告或電子郵件發送給最終用戶。 這種情況的基本語法是:

EXEC msdb.dbo.sp_send_dbmail 
@recipients='[email protected]', 
@subject='Testing Email from SQL Server', 
@body='<p>It Worked!</p><p>Email sent successfully</p>', 
@body_format='HTML', 
@from_address='Sender Name <[email protected]>', 
@reply_to='[email protected]' 

使用之前,數據庫郵件必須使用數據庫郵件配置嚮導或sp_configure的啓用。數據庫或Exchange管理員可能需要幫助您配置此。 請參閱 http://msdn.microsoft.com/en-us/library/ms190307.aspxhttp://www.codeproject.com/Articles/485124/Configuring-Database-Mail-in-SQL-Server瞭解更多信息。

2

你也可以用光標來做到這一點。 假設您已創建帳戶和配置文件,例如「個人資料」和一個帳戶,並且您擁有可以保存電子郵件的表格,例如「EmailMessageTable」你可以做到以下幾點:

USE database_name 
GO 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 

CREATE PROCEDURE mass_email AS 
declare @email nvarchar (50) 
declare @body nvarchar (255) 

declare test_cur cursor for    
SELECT email from [dbo].[EmailMessageTable] 

open test_cur           

fetch next from test_cur into 
@email  
while @@fetch_status = 0  
begin          

set @body = (SELECT body from [dbo].[EmailMessageTable] where email = @email) 
EXEC msdb.dbo.sp_send_dbmail 
    @profile_name = 'profile', 
    @recipients = @email, 
    @body = @body, 
    @subject = 'Credentials for Web'; 
fetch next from test_cur into 
@email 
end  
close test_cur 
deallocate test_cur 

後,所有你需要做的就是執行存儲過程

EXECUTE mass_email 
GO