Windows服務實現自動發送郵件通知

來源:互聯網
上載者:User

簡介

微軟的windows服務,以前以NT服務著稱。它可以使你在自己的系統會話的基礎上,建立一個長時間啟動並執行可執行應用程式。這些服務可以在開機的時候自動啟動,可以中止、重啟,並且不現實任何使用者介面。在你使用伺服器或者你需要在一個機器上長期運行,且不能干擾其他使用者使用該電腦的時候,服務的特性,就使它成為了非常理想的一種技術。
一個Windows服務可以由微軟的VS或者Embarcadero Delphi等開發工具來建立。為了讓程式真正成為windows系統的服務,就需要程式可以被服務控制管理員啟動、停止、中止。服務控制管理員是一個啟動和停止服務的一個windows作業系統的組件。
代碼

     開啟 Visual Studio

     開啟檔案->建立->項目

     選擇Windows 服務

     右鍵Service1.cs 在下拉式功能表中選擇屬性

     在屬性表單中單擊,添加安裝程式:

  •  

  •  在屬性表單中單擊,添加安裝程式:

     

  • 右鍵serviceInstaller1,選擇屬性

  • 改變DisplayName, ServiceName為SeraMailService

  • 啟動類型為自動啟動

     

  • 右鍵serviceProcessInstaller1 ,之後選擇屬性

    更改使用者為LocalSystem

    右鍵Service1.cs設計檔案,選擇查看代碼

     

    在OnStart方法中,輸入下列代碼:

     代碼如下 複製代碼

    public void GetMail(object sender, System.Timers.ElapsedEventArgs args)
            {
                NetworkCredential cred = new NetworkCredential("email@lafarge.com", "Password");
                MailMessage msg = new MailMessage();
                msg.To.Add("email@apsissolutions.com");
                msg.Subject = "Welcome JUBAYER";
     
                msg.Body = "You Have Successfully Entered to Sera's World!!!";
                msg.From = new MailAddress("email@apsissolutions.com"); // Your Email Id
                SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
                SmtpClient client1 = new SmtpClient("smtp.mail.yahoo.com", 465);
                client.Credentials = cred;
                client.EnableSsl = true;
                client.Send(msg);
            }

    為了使這個方法一直運行,我們需要添加時距。
    在onload 方法之前,插入下列代碼:

  •  代碼如下 複製代碼

    System.Timers.Timer createOrderTimer;

    在onStart 方法中,輸入下列代碼:

  •  代碼如下 複製代碼

    createOrderTimer = new System.Timers.Timer();
    createOrderTimer.Elapsed += new System.Timers.ElapsedEventHandler(GetMail);
    createOrderTimer.Interval = 180000;//Set Three minutes intervals
    createOrderTimer.Enabled = true;
    createOrderTimer.AutoReset = true;
    createOrderTimer.Start();

    全部代碼如下:

  •  代碼如下 複製代碼

    System.Timers.Timer createOrderTimer;
     
            public Service1()
            {
                InitializeComponent();
            }
     
            protected override void OnStart(string[] args)
            {
                createOrderTimer = new System.Timers.Timer();
                createOrderTimer.Elapsed += new System.Timers.ElapsedEventHandler(GetMail);
                createOrderTimer.Interval = 500;
                createOrderTimer.Enabled = true;
                createOrderTimer.AutoReset = true;
                createOrderTimer.Start();          
            }
     
            public void GetMail(object sender, System.Timers.ElapsedEventArgs args)
            {
                NetworkCredential cred = new NetworkCredential("email@lafarge.com", "Password");
                MailMessage msg = new MailMessage();
                msg.To.Add("email@apsissolutions.com");
                msg.Subject = "Welcome JUBAYER";
     
                msg.Body = "You Have Successfully Entered to Sera's World!!!";
                msg.From = new MailAddress("email@apsissolutions.com"); // Your Email Id
                SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
                SmtpClient client1 = new SmtpClient("smtp.mail.yahoo.com", 465);
                client.Credentials = cred;
                client.EnableSsl = true;
                client.Send(msg);
            }

    現在用Ctrl+Shift+B編譯該服務。
    在命令列模式下,輸入代碼,來安裝InstallUtil.exe。

  •  代碼如下 複製代碼

    C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/InstallUtil.exe

    安裝服務的指令為:

  •  代碼如下 複製代碼

    C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727>InstallUtil.exe
    "C:/Documents and
    Settings/Administrator/My Documents/Visual Studio 2008/Projects/SeraMailService/
    SeraMailService/bin/Debug/SeraMailService.exe"

    在啟動並執行表單中輸入services.msc命令,來查看作業系統的服務。右鍵所安裝的服務名稱,啟動服務即可。

  •  代碼如下 複製代碼

    C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727>InstallUtil.exe /u
    "C:/Documents and Settings/Administrator/My Documents/Visual Studio 2008/
    Projects/SeraMailService/SeraMailService/bin/Debug/SeraMailService.exe"

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.