質問

I'm using the ExchangeService WebService API (Microsoft.Exchange.WebServices.Data) but I cannot find any Close or Dispose method.

Is it not neccessary to close the connection somehow?

My method looks like this:

public void CheckMails()
{
    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
    IMAPCredentials creds = new IMAPCredentials();
    service.Credentials = new NetworkCredential(creds.User, creds.Pass, creds.Domain);
    service.AutodiscoverUrl(creds.User + "@example.com");

    // not the real code from here on but you'll get the idea...
    // var emails = service.FindItems();
    // emails[0].Load();
    // emails[0].Attachments[0].Load();
    // ...
}
役に立ちましたか?

解決

There is no Close/Dispose method on the ExchangeService class because the class does not maintain a connection to the web services. Instead a new HTTP connection is created and closed as needed.

For example when you call ExchangeService.FindItems a new HTTP connection to the Exchange server is created and closed within the method call to FindItems.

他のヒント

I realize that this is pretty old, but I had the same question recently, because we've had a problem after connecting to a mailbox, and trying the same method again soon after, we get an HTTP exception. Then, after waiting a minute or so, we can connect...but like the comments on the accepted answer, this is probably a setting on the Exchange server.

To answer the question, technically speaking, since ExchangeService does not implement IDisposable, then there is no need to Dispose a connection, nor could you wrap an instance in a using statement.

private static void ProcessMail()
{
    ExchangeService exchange = new ExchangeService();
    exchange.Credentials = new WebCredentials(sACCOUNT, sPASSWORD, sDOMAIN);
    exchange.AutodiscoverUrl(sEMAIL_ADDRESS);

    if (exchange != null)
    {
        Folder rootFolder = Folder.Bind(exchange, WellKnownFolderName.Inbox);
        rootFolder.Load();

        foreach (Folder folder in rootFolder.FindFolders(new FolderView(100)))
        {
            //your code
        }
        exchange = null;
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top