Create Windows Service and Send Mail Daily At Fixed Time Using C#Introduction. In this article I am explaining how to create a Windows Service to schedule daily mail at a specified time. Scheduling email and sending the email is a basic requirement of any project. Step 1. Open Visual Studio and create a new project. Under Windows Desktop select Windows Service and provide a proper name and click on the OK button. Step 2. Rename the service. In this case I am using mail service. Click on “Click here to switch to code view”. Step 3. Add some app settings in the app. Settings> < add key="Start. Time" value="0. 8: 3. PM "/> < add key="call. Sbcl This manual is part of the SBCL software system. See the README file for more information. This manual is largely derived from the manual for the CMUCL system. Duration" value="2"/> < add key="Call. Type" value="1"/> < add key="From. Mail" value="****"/> < add key="Password" value="****"/> < add key="Host" value="smtpout. Settings> Step 4. In the Mail. Service class write the following function.
Scheduler() { Initialize. Component(); int str. Time = Convert. To. Int. 32(Configuration. Manager. App. Settings["call. Duration"]); get. Call. Type = Convert. To. Int. 32(Configuration. Manager. App. Settings["Call. Type"]); if (get. Call. Type == 1) { timer. System. Timers. Timer(); double inter = (double)Get. Next. Interval(); timer. Interval = inter; timer. Elapsed += new Elapsed. Event. Handler(Service. Timer_Tick); } else { timer. System. Timers. Timer(); timer. Interval = str. Time * 1. Elapsed += new Elapsed. Event. Handler(Service. Timer_Tick); } } Note: For using the Configuration. Manager add the reference first and then use the class. Get. Next. Interval() { time. String = Configuration. Manager. App. Settings["Start. Time"]; Date. Time t = Date. Time. Parse(time. String); Time. Span ts = new Time. Span(); int x; ts = t - System. Date. Time. Now; if (ts. Total. Milliseconds < 0) { ts = t. Add. Days(1) - System. Date. Time. Now; } return ts. Total. Milliseconds; } private void Set. Timer() { try { double inter = (double)Get.Next. Interval(); timer.Interval = inter; timer.Start(); } catch (Exception ex) { } } In the timer On.Start() function first write a message to the log that the service has been started and when the service stops write to the log that the service has stopped.On. Start(string[] args) { timer.Auto. Reset = true; timer. Axis Camera Station Crack Serial Key . Enabled = true; Service. Log. Write. Error. Log("Daily Reporting service started"); } protected override void On. Stop() { timer. Auto. Reset = false; timer. Enabled = false; Service. Log. Write. Error. Log("Daily Reporting service stopped"); } Here I am creating a class Service. Log that contains the followings function. Write. Error. Log(Exception ex) { Stream. Writer sw = null; try { sw = new Stream. Writer(App. Domain. Current. Domain. Base. Directory + "\\Log. File. txt", true); sw. Write. Line(Date. Time. Now. To. String() + ": " + ex. Source. To. String(). Trim() + "; " + ex. Message. To. String(). Trim()); sw. Flush(); sw. Close(); } catch { } } public static void Write. Error. Log(string Message) { Stream. Writer sw = null; try { sw = new Stream. Writer(App. Domain. Current. Domain. Base. Directory + "\\Log. File. txt", true); sw. Write. Line(Date. Time. Now. To. String() + ": " + Message); sw. Flush(); sw. Close(); } catch { } } This class also contains a function that will send email to the mail id passed by its arguments. Send Email Code Function public static void Send. Email(String To. Email, string cc, string bcc, String Subj, string Message) { string Host. Add = Configuration. Manager. App. Settings["Host"]. To. String(); string From. Emailid = Configuration. Manager. App. Settings["From. Mail"]. To. String(); string Pass = Configuration. Manager. App. Settings["Password"]. To. String(); Mail. Message mail. Message = new Mail. Message(); mail. Message. From = new Mail. Address(From. Emailid); mail. Message. Subject = Subj; mail. Message. Body = Message; mail. Message. Is. Body. Html = true; string[] To. Muli. Id = To. Email. Split(','); foreach (string To. EMail. Id in To. Muli. Id) { mail. Message. To. Add(new Mail. Address(To. EMail. Id)); } string[] CCId = cc. Split(','); foreach (string CCEmail in CCId) { mail. Message. CC. Add(new Mail. Address(CCEmail)); } string[] bccid = bcc.Split(','); foreach (string bcc.Email. Id in bccid) { mail.Message. Bcc. Add(new Mail. . Address(bcc. Email. Id)); } Smtp. Client smtp = new Smtp. Client(); smtp. Host = Host. Add; smtp.Enable. Ssl = false; Network.Credential Network. The Fall Last Days Of Gaia Gameplay more. Cred = new Network.Credential(); Network. Cred. User. Name = mail. Message. From. Address; Network. Cred. Password = Pass; smtp. Use. Default. Credentials = true; smtp. Credentials = Network. Cred; smtp. Port = 3. 53. 5; smtp. Send(mail. Message); } #endregion Step 5. Now inside the Mail. Service class write a Service. Timer_Tick() function. This function is mainly used for the task done by this Window Service when the timer tic reaches the time specified in the app. You can change your code based on your requirements. Service. Timer_Tick(object sender, System. Timers. Elapsed. Event. Args e) { string Msg = "Hi ! This is Daily. Mail. Scheduler. Service mail."; Service. Log. Send. Email("manishki. Daily Report of Daily. Mail. Scheduler. Service on " + Date. Time. Now. To. String("dd- MMM- yyyy"), Msg); if (get. Call. Type == 1) { timer. Stop(); System. Threading. Thread. Sleep(1. 00. 00. 00); Set. Timer(); } } Step 6. Now your Windows Service is ready. Compile this and use the following procedure to install and use this Windows Service. Install Windows Service. Go to "Start" > > "All Programs" > > "Microsoft Visual Studio 2. Visual Studio Tools" . Click "Developer Command Prompt for VS2. Type the following command: cd < physical location of your Daily. Mail. Scheduler. Service. In my case it is: cd D: \Dot Net Program\Daily. Mail. Scheduler. Service\Daily. Mail. Scheduler. Service\bin\Debug. Type the following command: Install. Util. exe “Daily. Mail. Scheduler. Service. And press Enter. Now go to Services and find the services of your project name and start that service. You will then start finding the mail every day at 0. PM. Debug Window Service. Build the Solution. Start your Service. While your Service is starting, go to - -> Debug - -> Attach Process. Make sure "Show Process from all users" and "Show Process in all sessions" are checked. Find your "" in the list and click "Attach". You should now be at the breakpoint you inserted. Summary. In this illustration we learned about Windows Service and sending mail daily by using that service. This may be helpful somehow when scheduling something based on your requirements. Please provide your valuable comments about this article.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
November 2017
Categories |