0

I have several project which requires me to connect an email account and retrieve data from email arrives.

When running locally on my PC, everything works fine.

I have Teamcity (latest version) installed on a Windows 2008 server.

This is my code where it seems it fails:

  public void DeleteAllEmails(string Folder)
    {
        Application myApp = new ApplicationClass();
        NameSpace mapiNameSpace = myApp.GetNamespace("MAPI");
        MAPIFolder myInbox = mapiNameSpace.GetDefaultFolder(OlDefaultFolders.olFolderInbox).Folders[Folder];

        List<MailItem> ReceivedEmails = new List<MailItem>();

        foreach (MailItem mail in myInbox.Items)
        { ReceivedEmails.Add(mail); }

        foreach (MailItem mail in ReceivedEmails)
        {
            mail.Delete();
        }
    }

When running my tests from TeamCity, I get this error:

[ReportAppeal.dll] ReportAppeal.MainTestRunner.Create2ApplicationsTryOneWithcorrectReportIdSecondwithUncorrectReportId("PostOffice") (2m:06s) [13:56:35][ReportAppeal.MainTestRunner.Create2ApplicationsTryOneWithcorrectReportIdSecondwithUncorrectReportId("PostOffice")] System.Runtime.InteropServices.COMException : Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80080005 Server execution failed (Exception from HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE)).

Or

[ReportAppeal.MainTestRunner.CheckStatusFor2ApplicationsOfTheSameCarNumber("PostOffice")] at ReportAppeal.Email.DeleteAllEmails(String Folder) in C:\BuildAgent\work\430330697c233f87\Projects\ReportAppeal\ReportAppeal\Email.cs:line 50 at ReportAppeal.MainTestRunner.CheckStatusFor2ApplicationsOfTheSameCarNumber(String Address) in C:\BuildAgent\work\430330697c233f87\Projects\ReportAppeal\ReportAppeal\MainTestRunner.cs:line 427 [13:50:09][ReportAppeal.dll] ReportAppeal.MainTestRunner.Create2ApplicationsButFinishProcessJustAfterOne("Email") (2m:07s)

I have Outlook 2013 installed on my server so the environments are pretty similar. What could I do in order to make it work?

JumpIntoTheWater
  • 1,306
  • 2
  • 19
  • 46
  • Not the kind of code that ever works that well in a server scenario. It creates a new instance of Outlook.exe every single time the method gets called. A very heavy process, designed to run on a work station class machine. The code doesn't put any real pressure on the garbage collector, so that process just keeps running. That won't last forever, sooner or later the OS runs out of kernel memory pool and then the show is over. Easy to see with Task Manager, you'll see dozens of copies of Outlook running. [Forcing a collection](http://stackoverflow.com/a/25135685/17034) is required. – Hans Passant Apr 02 '17 at 15:52
  • So the answer to my problem is killing the outlook process at the end of each call to the outlook ? Is that correct? – JumpIntoTheWater Apr 03 '17 at 04:24
  • Probably not, but nobody can tell why you create a new Application object over and over again. Just don't, one ought to be enough. – Hans Passant Apr 03 '17 at 04:27
  • Ok.thanks. I will change that one – JumpIntoTheWater Apr 03 '17 at 04:53

0 Answers0