简体   繁体   中英

How to send automatic email using windows service in asp.net mvc-5 c# app

I am working on an asp.net MVC-5 web project where i have to send daily email to all users. And i know there are 2 ways to achieve this:

  1. Windows service
  2. Sql server agent job.

I have decided to use Windows service . I am working on Express Version of Visual Studio 2013 for Web, so Windows service template is not present in it. After digging a lot i have created a windows service as console application in Visual Studio. Here's the code:

Service

public class FirstService : ServiceBase
{
        public FirstService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            this.EventLog.WriteEntry("FirstService Service Has Started");
        }

        protected override void OnStop()
        {
            this.EventLog.WriteEntry("FirstService Service Has Stopped");
        }

        private void InitializeComponent()
        {
            this.ServiceName = "FirstService";
            this.CanStop = true;
            this.AutoLog = false;
            this.EventLog.Log = "Application";
            this.EventLog.Source = "FirstService";
        }
}

Installer

[RunInstaller(true)]
public class MyServiceInstaller : Installer
{
    private ServiceProcessInstaller FirstServiceProcessInstaller;

    private ServiceInstaller FirstServiceInstaller;

    public MyServiceInstaller()
    {
        InitializeComponent();
    }

    private void InitializeComponent()
    {
        this.FirstServiceProcessInstaller = new ServiceProcessInstaller();
        this.FirstServiceProcessInstaller.Account = ServiceAccount.LocalSystem;
        this.FirstServiceProcessInstaller.Username = null;
        this.FirstServiceProcessInstaller.Password = null;
        this.FirstServiceInstaller = new ServiceInstaller();
        this.FirstServiceInstaller.Description = "FirstService Service Template";
        this.FirstServiceInstaller.DisplayName = "First Service";
        this.FirstServiceInstaller.ServiceName = "FirstService";
        this.FirstServiceInstaller.StartType = ServiceStartMode.Manual;
        this.Installers.AddRange(new Installer[] { this.FirstServiceProcessInstaller, this.FirstServiceInstaller });
    }
}

Main

static class Program
{
    [STAThread]
    static void Main()
    {
        ServiceBase.Run(new FirstService());
    }
}

After this i have successfully installed this service using installutil.exe and can successfully start and stop service from Control Panel -> Administrative Tools -> Serivces . Upto this everything is good.

Now as i mention earlier i want to use windows service in my mvc-5 application for sending automatic email to my application users, i have some questions:

  1. How i will integrate Windows Service in my asp.net mvc-5 application?
  2. How i will deploy the application to the server?
  3. How i will install the windows service on my application hosting server?

Please suggest me any good tutorial which contains the example, if possible. Thanks!

Let me suggest two more options:

  1. Create a console application and use Task Scheduler to invoke it at a given time. This option is really simple.
  2. Use Quartz.net . This option may be valid in case you are using a shared host environment.

I don´t understand what you mean by integrating windows service in your mvc application.

They are two different processes, so you need to implement any sort of communication between processes (eg through database, file, messaging, etc).

  1. You can invoke a controller action using HTTPWebRequest / WebClient class, the controller action will send the mails to users.

2 & 3 : You need to follow the same steps as you did in the local development system.

Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM