How to send automatic email on daily ,weekly and monthly bases? - c#

How do I send email on daily, weekly and monthly bases using c#.net?
I was thinking of creating a windows service application but I don't know how to do it and if it's the right way to take.
Your thoughts are greatly appreciated.

A windows service is probably the best option.
In your service have a timer that fires with whatever resolution you need (every minute/hour/day etc) and on the timer tick even send your emails.
There are many tutorials for creating windows services with .NET.

A Windows service will do the trick. A good benefit of a Windows service is that it starts up when Windows starts up (or can be set to, anyway). So the machine can be left fairly unattended (as a good server should) and doesn't need anybody logged into it for the service to run. So if that's an important consideration for your scenario then perhaps a Windows service would be the way to go.
If you just want to create a console application instead (which can generally be easier to create/test/debug) then you can schedule it with the native Windows task scheduler. However, unless I'm mistaken, I think a user needs to be logged in to the machine in order for it to run. At least under certain circumstances. So a Windows service is probably your best bet for an unattended task.
As for creating the service, Visual Studio should have a project template for that. The scheduling would be handled with a Timer.

You could also use the Windows Task Scheduler. Quartz.Net seems to be the right tool, too (never used it though).

Windows Service is a good choice.
Remember to save state to disk (file or database) because service restart (for example, due to a reboot) is possible. For example, your next email delivery is a week in the future, you have to save that date to disk. So when your server is down due to a power failure 3 days later and recover after another day, your Windows Service can still set the right timer according to the date saved to disk.

Create a console application, which would be launched from the Windows task scheduler. Creating a windows service is unnecessarily complex, IMO, since you are simply having the program execute in response to time intervals; not other unpredictable external events where you would need a 'listener' type application. KISS!

Related

Process.Start Notepad++ an application which is started by Windows Service [duplicate]

I have done a lot of searching to find a way to start a GUI application from a windows service on Windows 7. Most of what I have found is that with Windows 7 services now run in a separate user session and can not display any graphical interface to the current user. I'm wondering is there is any kind of workaround or different way of accomplishing something like this? Can the service start a process in a different user session?
This change was made for a reason and not simply to annoy developers. The correct approach is to put your UI in a different program and communicate with the session through a pipe, or some other IPC mechanism. The recommendation that services do not present UI is more than 10 years old now.
You should really try to follow these rules, even though it may seem inconvenient to begin with. On the plus side you will enjoy the benefit of keeping your service logic and UI logic separate
If your services runs under the LOCALSYSTEM account then you can check "Allow service to interact with desktop", for the benefit of legacy services that would fail if they could not show UI. But it won't help you anyway because the UI will show in session 0 where it is never seen!
I recommend you take a read of the official Microsoft document describing session 0 isolation.
There is a way to do this.
If you need to show a simple message box you can use the WTSSendMessage Routine.
If you need a complex UI elements you can put it in a separate program and you need to use CreateProcessAsUser Routine.
In this sample provided by microsoft you can see the process.
http://blogs.msdn.com/b/codefx/archive/2010/11/26/all-in-one-windows-service-code-samples.aspx
Windows 7 introduced what is called "Session 0 isolation" that in practice means that every service (except system services) run in a separate non-interactive session. For this reason you cannot directly create a GUI from within the service, except if you run in legacy mode by flagging the Interact With Destop option, which is not good if you plan to run your service for some years in the future.
As David Heffernan said, the best is to use a client-server architecture. WCF makes it easy to communicate with named pipes.
This page is a good starting point to read about Session 0 Isolation and this white paper is also very good.

C# scheduler tools

I’m loath to ask another scheduler question here, I’ve read through dozens, but it’s still not clear to me what tools would best fit my need. I have three requirements for a reporting app:
User invoked
fixed scheduled
user scheduled.
I have an ASP.NET forms app to cover #1 and a C# console app to handle #2 but now #3 has been added to the mix.
So for the user scheduled reports I need to:
Present the user with a schedule selector and save their selection (into SQL Server?)
Have an app that checks the database for jobs to run/schedule
App to run the query and format the report
I suppose the latter two could be a single app but I’ve read it’s hard to debug service apps so keeping them separate may be good. I don’t know what parts of my requirements are met by Quartz.net and I’ve seen separate GUI tools (DayPilot) and backend (Task Manager API, CodePlex taskscheduler) mentioned. Not having used any of these I’m hoping to minimize my false starts.
If you require job scheduler try using hangfire.io
If you have SQL Server, then you should use SQL Server Reporting Services, which does all three.

