本文來(lái)源于網(wǎng)頁(yè)設(shè)計(jì)愛(ài)好者web開(kāi)發(fā)社區(qū)http://www.html.org.cn收集整理,歡迎訪問(wèn)。
說(shuō)是新方式,其實(shí)也是早就用到的技術(shù)了,所以放上來(lái)!
在.net中,大家知道,可以使用system.web.mail來(lái)發(fā)送郵件。在framework 1.1下支持驗(yàn)證。
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 );
}
以前我曾寫(xiě)過(guò)在.net下發(fā)送郵件的方法,詳見(jiàn):
http://dev.csdn.net/develop/article/17/17189.shtm
 
sql server中,我們一般使用sql本身的郵件發(fā)送方式,但需要配置exchage server、outlook等,也是一個(gè)比較繁瑣的事情。很多人抱怨說(shuō)配置不成功。
其實(shí),我們可以在 sql server中創(chuàng)建 ole 對(duì)象實(shí)例,調(diào)用iis smtp自帶的發(fā)送組件來(lái)實(shí)現(xiàn)郵件發(fā)送。
我們建立這個(gè)存儲(chǔ)過(guò)程,你需要修改的地方是,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' 
--下面三條語(yǔ)句是smtp驗(yàn)證,如果服務(wù)器需要驗(yàn)證,則必須要這三句,你需要修改用戶名和密碼
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
--判斷出錯(cuò)
if @hr <> 0
begin
   exec sp_oageterrorinfo @object   
   return @object
end
print 'success'
exec @hr = sp_oadestroy @object
go 
注意:必須確保安裝smtp,可以訪問(wèn)cdo對(duì)象。