
使用SQL Server发送邮件
热 荐
【字体:小 大】
使用SQL Server发送邮件
作者:- 文章来源:- 点击数:1467 更新时间:2006-4-20 
在.NET中,大家知道,可以使用System.Web.Mail来发送邮件。在Framework 1.1下支持验证。
private void Page_Load(object sender, System.EventArgs e)
{
MailMessage mail = new MailMessage();
mail.To = "me@mycompany.com";
mail.From = "you@yourcompany.com";
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'
