在SQL Server中,不使用“SQL 邮件”的情况下发送邮件
2008-11-11 17:07
495 查看
1、
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
ALTER PROCEDURE usp_SendMail @To varchar(100) ,@Subject varchar(400)=' ', @Body varchar(8000) =' '
AS
Declare @object int
Declare @hr int
EXEC @hr = sp_OACreate 'CDO.Message', @object OUT
EXEC @hr = sp_OASetProperty @object, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendusing").Value','2'
EXEC @hr = sp_OASetProperty @object, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpserver").Value', 'SMTP' --SMTP地址
--下面三条语句是smtp验证,如果服务器需要验证,则必须要这三句,你需要修改用户名和密码
EXEC @hr = sp_OASetProperty @object, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate").Value','1'
EXEC @hr = sp_OASetProperty @object, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendusername").Value','your email address' --你的邮件地址
EXEC @hr = sp_OASetProperty @object, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendpassword").Value','yourpassword' --邮箱密码
EXEC @hr = sp_OAMethod @object, 'Configuration.Fields.Update', null
EXEC @hr = sp_OASetProperty @object, 'To', @To
EXEC @hr = sp_OASetProperty @object, 'Bcc', 'goodspeedwang@yahoo.com.cn'
EXEC @hr = sp_OASetProperty @object, 'From', 'Goodspeed <goodspeed@idg-rbi.com.cn>'
EXEC @hr = sp_OASetProperty @object, 'Subject', @Subject
EXEC @hr = sp_OASetProperty @object, 'BodyFormat', 'MailFormat.Text'
EXEC @hr = sp_OASetProperty @object, 'TextBody', @Body -- Text格式
SET @Body = REPLACE(@Body,CHAR(13),'<br />')
EXEC @hr = sp_OASetProperty @object, 'HtmlBody',@Body --HTML格式的邮件
EXEC @hr = sp_OAMethod @object, 'Send', NULL
--判断出错
IF @hr <> 0
BEGIN
EXEC sp_OAGetErrorInfo @object
RETURN @object
END
PRINT 'success'
EXEC @hr = sp_OADestroy @object
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
更多cdo的信息就访问
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/exchanchor/htms/msexchsvr_cdo_top.asp
SET NOCOUNT ON
DECLARE @enter char(2)
DECLARE @body AS varchar(8000)
DECLARE @sql nvarchar(4000)
SET @sql = ''
/**//* 服务器剩余空间 */
SET @body = '服务器剩余空间'+ CHAR(13)
CREATE TABLE #t(a varchar(100),m int)
INSERT #t EXEC master..xp_fixeddrives
SELECT @body = @body + a + ':',@body = @body + CAST(m AS varchar(500)) + 'M'+CHAR(13)
FROM #t
Truncate table #t
/**//* 数据库空间使用情况 */
SET @body = @body + CHAR(13) + '数据库空间使用情况' + CHAR(13)
SELECT
@sql = @sql + 'SELECT name ,size*8/1024 AS size FROM '+ name + '.dbo.sysfiles' + ' UNION ALL '
FROM master.dbo.sysdatabases WHERE has_dbaccess(name) = 1
SET @sql = LEFT(@sql,LEN(@sql)-10)
INSERT #t EXEC sp_executesql @sql
SELECT @body = @body + RTRIM(a) + ':',@body = @body + CAST(m AS varchar(500)) + 'M' + CHAR(13)
FROM #t
DROP TABLE #t
/**//* 发邮件 */
--SELECT @body
EXEC usp_SendMail 'goodspeed@123.com','服务器健康状态报表',@body
2、
MS SQL不用SQLmail发邮件的存储过程(转帖)
2008年08月21日 星期四 上午 04:17
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
ALTER PROCEDURE usp_SendMail @To varchar(100) ,@Subject varchar(400)=' ', @Body varchar(8000) =' '
AS
Declare @object int
Declare @hr int
EXEC @hr = sp_OACreate 'CDO.Message', @object OUT
EXEC @hr = sp_OASetProperty @object, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendusing").Value','2'
EXEC @hr = sp_OASetProperty @object, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpserver").Value', 'SMTP' --SMTP地址
--下面三条语句是smtp验证,如果服务器需要验证,则必须要这三句,你需要修改用户名和密码
EXEC @hr = sp_OASetProperty @object, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate").Value','1'
EXEC @hr = sp_OASetProperty @object, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendusername").Value','your email address' --你的邮件地址
EXEC @hr = sp_OASetProperty @object, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendpassword").Value','yourpassword' --邮箱密码
EXEC @hr = sp_OAMethod @object, 'Configuration.Fields.Update', null
EXEC @hr = sp_OASetProperty @object, 'To', @To
EXEC @hr = sp_OASetProperty @object, 'Bcc', 'goodspeedwang@yahoo.com.cn'
EXEC @hr = sp_OASetProperty @object, 'From', 'Goodspeed <goodspeed@idg-rbi.com.cn>'
EXEC @hr = sp_OASetProperty @object, 'Subject', @Subject
EXEC @hr = sp_OASetProperty @object, 'BodyFormat', 'MailFormat.Text'
EXEC @hr = sp_OASetProperty @object, 'TextBody', @Body -- Text格式
SET @Body = REPLACE(@Body,CHAR(13),'<br />')
EXEC @hr = sp_OASetProperty @object, 'HtmlBody',@Body --HTML格式的邮件
EXEC @hr = sp_OAMethod @object, 'Send', NULL
--判断出错
IF @hr <> 0
BEGIN
EXEC sp_OAGetErrorInfo @object
RETURN @object
END
PRINT 'success'
EXEC @hr = sp_OADestroy @object
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
更多cdo的信息就访问
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/exchanchor/htms/msexchsvr_cdo_top.asp
SET NOCOUNT ON
DECLARE @enter char(2)
DECLARE @body AS varchar(8000)
DECLARE @sql nvarchar(4000)
SET @sql = ''
/**//* 服务器剩余空间 */
SET @body = '服务器剩余空间'+ CHAR(13)
CREATE TABLE #t(a varchar(100),m int)
INSERT #t EXEC master..xp_fixeddrives
SELECT @body = @body + a + ':',@body = @body + CAST(m AS varchar(500)) + 'M'+CHAR(13)
FROM #t
Truncate table #t
/**//* 数据库空间使用情况 */
SET @body = @body + CHAR(13) + '数据库空间使用情况' + CHAR(13)
SELECT
@sql = @sql + 'SELECT name ,size*8/1024 AS size FROM '+ name + '.dbo.sysfiles' + ' UNION ALL '
FROM master.dbo.sysdatabases WHERE has_dbaccess(name) = 1
SET @sql = LEFT(@sql,LEN(@sql)-10)
INSERT #t EXEC sp_executesql @sql
SELECT @body = @body + RTRIM(a) + ':',@body = @body + CAST(m AS varchar(500)) + 'M' + CHAR(13)
FROM #t
DROP TABLE #t
/**//* 发邮件 */
--SELECT @body
EXEC usp_SendMail 'goodspeed@123.com','服务器健康状态报表',@body
2、
MS SQL不用SQLmail发邮件的存储过程(转帖)
2008年08月21日 星期四 上午 04:17
一般情况下,SQL发邮件的话,要设定SQL mail.系统还要安装outlook.用起来极度不爽. 有时邮件还不能及时的发出,使用这个存储过程,则方便许多 前提是:要有一个SMTP的服务器,请设定存储过程中的地址 OK,Let's GO. CREATE PROCEDURE [dbo].[sp_send_cdosysmail] @From varchar(100) , @To varchar(100) , @Subject varchar(100)=" ", @Body varchar(4000) =" " /********************************************************************* This stored procedure takes the above parameters and sends an e-mail. All of the mail configurations are hard-coded in the stored procedure. Comments are added to the stored procedure where necessary. Reference to the CDOSYS objects are at the following MSDN Web site: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cdosys/html/_cdosys_messaging.asp ***********************************************************************/ AS Declare @iMsg int Declare @hr int Declare @source varchar(255) Declare @description varchar(500) Declare @output varchar(1000) select @Subject=@@ServerName + ' : ' + @Subject select @Body= @Body + ' - - - Message from : ' + @@Servername --************* Create the CDO.Message Object ************************ EXEC @hr = sp_OACreate 'CDO.Message', @iMsg OUT --***************Configuring the Message Object ****************** -- This is to configure a remote SMTP server. -- http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cdosys/html/_cdosys_schema_configuration_sendusing.asp EXEC @hr = sp_OASetProperty @iMsg, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendusing").Value','2' -- This is to configure the Server Name or IP address. -- Replace MailServerName by the name or IP of your SMTP Server. EXEC @hr = sp_OASetProperty @iMsg, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpserver").Value', '192.168.66.2' -- Save the configurations to the message object. EXEC @hr = sp_OAMethod @iMsg, 'Configuration.Fields.Update', null -- Set the e-mail parameters. EXEC @hr = sp_OASetProperty @iMsg, 'To', @To EXEC @hr = sp_OASetProperty @iMsg, 'From', @From EXEC @hr = sp_OASetProperty @iMsg, 'Subject', @Subject -- If you are using HTML e-mail, use 'HTMLBody' instead of 'TextBody'. EXEC @hr = sp_OASetProperty @iMsg, 'TextBody', @Body EXEC @hr = sp_OAMethod @iMsg, 'Send', NULL -- Sample error handling. IF @hr <>0 select @hr BEGIN EXEC @hr = sp_OAGetErrorInfo NULL, @source OUT, @description OUT IF @hr = 0 BEGIN SELECT @output = ' Source: ' + @source PRINT @output SELECT @output = ' Description: ' + @description PRINT @output END ELSE BEGIN PRINT ' sp_OAGetErrorInfo failed.' RETURN END END -- Do some error handling after each step if you need to. -- Clean up the objects created. EXEC @hr = sp_OADestroy @iMsg GO 再补贴一个可以发附件的过程 使用CDOsys组件,不用作任何设置,就可以发邮件(当然,有要支持SMTP的邮件服务器) -- Code to call Procedure declare @MailSubject varchaar(1000), @MailBody varchar(1), @MailSender varchar(100), @MailRecipient varchar(100), @MailAttachment varchar(250) set @MailSubject = 'Spreadsheet' set @MailBody = ' ' set @MailSender = 'SQL_Dude@cox.com' set @MailRecipient = 'was@cox.com' set @MailAttachment = 'N:/WAS_Query.xls' exec master..sp_send_cdosysmail @MailSender, @MailRecipient, @MailSubject, @MailBody, @MailAttachment -- Procedure code CREATE PROCEDURE [dbo].[sp_send_cdosysmail] @From varchar(100), @To varchar(100), @Subject varchar(100) = " ", @Body varchar(4000) = " ", @Attachment varchar(250) = " " /********************************************************************* This stored procedure takes the above parameters and sends an e-mail. All of the mail configurations are hard-coded in the stored procedure. Comments are added to the stored procedure where necessary. Reference to the CDOSYS objects are at the following MSDN Web site: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cdosys/html/_cdosys_messaging.asp ***********************************************************************/ AS Declare @iMsg int Declare @hr int Declare @source varchar(255) Declare @description varchar(500) Declare @output varchar(1000) --************* Create the CDO.Message Object ************************ EXEC @hr = sp_OACreate 'CDO.Message', @iMsg OUT --***************Configuring the Message Object ****************** -- This is to configure a remote SMTP server. -- http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cdosys/html/_cdosys_schema_configuration_sendusing.asp EXEC @hr = sp_OASetProperty @iMsg, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendusing").Value','2' -- This is to configure the Server Name or IP address. -- Replace MailServerName by the name or IP of your SMTP Server. EXEC @hr = sp_OASetProperty @iMsg, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpserver").Value', 'smtpmail.cox.com' -- Save the configurations to the message object. EXEC @hr = sp_OAMethod @iMsg, 'Configuration.Fields.Update', null -- Set the e-mail parameters. EXEC @hr = sp_OASetProperty @iMsg, 'To', @To EXEC @hr = sp_OASetProperty @iMsg, 'From', @From EXEC @hr = sp_OASetProperty @iMsg, 'Subject', @Subject -- If you are using HTML e-mail, use 'HTMLBody' instead of 'TextBody'. EXEC @hr = sp_OASetProperty @iMsg, 'TextBody', @Body if @Attachment <> ' ' EXEC @hr = sp_OAMethod @iMsg, 'AddAttachment', NULL, @Attachment EXEC @hr = sp_OAMethod @iMsg, 'Send', NULL -- Sample error handling. IF @hr <>0 select @hr BEGIN EXEC @hr = sp_OAGetErrorInfo NULL, @source OUT, @description OUT IF @hr = 0 BEGIN SELECT @output = ' Source: ' + @source PRINT @output SELECT @output = ' Description: ' + @description PRINT @output END ELSE BEGIN PRINT ' sp_OAGetErrorInfo failed.' RETURN END END -- Do some error handling after each step if you need to. -- Clean up the objects created. EXEC @hr = sp_OADestroy @iMsg |
相关文章推荐
- 在SQL Server中,不使用“SQL 邮件”的情况下发送邮件
- 使用SQL Server发送邮件时遇到的诡异事件
- 使用SQL Server 发送邮件
- Sql Server 索引使用情况及优化的相关 Sql
- 在SQL Server中,使用CDONTS发送邮件,并让发件人显示姓名而不是单纯的邮件地址
- 使用SQL Server Profiler跟踪“金蝶K3ERP“后台sql语句执行情况
- Sql Server:索引使用情况及优化的相关 Sql
- [SQL]SQL Server 2008 通过配置数据库邮件实现发送邮件功能
- Sql Server 索引使用情况及优化的相关 Sql
- 祥解使用 SQL Server 2005/2008 发送数据库邮件
- SQL Server 使用数据库发送邮件(sp_send_dbmail)
- 使用cmd 的telnet经过smtp发送邮件,同时可用于测试smtp调用情况
- Python以邮件的方式发送由各个区服Redis数据库内存使用情况组成的Zabbix Screen图片
- C#拼接SQL语句,SQL Server 2005+,多行多列大数据量情况下,使用ROW_NUMBER实现的高效分页排序
- 使用邮件发送执行时间久的SQL语句
- 使用邮件发送运行时间久的SQL语句
- 使用SQL Server发送邮件(转贴)
- 如果不使用 SQL Mail,如何在 SQL Server 中发送电子邮件
- sql 邮件发送测试情况
- 如果不使用 SQL Mail,如何在 SQL Server 中发送电子邮件