SQL作业 想通过SQL作业,定时发送邮件,请问这种应该怎么实现?感谢大家了。。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这个得看sql是什么版本?sql2000 --> 用sql mailsql2005/sql2008 --> 用database mail详细用法见google. SQL Server 并没有内置邮件服务器(Mail Server),它跟我们发送邮件一样,需要用户名和密码通过 SMTP(Simple Message Transfer Protocol)去连接邮件服务器。我们想让 SQL Server 来发送邮件,首先要告诉它用户名称,密码,服务器地址,网络传送协议,邮件服务器的端口等信息。 以下脚本实现了数据库邮件的配置:--下面是具体的配置邮件步骤 --在 sa 系统帐户下运行。 ----1. 启用 SQL Server 2005 邮件功能。 use mastergoexec sp_configure 'show advanced options',1goreconfiguregoexec sp_configure 'Database mail XPs',1goreconfigurego--2. 在 SQL Server 2005 中添加邮件帐户(account) exec msdb..sysmail_add_account_sp @account_name = 'jgj' -- 邮件帐户名称(SQL Server 使用) ,@email_address = [email='[email protected]']'[email protected]'[/email] -- 发件人邮件地址 ,@display_name = null -- 发件人姓名 ,@replyto_address = null ,@description = null ,@mailserver_name = '203.86.70.229' -- 邮件服务器地址 ,@mailserver_type = 'SMTP' -- 邮件协议(SQL 2005 只支持 SMTP) ,@port = 25 -- 邮件服务器端口 ,@username = [email='[email protected]']'[email protected]'[/email] -- 用户名 ,@password = 'xxxxxx' -- 密码 ,@use_default_credentials = 0 ,@enable_ssl = 0 ,@account_id = null--3. 在 SQL Server 2005 中添加 profile exec msdb..sysmail_add_profile_sp @profile_name = 'dba_profile' -- profile 名称 ,@description = 'dba mail profile' -- profile 描述 ,@profile_id = null-- 在 SQL Server 2005 中映射 account 和 profile exec msdb..sysmail_add_profileaccount_sp @profile_name = 'dba_profile' -- profile 名称 ,@account_name = 'jgj' -- account 名称 ,@sequence_number = 1 -- account 在 profile 中顺序 --5. 利用 SQL Server 2005 Database Mail 功能发送邮件。 exec msdb..sp_send_dbmail @profile_name = 'dba_profile' -- profile 名称 ,@recipients = [email='[email protected]']'[email protected]'[/email] -- 收件人邮箱 ,@subject = 'SQL Server 2005 Mail 测试' -- 邮件标题 ,@body = 'Hello Mail!测试' -- 邮件内容 ,@body_format = 'TEXT' -- 邮件格式 --6. 查看邮件发送情况: use msdbgoselect * from sysmail_allitemsselect * from sysmail_mailitemsselect * from sysmail_event_log--如果不是以 sa 帐户发送邮件,则可能会出现错误: ----Msg 229, Level 14, State 5, Procedure sp_send_dbmail, Line 1--EXECUTE permission denied on object 'sp_send_dbmail', database 'msdb', schema 'dbo'.----这是因为,当前 SQL Server 登陆帐户(login),在 msdb 数据库中没有发送数据库邮件的权限,需要加入 msdb 数据库用户,并通过加入 sp_addrolemember 角色赋予权限。假设该SQL Server 登陆帐户名字为 “dba” ----use msdb--go----create user dba for login dba--go----exec dbo.sp_addrolemember @rolename = 'DatabaseMailUserRole',-- @membername = 'dba'--go----此时,再次发送数据库邮件,仍可能有错误: ----Msg 14607, Level 16, State 1, Procedure sp_send_dbmail, Line 119--profile name is not valid----虽然,数据库用户 “dba” 已经在 msdb 中拥有发送邮件的权限了,但这还不够,他还需要有使用 profile:“dba_profile” 的权限。 ----use msdb--go----exec sysmail_add_principalprofile_sp @principal_name = 'dba'-- ,@profile_name = 'dba_profile'-- ,@is_default = 1----从上面的参数 @is_default=1 可以看出,一个数据库用户可以在多个 mail profile 拥有发送权限。 也是通过smtp发的,没有内部邮件服务器的话,电脑应该需要有外网。 小F,我用你给出的代码试了下,报了3个错:1、-------------------------------ExternalMailQueue 接收到无效的 XML 消息格式。conversation_handle: 4F6E1450-D622-E111-A242-00248CFDD5E2。message_type_name: {//www.microsoft.com/databasemail/messages}SendMailStatus。消息正文: <?xml version="1.0" encoding="utf-8"?> <responses:SendMail xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schemas.microsoft.com/databasemail/responses ResponseTypes.xsd" xmlns:responses="http://schemas.microsoft.com/databasemail/responses"> <Information> <Failure Message="由于邮件服务器故障,无法将邮件发送给收件人。 (使用帐户 1 (2011-12-10T10:26:09) 发送邮件。 异常邮件: 无法连接到邮件服务器。 (由于目标机器积极拒绝,无法连接。 127.0.0.1:25)。
)" /> </Information> <MailItemId Id="1" /> <SentStatus Status="3" /> <SentAccountId Id="0" /> <SentDate Date="2011-12-10T10:26:09" /> <CallingProcess Id="2416" /> </responses:SendMail> 。2、-------------------------------------- 1) Exception Information =================== Exception Type: System.Data.SqlClient.SqlException Errors: System.Data.SqlClient.SqlErrorCollection Class: 16 LineNumber: 1 Number: 6602 Procedure: sp_xml_preparedocument Server: VSTAR_NET_QIANY State: 2 Source: .Net SqlClient Data Provider ErrorCode: -2146232060 Message: 错误说明是 '文本内容中发现无效字符。'。 XML 文本 " <Failure Message="" 附近的行号 4 处出现 XML 分析错误 0xc00ce508。 Data: System.Collections.ListDictionaryInternal TargetSite: Void OnError(System.Data.SqlClient.SqlException, Boolean) HelpLink: NULL StackTrace Information =================== 在 System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) 在 System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) 在 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) 在 System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) 在 System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) 在 System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) 在 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) 在 System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) 在 System.Data.SqlClient.SqlCommand.ExecuteNonQuery() 在 Microsoft.SqlServer.Management.SqlIMail.Server.DataAccess.DataAccessAdapter.PostResponse(Response r, Guid convHandle) 在 Microsoft.SqlServer.Management.SqlIMail.IMailProcess.ThreadCallBack.PostResponseIfNeeded(MailInfo mi, Response r) 在 Microsoft.SqlServer.Management.SqlIMail.IMailProcess.ThreadCallBack.MailOperation(Object o)3、----------------------------------------Invalid XML message format received on the ExternalMailQueue. conversation_handle: 4C6E1450-D622-E111-A242-00248CFDD5E2. message_type_name: {//www.microsoft.com/databasemail/messages}SendMailStatus. message body: <?xml version="1.0" encoding="utf-8"?> <responses:SendMail xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schemas.microsoft.com/databasemail/responses ResponseTypes.xsd" xmlns:responses="http://schemas.microsoft.com/databasemail/responses"> <Information> <Failure Message="由于邮件服务器故障,无法将邮件发送给收件人。 (使用帐户 1 (2011-12-10T10:26:09) 发送邮件。 异常邮件: 无法连接到邮件服务器。 (由于目标机器积极拒绝,无法连接。 127.0.0.1:25)。
)" /> </Information> <MailItemId Id="1" /> <SentStatus Status="3" /> <SentAccountId Id="0" /> <SentDate Date="2011-12-10T10:26:09" /> <CallingProcess Id="2416" /> </responses:SendMail> .我想问下这是什么原因? Exec dbo.sp_send_dbmail @profile_name='liuml', @recipients='[email protected]', @subject='TMS数据库备份', @body='数据备份成功!'Go楼主是说dbmail功能吧,sp_send_dbmail有个参数@query --> 查询语句@recipients --> 邮件收件者可将该查询语句的执行结果,发送给指定的邮箱.实际应用还需要配置其他参数.参考 http://msdn.microsoft.com/zh-cn/library/ms190307.aspx配置Database Mail圖文教你怎樣配置Database Mail+JOB(MS_SQL2005以上版本) http://blog.csdn.net/roy_88/article/details/5194601希望对楼主有用! 求教一个简单Sql执行顺序 MS SQL INSERT INTO 语句 一个存储过程引发的错误,急!帮忙看一下 SP中with后面是否可以接IF语句 关于SQL SERVER 2000,备份数据库的问题 求一触发器写法, 有关CONVERT的小问题。 恢复服务器上数据库总是提示扩大物理文件时,MODIFY FILE遇到操作系统错误112,无法恢复。请帮帮忙 我用GPRS移动笔记本访问SQLServer数据库为什么连不上呢?什么原因?上网很快。(急) 什么是“OLAP“谁给我讲讲! 企业管理器无项目 从属表疑问
以下脚本实现了数据库邮件的配置:
--下面是具体的配置邮件步骤
--在 sa 系统帐户下运行。
--
--1. 启用 SQL Server 2005 邮件功能。
use master
go
exec sp_configure 'show advanced options',1
go
reconfigure
go
exec sp_configure 'Database mail XPs',1
go
reconfigure
go
--2. 在 SQL Server 2005 中添加邮件帐户(account)
exec msdb..sysmail_add_account_sp
@account_name = 'jgj' -- 邮件帐户名称(SQL Server 使用)
,@email_address = [email='[email protected]']'[email protected]'[/email] -- 发件人邮件地址
,@display_name = null -- 发件人姓名
,@replyto_address = null
,@description = null
,@mailserver_name = '203.86.70.229' -- 邮件服务器地址
,@mailserver_type = 'SMTP' -- 邮件协议(SQL 2005 只支持 SMTP)
,@port = 25 -- 邮件服务器端口
,@username = [email='[email protected]']'[email protected]'[/email] -- 用户名
,@password = 'xxxxxx' -- 密码
,@use_default_credentials = 0
,@enable_ssl = 0
,@account_id = null
--3. 在 SQL Server 2005 中添加 profile
exec msdb..sysmail_add_profile_sp @profile_name = 'dba_profile' -- profile 名称
,@description = 'dba mail profile' -- profile 描述
,@profile_id = null
-- 在 SQL Server 2005 中映射 account 和 profile
exec msdb..sysmail_add_profileaccount_sp @profile_name = 'dba_profile' -- profile 名称
,@account_name = 'jgj' -- account 名称
,@sequence_number = 1 -- account 在 profile 中顺序
--5. 利用 SQL Server 2005 Database Mail 功能发送邮件。
exec msdb..sp_send_dbmail @profile_name = 'dba_profile' -- profile 名称
,@recipients = [email='[email protected]']'[email protected]'[/email] -- 收件人邮箱
,@subject = 'SQL Server 2005 Mail 测试' -- 邮件标题
,@body = 'Hello Mail!测试' -- 邮件内容
,@body_format = 'TEXT' -- 邮件格式
--6. 查看邮件发送情况:
use msdb
go
select * from sysmail_allitems
select * from sysmail_mailitems
select * from sysmail_event_log
--如果不是以 sa 帐户发送邮件,则可能会出现错误:
--
--Msg 229, Level 14, State 5, Procedure sp_send_dbmail, Line 1
--EXECUTE permission denied on object 'sp_send_dbmail', database 'msdb', schema 'dbo'.
--
--这是因为,当前 SQL Server 登陆帐户(login),在 msdb 数据库中没有发送数据库邮件的权限,需要加入 msdb 数据库用户,并通过加入 sp_addrolemember 角色赋予权限。假设该SQL Server 登陆帐户名字为 “dba”
--
--use msdb
--go
--
--create user dba for login dba
--go
--
--exec dbo.sp_addrolemember @rolename = 'DatabaseMailUserRole',
-- @membername = 'dba'
--go
--
--此时,再次发送数据库邮件,仍可能有错误:
--
--Msg 14607, Level 16, State 1, Procedure sp_send_dbmail, Line 119
--profile name is not valid
--
--虽然,数据库用户 “dba” 已经在 msdb 中拥有发送邮件的权限了,但这还不够,他还需要有使用 profile:“dba_profile” 的权限。
--
--use msdb
--go
--
--exec sysmail_add_principalprofile_sp @principal_name = 'dba'
-- ,@profile_name = 'dba_profile'
-- ,@is_default = 1
--
--从上面的参数 @is_default=1 可以看出,一个数据库用户可以在多个 mail profile 拥有发送权限。
小F,我用你给出的代码试了下,报了3个错:
1、-------------------------------
ExternalMailQueue 接收到无效的 XML 消息格式。conversation_handle: 4F6E1450-D622-E111-A242-00248CFDD5E2。message_type_name: {//www.microsoft.com/databasemail/messages}SendMailStatus。消息正文: <?xml version="1.0" encoding="utf-8"?> <responses:SendMail xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schemas.microsoft.com/databasemail/responses ResponseTypes.xsd" xmlns:responses="http://schemas.microsoft.com/databasemail/responses"> <Information> <Failure Message="由于邮件服务器故障,无法将邮件发送给收件人。 (使用帐户 1 (2011-12-10T10:26:09) 发送邮件。 异常邮件: 无法连接到邮件服务器。 (由于目标机器积极拒绝,无法连接。 127.0.0.1:25)。
)" /> </Information> <MailItemId Id="1" /> <SentStatus Status="3" /> <SentAccountId Id="0" /> <SentDate Date="2011-12-10T10:26:09" /> <CallingProcess Id="2416" /> </responses:SendMail> 。2、--------------------------------------
1) Exception Information =================== Exception Type: System.Data.SqlClient.SqlException Errors: System.Data.SqlClient.SqlErrorCollection Class: 16 LineNumber: 1 Number: 6602 Procedure: sp_xml_preparedocument Server: VSTAR_NET_QIANY State: 2 Source: .Net SqlClient Data Provider ErrorCode: -2146232060 Message: 错误说明是 '文本内容中发现无效字符。'。 XML 文本 " <Failure Message="" 附近的行号 4 处出现 XML 分析错误 0xc00ce508。 Data: System.Collections.ListDictionaryInternal TargetSite: Void OnError(System.Data.SqlClient.SqlException, Boolean) HelpLink: NULL StackTrace Information =================== 在 System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) 在 System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) 在 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) 在 System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) 在 System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) 在 System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) 在 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) 在 System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) 在 System.Data.SqlClient.SqlCommand.ExecuteNonQuery() 在 Microsoft.SqlServer.Management.SqlIMail.Server.DataAccess.DataAccessAdapter.PostResponse(Response r, Guid convHandle) 在 Microsoft.SqlServer.Management.SqlIMail.IMailProcess.ThreadCallBack.PostResponseIfNeeded(MailInfo mi, Response r) 在 Microsoft.SqlServer.Management.SqlIMail.IMailProcess.ThreadCallBack.MailOperation(Object o)3、----------------------------------------
Invalid XML message format received on the ExternalMailQueue. conversation_handle: 4C6E1450-D622-E111-A242-00248CFDD5E2. message_type_name: {//www.microsoft.com/databasemail/messages}SendMailStatus. message body: <?xml version="1.0" encoding="utf-8"?> <responses:SendMail xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schemas.microsoft.com/databasemail/responses ResponseTypes.xsd" xmlns:responses="http://schemas.microsoft.com/databasemail/responses"> <Information> <Failure Message="由于邮件服务器故障,无法将邮件发送给收件人。 (使用帐户 1 (2011-12-10T10:26:09) 发送邮件。 异常邮件: 无法连接到邮件服务器。 (由于目标机器积极拒绝,无法连接。 127.0.0.1:25)。
)" /> </Information> <MailItemId Id="1" /> <SentStatus Status="3" /> <SentAccountId Id="0" /> <SentDate Date="2011-12-10T10:26:09" /> <CallingProcess Id="2416" /> </responses:SendMail> .我想问下这是什么原因?
@recipients='[email protected]',
@subject='TMS数据库备份',
@body='数据备份成功!'
Go楼主是说dbmail功能吧,
sp_send_dbmail有个参数
@query --> 查询语句
@recipients --> 邮件收件者
可将该查询语句的执行结果,发送给指定的邮箱.
实际应用还需要配置其他参数.参考 http://msdn.microsoft.com/zh-cn/library/ms190307.aspx
配置Database Mail
圖文教你怎樣配置Database Mail+JOB(MS_SQL2005以上版本)
http://blog.csdn.net/roy_88/article/details/5194601
希望对楼主有用!