2017年9月1日 星期五

[ASP]使用CDONT寄送email

最近羊男需要使用古老的技術ASP寫個客戶留言版,此需求要將留言內容email寄送至員工信箱
於是我翻開十年前的老書上看到有個方法要在windows server 2000上安裝SMTP虛擬伺服器,
這是一種類似SMTP代理伺服器的服務,此服務也提供email relay的功能,不過既然己經有exchange server可以寄信了,何必多此一舉,於是使用連至Exchage Server來寄信,程式碼如下:

<%
Set Email = Server.CreateObject("CDO.Message"); 'CDO.Message 是 Windows 2000 內建傳送email 元件,它許多功能(html格式、透過exchange server寄信、可設定編碼)
Email.From ="aa@hotmail.com" '寄件者
Email.To ="xx@hotmail.com" '收件者
Email.Subject ="XX實業來信" '主旨
Email.HtmlBody ="<html><body>simple content</body></html>" 
Email.BodyPart.Charset = "big5"
Set Conf = CreateObject("CDO.Configuration") 
Set Flds = Conf.Fields 
With Flds 
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 '使用網路上傳送訊息
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "127.0.0.1"  'Exchange Server IP
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 '使用的port
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "testacount" '帳號
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password" '密碼
.Update '整批更新剛才的設定
End With
Email.Configuratoin = Conf
Email.Send '寄送email
Set Email = nothing '清除此物件
Set Conf = nothing
%>

如有問題,歡迎留言指教~

參考資料:

[筆記]簡易Log method

程式中經常會遇到許多需要紀錄log的部份,有些是透過套件(如:Log4Net)來實作,小弟介紹的是簡易log method,其程式碼如下:
#region 寫入Log
/// 
///message:紀錄文字
///
public void LogWrite(string message)
{   
    string FilePath = ConfigurationSettings.AppSettings["LogDir"].ToString();    
     //從config檔抓取LogDir參數的值 
    string fileName = FilePath + string.Format("\\{0:yyyy}\\{0:MM}\\{0:yyyy-MM-dd}.txt", DateTime.Now);
    //設定檔案名稱,格式為西元年-月-日.txt  
    FileInfo fileInfo = new FileInfo(fileName);   
    if (fileInfo.Directory.Exists == false)
    {
        fileInfo.Directory.Create();
        //若無此檔案則新增
    }
    string writeString = string.Format("{0:yyyy/MM/dd HH:mm:ss.fff} {1}",DateTime.Now, message) + Environment.NewLine;
    File.AppendAllText(fileName, writeString, Encoding.Unicode);
    
}
#endregion
此method會依年、月建立目錄並產生每日log文字檔,如下圖:

以上介紹,各位如有任何意見,歡迎留言指教!

[SQL Server]主動通知SQL Server發生錯誤的機制

當SQL Server有特定錯誤發生時,管理人員也沒有時間經常去查看SQL Server錯誤檔, 於是有時特定錯誤可能很重要時,但卻沒有人被通知到,現在小弟實作一個主動通知的機制。 首先先新增警示,在這裡選擇想要被通知的錯誤訊息的等級或其它設定 接著在回應頁籤中,勾選執...