Windows服务阅读电子邮件c#

时间:2017-10-25 17:55:46

标签: c# asp.net windows-services

我正在尝试编写一个Windows服务,每分钟检查一次我的Outlook收件箱。

我有以下代码设置。

    protected override void OnStart(string[] args)
    {
        timeDelay = new System.Timers.Timer(30000);
        timeDelay = new System.Timers.Timer();
        timeDelay.Elapsed += new System.Timers.ElapsedEventHandler(WorkProcess);
        timeDelay.Enabled = true;
    }

    public void WorkProcess(object sender, System.Timers.ElapsedEventArgs e)
    {
        ReadMyEmail();
    }

  private void ReadMyEmail()
    {
        string content;
        Application outlookApplication = null;
        NameSpace outlookNamespace = null;
        MAPIFolder inboxFolder = null;
        Items mailItems = null;

        try
        {
            outlookApplication = new Application(); // m getting the error here...
            outlookNamespace = outlookApplication.GetNamespace("MAPI");
            inboxFolder = outlookNamespace.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
            mailItems = inboxFolder.Items;

            foreach (MailItem item in mailItems)
            {
                if (item.UnRead)
                ......
                ...... all the code for reading emails.

            }
        }
     }

我在尝试调试应用程序时遇到以下错误。

  

由于以下错误,检索CLSID为{00024500-0000-0000-C000-000000000046}的组件的COM类工厂失败:80070005拒绝访问。 (HRESULT异常:0x80070005(E_ACCESSDENIED))

我在这里做错了什么..我只是想尝试使用服务阅读电子邮件。

1 个答案:

答案 0 :(得分:0)

这不应该写成Windows服务。对于“计划”应用程序,您可以将其编写为普通控制台应用程序并设置计划以使用Windows计划程序运行它。您也可以将其设置为以系统的形式运行而不是作为系统运行。