使用C#程序从Exchange服务器读取电子邮件

9
当您在网上搜索“如何通过编程方式读取电子邮件”时,您会发现很多简单的答案......所有网站都会解释大多数相同的内容,就像这个页面一样。http://omegacoder.com/?p=454。请注意保留HTML标记。
// 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命令时,出现了错误。错误信息为“无法找到Autodiscover服务。”

于是我进一步搜索并在微软的网站找到了这个 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

你需要使用自动发现吗?似乎交换服务器上未启用/运行该服务。 - Can Gencer
1
对我来说无所谓,我只想处理收到的电子邮件(附件)。我只知道自动发现很容易使用。Exchange服务器上需要运行哪些服务?我的意思是我可以检查一下。你建议处理收到的电子邮件和附件的方法是什么?感谢你的帮助。 - ethem
4个回答

17

如果你已经知道你的交换服务器地址,就不一定需要使用自动发现。可以尝试使用以下方法(更多信息请参考这里):

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

用你的交换服务器的主机名替换 "hostname"。


嗨Can,非常感谢。这太棒了。它有效...你能帮我获取附件(例如Zip文件)和最终的消息正文吗?我该如何获得这些?至少是附件。因为我每天都会收到一个需要处理的zip文件。我使用了“FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(10));”并循环遍历findResults.Items...我可以获取主题但无法获取正文和附件...谢谢Mesut - ethem
1
我认为您需要获取ItemId,然后单独下载消息。这是一个例子:http://msdn.microsoft.com/en-us/library/dd633665(v=exchg.80).aspx - Can Gencer
1
是的,但使用AutoDiscover的好处是在服务器更改时无需更新Exchange的“主机名”。 - WEFX
@WEFX,是的,但我认为他的域名没有正确启用/配置AutoDiscover。 - Can Gencer

2
我希望您现在已经有解决方案了。这只是为了帮助任何遇到此问题的人。 我在Technet文章中找到了解决方案,我进行了修改以适应自己的需求,并且对我很有效。 只需用以下内容替换您代码中的行即可:
service.AutodiscoverUrl("user@yourdomain.com",
delegate
{
     return true;
});

我还有其他问题,但与这个问题无关。

愉快的编码,

Sanjay。


1
我遇到了自动发现的同样问题。它并不是必须的,你可以指定你的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图标,然后单击“测试电子邮件自动配置”。
选中“使用AutoDiscover”复选框。输入托管在该Exchange服务器上的电子邮件地址及其密码,您将收到一堆URL。使用标有“可用性服务URL”的1个URL。

0

请注意,传递的凭据需要具有访问给定交换邮箱/服务器的权限。在我的情况下,使用不同的已获得适当权限的凭据可以工作,但对于我正在尝试使其工作的服务帐户则不行。

一旦我发现帐户需要获得哪些权限,我将在此更新。

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

供参考,异常信息如下: Microsoft.Exchange.WebServices.Data.ServiceResponseException: 未能获取调用帐户的有效Active Directory信息。确认它是有效的Active Directory帐户。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接