How to have periodical events in a C# MVC web app?

I have a web app that I'm writing right now that is supposed to have "periodical events". For instance, at midnight, the web app should calculate "scores" for all users. I want this done only once during the day.
Is there a way that I can automate this, so it runs automatically at midnight (or whatever hour I choose)?
I don't like the idea of creating a separate script (VBS) to do this, as the calculation would depend on a lot of business logic of the app. I was thinking to put it into a separate Class Library, so it can use the web app logic (which is also in a class library), but is this the best way to go about it?
I also don't like the idea of using the Session_Start() event in the Global.asax to trigger the event by checking the hour manually. There must be some easier way - especially because down the road I expect there will be a lot more of periodical events - some may have to be triggered every fifteen minutes, for example ...
Thanks a lot for any help you can give me.
You should not do this in the web app itself. You are correct to put the business logic in a separate library. Once you have done this, you can use the business logic from anywhere, and therefore, a good solution would be to create a console application that does the nightly jobs, and invoke the console application from Windows Task Scheduler. IIS is not suitable as a host for periodical events.
I guess you are missing the point of separation of concern. Whatever you are asking is a job of a service. You need to develop a separate application as Windows Service that will do all your calculation and to be triggered by any scheduler even Windows Task Scheduler would do. This is what basically done on large scale applications.
Yeah... again awesome "change your architecture and hosting environment so that my answer can be relevant" responses.
Doing what you ask is actually quite easy, take a look at this article: http://www.codeproject.com/Articles/12117/Simulate-a-Windows-Service-using-ASP-NET-to-run-sc
This is a job for a windows service or scheduled task. A web application responds to HTTP requests. Essentially the service's job would be to wake up, run the appropriate calculations and write back to the database. Once in the database, your web application can use the newly calculated values.
Here is some information on windows services: http://msdn.microsoft.com/en-us/library/d56de412.aspx

MVC .NET Web app that does something every week

I am building a Web application using MVC3 .NET
The app should somehow keep a weekly todo list for each user. On Fridays, all todo lists must be reset.
Can someone help with that.
thanks
You may take a look at Quartz.NET. This being said, the culprit of handling scheduled jobs by a web application is that the web server could unload this ASP.NET application under some circumstances (memory or CPU threshold is hit, the application is not used for a long time, ...). For this reason it is recommended to develop a separate process that will perform those tasks. This process could be hosted for example in a Windows Service which is guaranteed to run all the time.
Another possibility is to simply use the Windows Scheduler in order to launch some custom made executable on a weekly basis which will take care of performing the necessary tasks.
If this is some database-level task, then perhaps a scheduled-task (or crontab on nix). There is no need for MVC to do that. If you really want, the scheduled-task could just hit a route on the web-site.
However, personally I'd simply partition the data with a week-number (or a start date), so that when I get the data, I'm getting the data for the week starting (your date). Then:
there is nothing to do
you have access to history as needed
i.e. add StartDate (or similar) as a column on your existing storage, and use that to select the most timely data. You will get calls about "I need the data from last week" - you might as well built it in from the outset. Besides... if something isn't done on Friday, that doesn't mean it ceases to exist...
do you expect the application to always be up and running? what if nobody is using it on midnight between Thursday and Friday? How can you then execute those jobs?
in general since a web application is not surely running at any given time, you cannot rely on timers inside the web application.
I think best solutions in this case would be to either create a windows service which will be installed on the server and will be always running by definition... a bit like reinventing same things again and again...
or you could also set a scheduled task in windows to call your web application, creating a request to a specific page every Friday, then the web application gets invoked with some parameters and you can do what you need.

What would be the best approach is my application had to pull information from a website every day at 2am

I'm making a small application that is supposed to download info from the web every day at 2am. It will download the information and write the strings to an XML file of my choosing.
Using .NET and C#.
My initial approach was to install a service on the users computer and have that run, but I'm not so sure. I've not even used it so much in the past, only once.
Which is the best (read: time tested :P ) approach to this very common problem.
You can either build your application as a Windows Service, as you mentioned.
Or else it would probably be a better idea to create a normal console application, and launch it automatically at 2.00am with the Windows Task Scheduler.
You can consider both methods as popular and "time-tested".
I would suggest having a console app, which calls data fetching algo in a separate public class (not the main method).
Like Daniel mentioned, run it via Windows Task Scheduler which itself will take care of most scheduling requirements.
This allows the solution to be scaled in the future if need be. E.g. convert into Windows Service, full GUI Winform or even SQL server scheduled tasks etc.

Categories

Resources