【问题标题】:c# programmatically reading emails from the Exchange serverc# 以编程方式从 Exchange 服务器读取电子邮件
【发布时间】:2011-07-12 02:17:42
【问题描述】:

当您在网络上搜索时,您会发现“如何以编程方式阅读电子邮件”的非常简单的答案......所有网站都在解释大部分与此页面相同的内容。 http://omegacoder.com/?p=454

// depends from Exchange server version      
        service.Credentials = new NetworkCredential("MDR", "password", "zzz");
        service.AutodiscoverUrl("mdr@zzz.be");
        object o = service.FindItems(WellKnownFolderName.Inbox, new ItemView(10));
        FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(10));
        foreach (Item item in findResults.Items)
        {
            Console.WriteLine(item.Subject);
        }

它在执行 autodiscoverURL 行时失败。错误提示“找不到自动发现服务”。

所以我进一步搜索并从 Microsoft https://www.testexchangeconnectivity.com/#&&/wEXAQUBcwUBME93h2+JjI0+MV2gTqcRL0g43z9m 找到了这个站点,您可以在这里测试您的邮件服务器.... 当我传递参数时,我得到下面的错误......

但我还是不明白问题出在哪里?我需要向 DNS 添加记录吗?有人可以帮忙吗?

Attempting to test potential Autodiscover URL https://autodiscover.zzz.be/AutoDiscover/AutoDiscover.xml
 Testing of this potential Autodiscover URL failed.
 Test Steps
 Attempting to resolve the host name autodiscover.ncb.be in DNS.
 The host name resolved successfully.
 Additional Details
 IP addresses returned: 213.246.192.205

Testing TCP port 443 on host autodiscover.ncb.be to ensure it's listening and open.
 The specified port is either blocked, not listening, or not producing the expected response.
  Tell me more about this issue and how to resolve it
 Additional Details
 A network error occurred while communicating with the remote host.
Exception details:
Message: No connection could be made because the target machine actively refused it 213.246.192.205:443
Type: System.Net.Sockets.SocketException
Stack trace:
at System.Net.Sockets.TcpClient.Connect(String hostname, Int32 port)
at Microsoft.Exchange.Tools.ExRca.Tests.TcpPortTest.PerformTestReally()




Attempting to contact the Autodiscover service using the HTTP redirect method.
 The attempt to contact Autodiscover using the HTTP Redirect method failed.
 Test Steps
 Attempting to resolve the host name autodiscover.zzz.be in DNS.
 The host name resolved successfully.
 Additional Details
 IP addresses returned: 213.246.192.205

Testing TCP port 80 on host autodiscover.zzz.be to ensure it's listening and open.
 The port was opened successfully.
ExRCA is checking the host autodiscover.zzz.be for an HTTP redirect to the Autodiscover service.
 ExRCA failed to get an HTTP redirect response for Autodiscover.
 Additional Details
 A Web exception occurred because an HTTP 404 - NotFound response was received from IIS7.



Attempting to contact the Autodiscover service using the DNS SRV redirect method.
 ExRCA failed to contact the Autodiscover service using the DNS SRV redirect method.
 Test Steps
 Attempting to locate SRV record _autodiscover._tcp.ncb.be in DNS.
 The Autodiscover SRV record wasn't found in DNS.
  Tell me more about this issue and how to resolve it

【问题讨论】:

  • 你需要使用自动发现吗?似乎该服务未在交换服务器上启用/运行
  • 对我来说没关系。我只想处理传入的电子邮件(附件)。我只是了解自动发现很容易使用。我需要在 Exchange 服务器上运行哪些服务?我的意思是我可以检查。您建议处理传入电子邮件的方法是什么 - 附件。感谢您的帮助。

标签: c# exchange-server exchange-server-2007


【解决方案1】:

我在使用 AutoDiscover 时遇到了同样的问题。没必要,你可以指定你的 URL 像

    Uri myUri = new Uri("https://Hostname/ews/exchange.asmx");
    userData.AutodiscoverUrl = myUri;
    service.Url = myUri;

作为主机名,您可以输入服务器 IP 地址,例如 192.168.100.10

或者,如果您使用 Outlook,要查找您的 Exchange 服务器主机名(实际上是要使用的整个 url),请转到显示日期和时间的计算机开始栏,您会找到 Outlook 图标, 按住 Ctrl + 右键单击​​ Outlook 图标并单击“测试电子邮件自动配置”

选中“使用自动发现”复选框。输入托管在该 Exchange 服务器上的电子邮件地址及其密码,您将收到一堆 url。使用 1 表示“可用性服务 URL”

【讨论】:

    【解决方案2】:

    考虑传递的凭据需要对给定的交换邮箱/服务器具有权限。在我的情况下,使用一组不同的经过适当许可的凭据可以工作,但不适用于我试图开始工作的服务帐户。 一旦我发现该帐户究竟需要获得什么许可,我将在此处更新它。

    更新:我的问题是服务帐户来自与运行 Exchange 2007 实例的域不同的域,即使两者之间存在信任关系。我发现这是 Exchange 2007 中记录在案的已知问题,即它如何在其林中查找帐户。最后必须在交换服务器所在的域上创建一个相同的服务帐户(名称/密码),并将用户名指定为 {exchange_domain}{service_account_name}。调用 EWS 的 Windows 服务以 {original_domain}{service_account_name} 的身份运行。

    作为参考,例外情况是: Microsoft.Exchange.WebServices.Data.ServiceResponseException: 无法获取呼叫帐户的有效 Active Directory 信息。确认它是有效的 Active Directory 帐户。

    【讨论】:

      【解决方案3】:

      我希望你现在应该有解决方案。这只是为了帮助任何遇到此帖子的人。 我在其中一个 technet article 上找到了解决方案,我很适合我,并且对我来说工作正常。

      只需将代码中的行替换为以下代码:

      service.AutodiscoverUrl("user@yourdomain.com",
      delegate
      {
           return true;
      });
      

      我还有一些其他问题,但与此无关。

      快乐编码,

      桑杰。

      【讨论】:

        【解决方案4】:

        如果您已经知道 Exchange 服务器的地址,则不一定需要使用自动发现功能。请尝试以下方法(有关更多信息,请查看here

        service.Url = new Uri("https://hostname/EWS/Exchange.asmx");
        

        将“主机名”替换为您的 Exchange 服务器的主机名。

        【讨论】:

        • 嗨,Can,非常感谢。这很棒。它有效...您能否帮助我获取附件(例如 Zip 文件)并最终获取邮件正文。我如何得到这些?至少附件。因为我每天都会收到一个需要处理的压缩文件。我使用“FindItemsResults findResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(10));”并遍历 findResults.Items ......我可以得到主题,但不能得到正文和附件......谢谢 Mesut
        • 我认为您需要获取ItemId,然后单独下载消息。这是一个示例:msdn.microsoft.com/en-us/library/dd633665(v=exchg.80).aspx
        • 是的,但是使用自动发现的好处是,当您的服务器发生变化时,您永远不必更新您的 Exchange“主机名”。
        • @WEFX,是的,但我认为他的域没有正确启用/配置自动发现。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-03-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-03
        • 1970-01-01
        • 2017-04-19
        相关资源
        最近更新 更多