日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

编写一个Windows服务程序,定时从数据库中拿出记录发送邮件

發布時間:2025/4/16 windows 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 编写一个Windows服务程序,定时从数据库中拿出记录发送邮件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言:編寫一個Windows服務程序,定時從數據庫中拿出記錄發送郵件。

測試環境:Visual Studio 2005 SP1、Windows Server 2003 SP2

一、新建項目

打開VS2005,新建一個“Windows 服務”項目。

二、添加Timer

展開“工具箱”,在“組件”標簽下找到“Timer”雙擊,這時就添加了一個Timer組件,修改“Name”屬性為“timEmail”、“Enabled”為“false”、“Interval”為“60000”。

接下來要做一些修補工作,不知是VS2005的BUG還是我沒找著地方,在VS2003下是不存在該問題的:剛從“組件”下添加的“Timer”按理說應該來自“System.Timers命名空間”,也只有“System.Timers.Timer”才能在Windows服務程序中正常工作,但是現在這個Timer卻是屬于“System.Windows.Forms.Timer”的。所以得稍作修改,打開“.Designer.cs”文件,修改如下:

#region?組件設計器生成的代碼 //........以上略
///?<summary>?
///?設計器支持所需的方法?-?不要
///?使用代碼編輯器修改此方法的內容。
///?</summary>
private?void?InitializeComponent()
{
????????????
this.components?=?new?System.ComponentModel.Container();
????????????
//this.timEmail?=?new?System.Windows.Forms.Timer(this.components);原
?????????? ?this.timEmail?=?new?System.Timers.Timer();//
???????? ???this.timEmail.Interval?=?60000;
????????????
this.ServiceName?=?"Service1"; }
#endregion
//private?System.Windows.Forms.Timer?timEmail;原
private?System.Timers.Timer?timEmail;//

?三、添加配置文件

服務每次調用配置文件,獲取一些基本參數,這樣一些變更就可直接修改配置文件而不必修改代碼。新建ServiceConfig.xml存放于項目“Bin\Debug\”下:

<?xml?version="1.0"?encoding="utf-8"??>?
<serviceConfig>
????
<serviceItem?
????????
name="sendEmail"?
????????enable
="true"?
????????elapsed
="60000"?
????????connectionString
="your?database?connection..."?
????????smtp
="smtp?address"?
????????account
="your?email?account..."?
????????password
="your?password..."?>
????
</serviceItem>
</serviceConfig>

四、以下是實現代碼

using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Diagnostics;
using?System.ServiceProcess;
using?System.Text;
using?System.Xml;//操作配置文件
using?System.IO;//寫日志
using?System.Threading;//使用線程

namespace?ClientWindowsService
{
????
public?partial?class?ClientService?:?ServiceBase
????{
????????
public?ClientService()
????????{
????????????InitializeComponent();
????????}

????????
protected?override?void?OnStart(string[]?args)
????????{
????????????
//服務啟動
??????this.timEmail.Enabled?=?true;
????????????
this.tSendEmail();
????????}

????????
protected?override?void?OnStop()
????????{
????????????
//服務停止
??????this.timEmail.Enabled?=?false;
????????}

????????
private?void?timEmail_Elapsed(object?sender,?System.Timers.ElapsedEventArgs?e)
????????{
????????????
//定時器
??????this.tSendEmail();
????????}

????????
//開啟新進程發送郵件
????private?void?tSendEmail()
????????{
????????????Thread?t?
=?new?Thread(new?ThreadStart(sendEmail));
????????????t.Start();
????????}

????????
//發送郵件函數
????private?void?sendEmail()
????????{
????????????XmlDocument?doc?
=?new?XmlDocument();
????????????
//添加System.Windows.Forms引用,獲取執行目錄
??????string?configFile?=?System.Windows.Forms.Application.StartupPath.ToString()?+?"\ServiceConfig.xml";
????????????doc.Load(@configFile);
????????????XmlElement?root?
=?doc.DocumentElement;
????????????
foreach?(XmlNode?node?in?root)
????????????{
????????????????
//如果配置文件中開啟服務
????????if?(node.Attributes["name"].Value?==?"sendEmail"?&&?node.Attributes["enable"].Value?==?"true")
????????????????{
????????????????????
try
????????????????????{
????????????????????????
//讀取數據庫,發送郵件操作,略
????????????????????}
????????????????????
catch?(Exception?error)
????????????????????{
????????????????????????
//寫錯誤日志
????????????using?(StreamWriter?sw?=?new?StreamWriter(System.Windows.Forms.Application.StartupPath.ToString()?+?@""?+?DateTime.Now.ToString("yyyy-MM-dd")?+?".txt",?true,?System.Text.Encoding.UTF8))
????????????????????????{
????????????????????????????sw.WriteLine(DateTime.Now.ToString()?
+?"");
????????????????????????????sw.WriteLine(error.ToString());
????????????????????????????sw.WriteLine(
"---------------------------------------------");
????????????????????????????sw.Close();
????????????????????????}
????????????????????}
????????????????}
????????????}
//end?foreach
????????}

????}
//end?class
}//end?namespace

五、布署服務

在設計模式下右鍵-->添加安裝程序-->設置serviceProcessInstaller1的Account為LocalSystem

設置serviceInstaller1的StartType為Automatic

編譯

在命令模式下執行:%systemroot%\microsoft.net\framework\v2.0.50727\installUtil.exe D:\項目目錄\bin\Debug\可執行文件名.exe

轉載于:https://www.cnblogs.com/ori8/archive/2009/05/23/1487541.html

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的编写一个Windows服务程序,定时从数据库中拿出记录发送邮件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。