• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

sqlserver 搞代码 7年前 (2018-06-16) 210次浏览 已收录 0个评论

说是新方式,其实也是早就用到的技术了,所以放上来!

在.NET中,大家知道,可以使用System.web.Mail来发送邮件。在Framework 1.1下支持验证。

private void Page_Load(object sender, System.EventArgs e)

http://www.gaodaima.com/35174.htmlSQL Server中发送邮件的新方式_sqlserver

{
       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对象。

欢迎大家阅读《SQL Server中发送邮件的新方式_sqlserver,跪求各位点评,若觉得好的话请收藏本文,by 搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:SQL Server中发送邮件的新方式_sqlserver

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址