-->

使用Exchange Web服务阅读电子邮件(read email using exchange w

2019-06-26 07:08发布

这是我的情况:我从Exchange 2010 SP2帐户阅读电子邮件。 我必须使用Exchange Web服务,POP3和IMAP被封锁。 我必须测试我的应用程序中,人们只能在企业内部网,通过Web浏览器访问他们的帐户的环境。 我不能直接调试我的应用程序这个内部网。 我有这样的片段来访问帐户:

private void Dowork()
{
    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);

    string dominio = "domain";
    string usuario = "user";
    string password = "password";

    service.Credentials = new NetworkCredential(usuario, password, dominio);

    string url = usuario + "@" + dominio + ".com";

    service.AutodiscoverUrl(url, RedirectionUrlValidationCallback);
    //service.AutodiscoverUrl(url);

    FindItemsResults<Item> findResults = service.FindItems(
       WellKnownFolderName.Inbox,
       new ItemView(10));

    string content = string.Empty;

    foreach (Item item in findResults.Items)
    {
        EmailMessage email = EmailMessage.Bind(service, item.Id);
        email.Load();

        content += item.Subject + "\n";
        content += email.From.Address + "\n";
        content += email.Body + "\n\n";

        //Console.WriteLine(item.Subject);
        //Console.WriteLine(email.From.Address);
        //Console.WriteLine(email.Body);
    }

    string result = content;
}

// Create the callback to validate the redirection URL.
static bool RedirectionUrlValidationCallback(String redirectionUrl)
{
    // Perform validation.
    return (redirectionUrl == "https://autodiscover-s.outlook.com/autodiscover/autodiscover.xml");
}

如果我使用这行:

service.AutodiscoverUrl(url);

我得到这个错误:

“自动发现阻止潜在的不安全的重定向https://autodiscover.colpatria.com/autodiscover/autodiscover.xml 。为了允许自动发现跟随重定向,使用AutodiscoverUrl(字符串,AutodiscoverRedirectionUrlValidationCallback)过载”。

因此,该方法RedirectionUrlValidationCallback开始实施,我不知道如果URL是正确的。 事实是,我得到这个错误:

“自动发现服务找不到”。

能够自动发现未正确配置? 我不是Exchange管理员,我怎么能知道,如果自动发现工作? 我需要的参数告诉Exchange管理员此功能必须进行配置。 谢谢你的帮助。

Answer 1:

已经运行到近期和工作来解决这些问题,我发现一个实用工具,为类似问题/在排除故障时非常有用: EWS编辑器它可能不是解决你的问题,但可以用来很快,希望这将提供一些线索遍历不同的配置组合你的问题。

我与客户建立测试和督促Exchange服务器自动发现和服务URL连接工作时使用这个应用程序。 这不仅是对我,而且客户的IT人员方便的为好。 他们下载和使用该实用程序来测试并验证其设置。

从http://ewseditor.codeplex.com :

项目描述

EWSEditor有三个目标:

  1. 通过它的源代码,演示了Exchange Web服务托管API的功能和简单的开发。

  2. 演示用于执行通过资源管理器用户界面启动的操作Exchange Web服务SOAP传输。

  3. 通过探索项目,文件夹,以及他们在深度特性帮助调试和理解Exchange存储非开发人员



Answer 2:

不知怎的,你需要登录什么样的结果redirectionUrl是。 当你你会得到这个错误redirectionUrl不匹配您指定的URI( 即你的自动发现验证回调函数返回FALSE )。 当然, redirectionUrl URI是不是你认为它是。 如果您使用的是SSL - 你需要处理重定向验证回调。

既然你不能调试应用程序,也许你可以发送电子邮件给自己,登录一个共享的数据库或文件,或者使用应用程序事件日志( 抛出一个应用程序异常 )。

注:第一个错误不会告诉你的自动发现URI是https://autodiscover.colpatria.com/autodiscover/autodiscover.xml 。 如果这种替换现有的字符串https://autodiscover-s.outlook.com/autodiscover/autodiscover.xml

另请参阅相关的SO发布有关交易所自动发现和验证MSDN上潜在的不安全重定向URL 。



Answer 3:

这是一个老帖子,我想我会把在报告的错误一个完整的示例解决方案。 只需更换service.AutodiscoverUrl( “someuser@somedomain.org”);的System.Uri( “ https://mail.somedomain.org/ews/Exchange.asmx ”);

下面是代码的完整块

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
                service.Credentials = new WebCredentials("someuser", "somepassword");
                //service.AutodiscoverUrl("someuser@somedomain.org");
                service.Url = new System.Uri("https://mail.somedomain.org/ews/Exchange.asmx");


Answer 4:

尝试service.TraceEnabled = true;

WFM。 在我来说,我需要通过安装在Exchange Server的证书到客户机建立SSL / TLS。 我被带到了从跟踪的输出这一解决方案。



Answer 5:

这就像一个魅力对我说:

   var exchange = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
   var username = Settings.EmailUserName;
   var password = Settings.EmailPassword;
   var domain = Settings.EmailDomain;
   var email = Settings.Email;
   exchange.Credentials = new WebCredentials(email, password);
   exchange.AutodiscoverUrl(email, RedirectionCallback);

和RedirectionCallback是:

 static bool RedirectionCallback(string url)
        {
            // Return true if the URL is an HTTPS URL.
            return url.ToLower().StartsWith("https://");
        }

继承人是链接: https://msdn.microsoft.com/en-us/library/office/dd635285(v=exchg.80).aspx

问候!



Answer 6:

当你与RedirectionUrlValidationCallback使用AutodiscoverUrl(),这里是一个示例代码:

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
service.PreAuthenticate = true;
service.Credentials = new WebCredentials("my_username","my_password"); //use WebCredentials instead of NetworkCredential
service.AutodiscoverUrl(userEmailAddress, RedirectionCallback);

而RedirectionCallback方法是这样的:

        static bool RedirectionCallback(string url)
        {
            bool redirectionValidated = false;
            Uri redirectionUri = new Uri(url);

//There are two ways of implementing a RedirectionCallback scheme

// Way 1: Return true if the URL is an HTTPS URL.
            //return url.ToLower().StartsWith("https://");
            if (redirectionUri.Scheme == "https")
                redirectionValidated = true;

//Way 2: check if url is autodiscovery url
            if (url.Equals(
                "https://autodiscover-s.outlook.com/autodiscover/autodiscover.xml"))
                redirectionValidated = true;

            return redirectionValidated;
        }

PS:注意代理禁止自动发现服务。 在我的情况下,该代码每次返回“自动发现服务无法找到”错误,但根本原因是403禁止在自动发现电话。 它没有经过代理设置。



文章来源: read email using exchange web services