2017-10-25 17 views
-2

毎分Outlookの受信トレイをチェックするWindowsサービスを作成しようとしています。Windowsサービスで電子メールを読むC#

私は次のコード設定をしています。

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. 

      } 
     } 
    } 

アプリケーションのデバッグ中に次のエラーが表示されます。私はここで間違ってやっている何

Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

は..私はちょうどサービスを使用して電子メールを読むためにしようとしています。

+0

@mason updated .. –

+0

@mason updated .. –

+0

「アクセスが拒否されました」は私には明らかです。ほとんどの場合、サービスはローカルユーザーではなくシステムユーザーとして実行されています。それから、アクセス権はありません。 – DonBoitnott

答えて

0

これはWindowsサービスとして記述されたものではありません。 「スケジュールされた」アプリケーションでは、これを通常のコンソールアプリケーションとして記述し、Windowsスケジューラを使用して実行するスケジュールを設定することができます。また、システムとしてではなく、あなたが実行するように設定することもできます。

関連する問題