首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在asp.net mvc-5 c#应用程序中使用windows服务发送自动电子邮件

如何在asp.net mvc-5 c#应用程序中使用windows服务发送自动电子邮件
EN

Stack Overflow用户
提问于 2014-01-15 12:49:10
回答 2查看 3.5K关注 0票数 1

我正在工作的一个asp.net MVC-5网络项目,在那里,我必须发送每日电子邮件给所有用户。我知道有两种方法可以做到这一点:

  1. Windows服务
  2. Sql服务器代理作业。

我决定使用Windows服务。我正在开发VisualStudio2013forWeb的Express版本,因此Windows服务模板不在其中。在深入研究之后,我在Visual中创建了一个作为控制台应用程序的windows服务。下面是代码:

服务

代码语言:javascript
复制
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";
        }
}

安装程序

代码语言:javascript
复制
[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 });
    }
}

代码语言:javascript
复制
static class Program
{
    [STAThread]
    static void Main()
    {
        ServiceBase.Run(new FirstService());
    }
}

在此之后,我已经成功地使用installutil.exe安装了此服务,并且可以成功地从控制面板、->管理工具、->系列中启动和停止服务。在此之前,一切都很好。

现在,正如我前面提到的,我想在我的mvc-5应用程序中使用windows服务向我的应用程序用户发送自动电子邮件,我有一些问题:

  1. 我将如何在我的asp.net mvc-5应用程序中集成Windows?
  2. 我将如何将应用程序部署到服务器?
  3. 我将如何在我的应用程序托管服务器上安装windows服务?

如果可能的话,请向我推荐包含此示例的任何好教程。谢谢!

EN

回答 2

Stack Overflow用户

发布于 2014-01-15 13:43:16

让我再提出两种选择:

  1. 创建一个控制台应用程序,并使用任务调度程序在给定的时间调用它。这个选项真的很简单。
  2. 使用Quartz.net。如果您使用的是共享主机环境,则此选项可能有效。

我不明白在mvc应用程序中集成windows服务是什么意思。

它们是两个不同的进程,因此您需要实现进程之间的任何类型的通信(例如,通过数据库、文件、消息传递等)。

票数 1
EN

Stack Overflow用户

发布于 2014-01-15 12:54:12

  1. 您可以使用HTTPWebRequest / WebClient类调用控制器操作,控制器操作将向用户发送邮件。

2&3:您需要遵循与本地开发系统相同的步骤。

希望它能帮上忙

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21137883

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档