国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > SQL Server > 正文

使用SQL Server發送郵件

2024-08-31 00:47:58
字體:
來源:轉載
供稿:網友

在.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對象。
(摘自李洪根)
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 封丘县| 信宜市| 望谟县| 太仆寺旗| 大宁县| 卢氏县| 乌什县| 通道| 神木县| 南城县| 济南市| 舒城县| 方正县| 华亭县| 东方市| 淮南市| 阿克苏市| 白沙| 舞阳县| 鹤壁市| 雷州市| 华阴市| 交口县| 凭祥市| 灌阳县| 贵州省| 定边县| 巴青县| 屏山县| 晋江市| 泰来县| 榕江县| 淳安县| 崇阳县| 怀安县| 福安市| 夏邑县| 油尖旺区| 黑龙江省| 襄樊市| 抚远县|