如何从SQL Server发送电子邮件?

如何使用T-SQL发送电子邮件,但电子邮件地址是否存储在表中? 我想通过表循环,并能够发送电子邮件。 我找不到这样做的好例子..

谢谢你的帮助

步骤1)创buildconfiguration文件和帐户

您需要使用“configuration数据库邮件向导”创buildconfiguration文件和帐户,可以从pipe理节点中的“数据库邮件”节点的“configuration数据库邮件”上下文菜单中访问该向导。 该向导用于pipe理帐户,configuration文件和数据库邮件全局设置。

第2步)

跑:

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='test@Example.com', @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<=@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 

在下面的链接发布这个http://ms-sql-queries.blogspot.in/2012/12/how-to-send-email-from-sql-server.html

以下是如何将表格中的电子邮件地址连接到单个@recipients参数的示例:

 CREATE TABLE #emailAddresses (email VARCHAR(25)) INSERT #emailAddresses (email) VALUES ('foo@foobar.com') INSERT #emailAddresses (email) VALUES ('bar@foobar.com') INSERT #emailAddresses (email) VALUES ('buzzlightyear@foobar.com') DECLARE @recipients VARCHAR(MAX) SELECT @recipients = COALESCE(@recipients + ';', '') + email FROM #emailAddresses SELECT @recipients DROP TABLE #emailAddresses 

由此产生的@recipients将是:

foo@foobar.com; bar@foobar.com; buzzlightyear@foobar.com

您可以使用数据库邮件从SQL Server本地发送电子邮件。 这是通知系统pipe理员关于错误或其他数据库事件的好工具。 您也可以用它来发送报告或电子邮件给最终用户。 这个基本的语法是:

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

在使用之前,必须使用数据库邮件configuration向导或sp_configure启用数据库邮件。 数据库或Exchangepipe理员可能需要帮助您configuration此。 有关更多信息,请参阅http://msdn.microsoft.com/en-us/library/ms190307.aspx和http://www.codeproject.com/Articles/485124/Configuring-Database-Mail-in-SQL-Server

你也可以用光标来做。 假设您已经创build了一个账户和一个configuration文件,例如“configuration文件”和一个账户,并且您已经有了保存电子邮件的表格,例如“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 

有时候直接找不到sp_send_dbmail。 您可以使用'msdb.dbo.sp_send_dbmail'来尝试(在Windows Server 2008 R2上正常工作,并经过testing)

为了使SQL服务器发送电子邮件通知,您需要从pipe理,数据库邮件创build邮件configuration文件。

1)用户右键点击获取邮件configuration文件菜单,然后selectconfiguration数据库邮件

2)select第一个打开(通过以下任务设置数据库邮件),然后按下一步注意:如果未configurationSMTP,请参阅下面的URL

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

3)在第二个屏幕填写configuration文件名称并添加SMTP帐户,然后按下一步

4)select邮件帐户的types(公共或私人),然后按下一步

5)更改与发送邮件选项相关的参数,然后按下6)按完成

现在让SQL服务器发送一个电子邮件,如果行动X发生,你可以做到这一点通过触发或工作(这是常见的方式不是唯一的)。

1)您可以从SQL服务器代理创buildJob,然后右键单击运营商并查看邮件(例如填写您的电子邮件),然后按确定之后,右键单击作业并select新作业并填写所需的信息以及步骤,姓名等等,并从通知标签中select您所作的个人资料。

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 = 'xxxx@xxxx.com', @body = @results, @subject = @subjectText, @exclude_query_output = 1 --Suppress 'Mail Queued' message GO