在.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對象。