Mailing list scheduler - iis-7

I have an asp script which I use to send newsletters to different mailing lists. At the moment I click a send button via html to mail everyone in the mailing list. Instead I'd prefer to be able to schedule the sending of a newsletter rather than sending it instantly.
How might I do this on iis7. I am experienced in programming in other languages so I'm open to all suggestions.

One way to do this would be to create a scheduled task on the server that calls your page on a defined schedule.
You can create a VBScript that will request your web page. Then create a scheduled task that calls this script with the URL as a parameter. So if you save the following script as WEBEXEC.VBS then you would call it in your scheduled task as "WEBEXEC.VBS http://www.test.com/mypage.asp".
Option Explicit
Dim objWinHttp, strURL
strURL = WScript.Arguments(0)
Set objWinHttp = CreateObject("MSXML2.ServerXMLHTTP")
objWinHttp.Open "GET", strURL
objWinHttp.Send
Set objWinHttp = Nothing
If you don't have access to create scheduled tasks on the server you could do the same thing on any other computer that is always turned on and connected to the Internet.
Another option is to use ASP.NET which will alow you to run a task in Golbal.asax that can call your URL on a schedule. The problem with this is that if your website is set to shutdown after a certain amount of inactivity then restart when a request comes in (which I think it is the default) then this will only work if you have visits around the clock.
If you need an example of how to do this ASP.NET let me know.

Related

Timer for emailing in asp.net

