开发者

how to send mails automatically using windows service in asp.net mvc

开发者 https://www.devze.com 2023-03-18 17:18 出处:网络
In my project, i have used schedule control. It can be able to fix appointments for single/several users. I have configured of sending mails to the selected users through save /edit appointment action

In my project, i have used schedule control. It can be able to fix appointments for single/several users. I have configured of sending mails to the selected users through save /edit appointment actions.

Now i need to send mails au开发者_运维百科tomatically using windows service for every 30mins to the selected users during appointment time. for e.g. if appointment spans for a user 2pm to 4pm, mail need to be sent for every 30mins bet 2pm to 4pm automatically.

I have installed windows service. But i don't know how to connect my MVC web project with the windows service. I'm using MDF database in my web project. I don't know how to process those datas in window service.

Please suggest me some way. I'm totally new to MVC and Windows service.

Thanks in advance.


The issue outlined in your question sounds like a good use of the Revalee open source project.

Revalee is a service that allows you to schedule web callbacks to your ASP.NET MVC application. Revalee manages task persistence and scheduling using a Windows Service, but leverages your ASP.NET MVC application to handle the processing effort (i.e., "the work"). In your case, your would use your MVC application to send an automated email messages to a user when the MVC application was called back by Revalee.

The following shows an overview of the workflow used by an MVC application with Revalee:

how to send mails automatically using windows service in asp.net mvc


(source: sageanalytic.com)

When an appointment is scheduled, your application would register a callback action with Revalee. This would include a date & time to call the MVC application back as well as the URL to call. Therefore to register a callback with Revalee you might include the following method in your MVC application.

private void ScheduleAppointmentReminderEmail(int appointmentId)
{
    // The DetermineAppointmentReminderTime() method is your private method
    // which returns an appointment's next reminder time as a DateTimeOffset.
    DateTimeOffset callbackTime = DetermineAppointmentReminderTime(appointmentId);

    // The callback should at the task's end time
    Uri callbackUrl = new Uri(
            string.Format(
                    "http://mywebapp.com/Email/SendAppointmentEmail/{0}",
                    appointmentId
                )
        );

    // Register the callback request with the Revalee service
    RevaleeRegistrar.ScheduleCallback(callbackTime, callbackUrl);
}

When your MVC application receives the callback, the SendAppointmentEmail action might look like:

[AllowAnonymous]
[CallbackAction]
public ActionResult SendAppointmentEmail(int appointmentId)
{
    // TODO   1. Validate the appointmentId,
    //        2. Lookup the appointment's information, &
    //        3. Send the email message
    // ...

    return new EmptyResult();
}

The Revalee website has a complete API Reference as well as instructions on how to install and configure the Windows Service. The service is available for easy deployment & intallation at Chocolatey, while MVC client libraries ready for use in Visual Studio are available at NuGet. (There are non-MVC client libraries too.) Naturally, as an open source project, Revalee's complete source code is available on GitHub.

Finally, in case it was not clear above, the Revalee Service is not an external 3rd party online scheduler service, but instead a Windows Service that you install and fully control on your own network. It resides and runs on a Windows server of your own choosing where it can receive callback registration requests from your ASP.NET MVC application.

I hope this helps. Gook luck!

Disclaimer: I was one of the developers involved with the Revalee project. To be clear, however, Revalee is free, open source software. The source code is available on GitHub.


I did´t understand your question very well, however, the connection point between the web application and the windows service is the database. Your web application writes in the database the pending appointments. Then, your windows service checks every 30 minutes, for example, if there is any pending appointment for which it has to send the emails. If it finds out that it has to send some emails, it sends them and marks the entry in the Db as completed (meaning that it has sent the notifications). You can also use Quartz.net as I mentioned in another answer, but a windows service is also a good solution.


You don't have to use the windows service. Everything you described can be solved by using the application built-in HttpModule aka Global.asax .

The high level concept is to keep the HttpApplication running, and within the life time of the application, which is your MVC web application deployed to IIS. This is to prevent the Application_End firing due to inactivity default at 20 minutes.

Once you kept the application running, you can schedule the preferred time interval to talk to database and send emails. This would be the simplest way to do your task.

There is a codeproject sample: http://www.codeproject.com/KB/aspnet/ASPNETService.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消