在 C# 中处置/关闭 ExchangeService?

发布于 2025-01-04 03:32:17 字数 726 浏览 4 评论 0原文

我正在使用 ExchangeService WebService API (Microsoft.Exchange.WebServices.Data),但找不到任何 CloseDispose 方法。

是否不需要以某种方式关闭连接?

我的方法如下所示:

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();
    // ...
}

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();
    // ...
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

葬シ愛 2025-01-11 03:32:17

ExchangeService 类上没有 Close/Dispose 方法,因为该类不维护与 Web 服务的连接。相反,会根据需要创建并关闭新的 HTTP 连接。

例如,当您调用 ExchangeService.FindItems 时,将创建与 Exchange 服务器的新 HTTP 连接,并在对 FindItems 的方法调用中关闭。

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.

[浮城] 2025-01-11 03:32:17

我意识到这已经很老了,但我最近也有同样的问题,因为我们在连接到邮箱后遇到了问题,并且不久之后再次尝试相同的方法,我们得到了 HTTP 异常。然后,等待一分钟左右,我们可以连接...但就像已接受答案的评论一样,这可能是 Exchange 服务器上的设置。

要回答这个问题,从技术上讲,由于 ExchangeService 没有实现 IDisposable,因此不需要 Dispose 连接,也不能将实例包装在 using 语句中。

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.

我不在是我 2025-01-11 03:32:17
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;
    }
}
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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文