I have some users in my web site that have a task with a deadline. I want to email them when their deadline is finish. but this action must be perform automatically. Is there any way to make something like that? should i use global in asp, or there is a better way?
You really should write either a scheduled task or a Windows service to perform that sort of action. An ASP.NET website will happily go to sleep if there are no requests, and you really don't want to have long running threads on a web server.
(There is a 'trick' using cache expiration to get scheduled callbacks to your code, try it if you can't use the options below.)
Write an endpoint (URL like /CheckTaskDeadline.ashx) that when called will check for task deadlines and trigger an action (send email). Add a scheduled task that makes a request for the endpoint every 5 minutes, or every hour (whatever granularity you need for reliably triggering close to the deadline). Use curl or wget to 'ping' the URL.
If you don't have access to the machine, sometimes you can create scheduled tasks using your webhost's configuration panel. If you don't, ask them nicely :-). A good hosting service provider should be able to help you. Otherwise, consider moving the application to another host, or get a separate 'ping' service, like Pingdom.

Automatically raise a event in ASP.NET C# to send emails

Is there a way in ASP.NET C# to raise a event on a given time daily to run a procedure and send emails to list of users with their sale report?
In a way, I want to keep a thread active in background in app_start event in global file.
I am on share hosting so don't have much power to update any setting on server as per my needs.
Why don't you build an application that send those emails and run it in a specific time using Windows Tasks Scheduler instead of keeping your application running all the time?
This way, after your application sends the emails and accomplish its task, you could simply end it and start it again whenever you need.
Have a look at this ASP.Net:Best way to run scheduled tasks
Use System.Timers.Timer and do what you need in the callback.

A Way to Run a Long Process From ASP.NET page

What are your most successful ways of running a long process, like 2 hours, in asp.net and return information to the client on the progress.
I've heard creating a windows service, httphandler and remoting can be successful.
Just a suggestion...
If you have logic that you are tyring to utilize already in asp.net... You could make an external app (windows service, console app, etc.) that calls a web service on your asp.net page.
For example, I had a similiar problem where the code I needed was asp.net and I needed to update about 3000 clients using this code. It started timing out, so I exposed the code through a web service. Then, instead of trying to run the whole 3000 clients at through asp.net all at once, I used a console app that is run by a nightly sql server job that ran the web service once for each client. This way all the time consuming processing was handled by the console app that doesn't have the time out issue, but the code we had already wrote in asp.net did not have to be recreated. In the end slighty modifying the design of my existing architecture allowed me easily get around this problem.
It really depends on the environment and constraints you have to deal with...Hope this helps.
There are two ways that I have handled this. First, you can simply run the process and let the client time out. This has two drawbacks: the UI isn't in synch and you are tying up an IIS thread for non-html purposes (I did this for a process that used to return quickly enough but that grew beyond time-out limits).
The better way to handle this is to write a "Service" application that handles the request as passed through a database table (put the details of the request there). Then you can create a window that gives the user a "window" into ongoing progress on the task (e.g. how many records have been processed or emails sent). This status window can either have a link to permit the user to refresh or you can automate the refresh using Ajax callbacks on a timer.
This isn't directly applicable but I wrote code that will let you run processes similar to "scheduled tasks" inside of ASP.NET without needing to use windows services or any type of cron jobs.
Scheduled Tasks in ASP.NET!
I very much prefer WCF service to scheduled tasks. You might (off the top of my head) pass an addr to the WCF service as a sort of 'callback' that the service can call with progress reports as it works.
I'd shy away from scheduled tasks... too course grained.

ASP.NET MVC + SQL Server Application: Best Way to Send Out Event-Driven E-mail Notifications

I have an ASP.NET MVC application that utilizes NHiberante and SQL Server 2008 on the backend. There are requirements for sending out both event-driven notifications on a daily/weekly basis AND general notifications on a weekly basis.
Here is an example of how the event-driven workflow needs to work:
Employee(s) create a number of purchase orders.
An e-mail notification is sent daily to any supervisor with a subordinate employee that has made a purchase order with a list of all purchase orders created by subordinates that require his approval. The supervisor should only get this once (e.g. if Employee A creates a PO, his supervisor should not get an e-mail EVERY DAY until he approves). Also, the list of purchase orders should ONLY include those which the supervisor has NOT taken action against. If no purchase orders need approval by a given supervisor ... they should not get an e-mail.
An e-mail notification is sent daily to Dept. Managers with a list of all purchase orders APPROVED by subordinate supervisors in a similar fashion to #2 above.
Any time any action is taken with regards to approving a PO by a supervisor or dept. manager, the employee should get an e-mail notification daily listing ALL such changes. If there are none for a given employee, they should not get an e-mail at all.
So given such a workflow:
What is the best way to schedule such notifications to happen daily, weekly or even immediately after an event occurs?
How would you ensure that such event-driven notifications ONLY get delivered once?
How would you handle exceptions to ensure that failed attempts to send e-mail are logged and so that an attempt could be made to send the following day?
Thanks!
I would have all your emails, notifications, etc saved to a DB / table first and then have a service that polls for new entries in this database or table that handles the actual sending of the email / notification.
To address your specific situations you could have controllers write to the DB when a email / notification is required and a service that does you interval / event specific checks also write to the DB to create new email. This way your application and service don't really care about how or what is happening to these notifications, they are just saying, "Hey do something." and the emailer/notification service is actually doing the implementation.
The advantage to this is that if your email provider is down you don't lose any emails and you have a history of all emails sent with their details of when, who, etc... You can also rip out or change the emailer to do more, like send to Twitter or a phone text message etc. This effectively decouples your notifications from your application.
All applications I have made recently use this type of model and it has stopped emails from being lost due to service failures and other reasons. It has also made it possible to lookup all emails that have gone through the system and get metrics that allows me to optimize the need to send emails by storing extra information in the email record like reason sent, if it's to report an error, etc... Adding on additions such as routing notifications (eg go to text message instead if email) based on time of day or user has been possible with no changes to the primary applicaton.
Your customer might think all they need is email today but you should make sure your solution is flexible enough to allow for more than just email in the future with just minor tweeks.
You can add a normal action in a controller
Function SendEmails() As ActionResult
Dim result As String = ""
''//big timeout to handle the load
HttpContext.Server.ScriptTimeout = 60 * 10 ''//ten minutes
result = DoTheActualWork()
''//returns text/plain
Return Content(result)
End Function
And then call the page from a scheduled task. Can be a scheduled task on the server or in any machine. Use a .vbs for this:
SendEmails.vbs:
''//Force the script to finish on an error.
On Error Resume Next
''//Declare variables
Dim objRequest
Dim URL
Set objRequest = CreateObject("Microsoft.XMLHTTP")
''//Put together the URL link appending the Variables.
URL = "http://www.mysite.com/system/sendemails"
''//Open the HTTP request and pass the URL to the objRequest object
objRequest.open "POST", URL , false
''//Send the HTML Request
objRequest.Send
''//Set the object to nothing
Set objRequest = Nothing
You can host a windows workflow or windows service. and setup a message queue to process these events. You might just use the database for your message queue or you could use ms message queue or use triggers in the database. But this kind of functionality really shouldn't be a responsibility of your front end web app. If push comes to shove you could spawn off another thread in you asp.net application to process this queue.
This can be done using SQL Server Agent read more about it here:
http://msdn.microsoft.com/en-us/library/ms189237.aspx
Sounds like a job for a service or a scheduled job.
You don't want to do it in ASP.NET because you'd have to configure IIS to keep your app alive all the time, which may not be the best idea.
A scheduled task is fine, but you'll have to program it with all the logic for parsing out your data. Not the best for a separation of concerns. Also you'll have to update two code bases if something changes.
A service isn't ideal as it would only truly be doing something once a day. But you could set up a wcf service and have the website queue up emails using the service.

Is there a way to run a process every day in a .Net web application without writing a windows service or SQL server jobs

We require that in a ASP.Net application, a .Net process should be invoked every day at a specified time automatically. This process needs to interact with the database (SQL Server 2005) and generate billing on a daily basis. We are using a shared hosting hence we are not able to create a windows service or create SQL Server jobs. How can this be achieved without user intervention?
You could try the technique described here, used at StackOverflow itself (or at least it was used here at one point). In a nutshell:
At startup, add an item to the HttpRuntime.Cache with a fixed
expiration.
When cache item expires, do your work, such as WebRequest or what have
you.
Re-add the item to the cache with a fixed expiration.
To get it to run at a specific time instead of an interval, you could lower the interval and simply modify your working method to check the time itself.
As the comments in the original article linked above note, this isn't a perfect solution, and no one should prefer it over a proper scheduling technique if one is available. See When Does Asp.Net Remove Expired Cache Items? for some additional qualifications.
Yes, use Windows Scheduler. Depending on how it's configured you might need to be logged in for the scheduler to run.
You could always schedule a task to run a webservice..
http://weblogs.asp.net/jgalloway/archive/2005/10/24/428303.aspx
The scheduler would run a VBS file with the following..
Set oServerXML = CreateObject("Msxml2.ServerXMLHTTP")
oServerXML.Open "GET","http://my.hostedservice.com/myService.asmx/myService?aParam=Value
oServerXML.setRequestHeader "Content-Type","application/x-www-form-urlencoded"
oServerXML.Send
Set oServerXML = nothing
Can't be done, unfortunately.
IIS only responds to requests, and SQL Server only wakes up for jobs.
The closest you'll be able to do is to put your routine in an ASPX page, not linked from the site and not with an obvious name, and trigger it by a request from some other machine out on the Internet.
The other machine could be a Windows, Linux, Mac, whatever you have available, and all of those platforms have ways of scheduling events (service, cron, etc.) that can make the request to trigger the update on the server.
There are ways to run "services" in .Net by using cache expiration to trigger the task.
More at CodeProject
You can use a Scheduled Task, but this might not work in a shared hosting environment either.
You could setup a webservice or page on your website to kickoff the process, then have a scheduled task on a desktop machine hit that page/service once daily to start the process. Hacky, but it might work.
Being .NET ignorant, I would imagine there's some kind of .NET based scheduler framework available for this (much like Quartz for Java).
Or you could simply fire off a long running thread that spends the bulk of its time sleeping, wake up every minute, check the time, check it's list of "things to do", fire off the ones that need to be done. Level of sophistication being as far as you want to take it, but the primary goal of keeping the primary scheduling thread "alive", "at all costs".
What i can think about now are:
Create a dll which contain the
schedule logic you want, and make
sure that this dll schedule function
will not stop and will loop for ever,
then you will need a page on that
server this page will fire this dll
functions. "you will need to call
this page at least once to start the
scheduler".
Create an application "holds schedule logic" on another machine, may be your Home PC, and make your pc application call the functions on the server through webservices or pages

Resources