SQL Server中發送HTML格式郵件的方法

來源:互聯網
上載者:User

標籤:

SqlSever 配置郵箱的方法就不說了,自行百度搜尋。發送郵件可以非常方便協助營運人員做統計或者發送給客戶定製的訂單等。

 

以下列舉使用sqlserver 發送郵件的3個樣本:

A. 寄送電子郵件

此樣本使用電子郵件地址 [email protected] 向 Dan Wilson 寄送電子郵件。該郵件的主題為 Automated Success Message。郵件內文包含一句話 ‘The stored procedure finished successfully‘。

 
EXEC msdb.dbo.sp_send_dbmail@profile_name = ‘AdventureWorks Administrator‘,@recipients = ‘[email protected]‘,@body = ‘The stored procedure finished successfully.‘,@subject = ‘Automated Success Message‘ ;
B. 發送包含查詢結果的電子郵件

此樣本使用電子郵件地址 [email protected] 向 Dan Wilson 寄送電子郵件。該郵件的主題為 Work Order Count。資料庫郵件將該結果附加為文字檔。

 
EXEC msdb.dbo.sp_send_dbmail@profile_name = ‘AdventureWorks Administrator‘,@recipients = ‘[email protected]‘,@query = ‘SELECT COUNT(*) FROM AdventureWorks.Production.WorkOrderWHERE DueDate > ‘‘2004-04-30‘‘AND  DATEDIFF(dd, ‘‘2004-04-30‘‘, DueDate) < 2‘ ,@subject = ‘Work Order Count‘,@attach_query_result_as_file = 1 ;
C. 發送 HTML 電子郵件此樣本使用電子郵件地址 [email protected] 向 Dan Wilson 寄送電子郵件。郵件的主題為 Work Order List,並包含一個 HTML 文檔,資料庫郵件使用 HTML 格式發送該郵件。DECLARE @tableHTML  NVARCHAR(MAX) ;
SET @tableHTML =
N‘<H1>Work Order Report</H1>‘ +
N‘<table border="1">‘ +
N‘<tr><th>Work Order ID</th><th>Product ID</th>‘ +
N‘<th>Name</th><th>Order Qty</th><th>Due Date</th>‘ +
N‘<th>Expected Revenue</th></tr>‘ +
CAST ( ( SELECT td = wo.WorkOrderID,       ‘‘,
td = p.ProductID, ‘‘,
td = p.Name, ‘‘,
td = wo.OrderQty, ‘‘,
td = wo.DueDate, ‘‘,
td = (p.ListPrice - p.StandardCost) * wo.OrderQty
FROM AdventureWorks.Production.WorkOrder as wo
JOIN AdventureWorks.Production.Product AS p
ON wo.ProductID = p.ProductID
WHERE DueDate > ‘2004-04-30‘
AND DATEDIFF(dd, ‘2004-04-30‘, DueDate) < 2
ORDER BY DueDate ASC,
(p.ListPrice - p.StandardCost) * wo.OrderQty DESCFOR XML PATH(‘tr‘), TYPE) AS NVARCHAR(MAX) ) +N‘</table>‘ ;EXEC msdb.dbo.sp_send_dbmail @recipients=‘[email protected]‘,
@subject = ‘Work Order List‘,
@body = @tableHTML,
@body_format = ‘HTML‘ ;  以上樣本均測試可行。

SQL Server中發送HTML格式郵件的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.