当前位置: 首页 > 图文教程 > 数据库 > MSSQL > SQL Server中发送邮件的新方式

MSSQL
SQL Server SA权限总结经典技术
ASP数据库编程SQL常用技巧
SQL SERVER数据库开发之存储过程应用
SQL Server 2000的安全配置
MSSQL经典语句
SQL 经典语句
有用的SQL语句(删除重复记录,收缩日志)
Access 数据类型与 MS SQL 数据类型的相应
SQL语句示例
SQL数据类型详解
将Sql Server对象的当前拥有者更改成目标拥有者
MSSQL内外连接(INNER JOIN)语句详解
SQL 外链接操作小结 inner join left join right join
SQL Server中网络备份一例
SQL语句导入导出大全
SQL 新增/修改 表字段列的类型等
系统存储过程,sp_executesql
sql2005开启xp_cmdshell
实例学习SQL的Select命令
删除数据库中重复数据的几个方法

MSSQL 中的 SQL Server中发送邮件的新方式


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-30   浏览: 35 ::
收藏到网摘: n/a

 在.NET中,大家知道,可以使用System.Web.Mail来发送邮件。在Framework 1.1下支持验证。private void Page_Load(object sender, System.EventArgs e) 
  {
  MailMessage mail = new MailMessage();
  mail.To = "[email protected]";
  mail.From = "[email protected]";
  mail.Subject = "this is a test email.";
  mail.Body = "Some text goes here";
  mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication
  mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "my_username_here"); //set your username here
  mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "super_secret"); //set your password here
  SmtpMail.SmtpServer = "mail.mycompany.com"; //your real server goes here
  SmtpMail.Send( mail );
  }
  以前我曾写过在.NET下发送邮件的方法,详见:
  http://dev.csdn.net/develop/article/17/17189.shtm
  SQL Server中,我们一般使用SQL本身的邮件发送方式,但需要配置Exchage Server、Outlook等,也是一个比较繁琐的事情。很多人抱怨说配置不成功。
  其实,我们可以在 SQL Server中创建 OLE 对象实例,调用IIS SMTP自带的发送组件来实现邮件发送。
  我们建立这个存储过程,你需要修改的地方是,SmtpServer的名字
  Create PROCEDURE sys_sendmail @From varchar(100) , @To varchar(100) , @Bcc varchar(500), @Subject varchar(400)=" ", @Body ntext =" "
  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.163.com’
  --下面三条语句是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’,’lihonggen0’
  EXEC @hr = sp_OASetProperty @object, ’Configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendpassword").Value’,’xxx’
  EXEC @hr = sp_OAMethod @object, ’Configuration.Fields.Update’, null
  EXEC @hr = sp_OASetProperty @object, ’To’, @To
  EXEC @hr = sp_OASetProperty @object, ’Bcc’, @Bcc
  EXEC @hr = sp_OASetProperty @object, ’From’, @From
  EXEC @hr = sp_OASetProperty @object, ’Subject’, @Subject
  EXEC @hr = sp_OASetProperty @object, ’TextBody’, @Body
  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 注意:必须确保安装Smtp,可以访问CDO对象。