使用Exchange Web服务阅读电子邮件 [英] read email using exchange web services

查看:135
本文介绍了使用Exchange Web服务阅读电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的情况:我必须阅读来自Exchange 2010 sp2帐户的电子邮件。我必须使用Exchange Web服务,POP3和IMAP被阻止。我必须在人们只能通过Intranet通过Web浏览器访问其帐户的环境中测试我的应用程序。我无法直接将应用调试到此Intranet。我有以下代码段可访问帐户:

This is my scenario: I have to read email from exchange 2010 sp2 accounts. I have to use Exchange Web Services, POP3 and IMAP are blocked. I have to test my app in an environment where people can access their accounts through a web browser only in the intranet. I can't debug my app directly to this intranet. I have this snippet to access an account:

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(string,AutodiscoverRedirectionUrlValidationCallback)重载。

"Autodiscover blocked a potentially insecure redirection to https://autodiscover.colpatria.com/autodiscover/autodiscover.xml. To allow Autodiscover to follow the redirection, use the AutodiscoverUrl(string, AutodiscoverRedirectionUrlValidationCallback) overload."

因此方法 RedirectionUrlValidationCallback 已实现,我不确定该URL是否正确。事实是我收到此错误:

So the method RedirectionUrlValidationCallback was implemented, I'm not sure if the url is right. The fact is I'm getting this error:


找不到自动发现服务。

"The Autodiscover service couldn't be located".

是否可能没有正确配置自动发现?我不是交易所管理员,我怎么知道自动发现是否有效?我需要参数来告诉交易所管理员必须配置此功能。谢谢您的帮助。

Is possible that Autodiscover is not configured properly?? I'm not the exchange administrator, how can I know if autodiscover works?? I need arguments to tell exchange administrators this feature must be configured. Thanks for any help.

推荐答案

这是一篇旧文章,我认为我应该针对报告的错误提供完整的示例解决方案。只需将 service.AutodiscoverUrl( someuser@somedomain.org); 替换为 System.Uri( https://mail.somedomain.org/ews/Exchange.asmx );

This is a old post I thought I'd put in a full example solution for the error reported. Simply replace service.AutodiscoverUrl("someuser@somedomain.org"); with 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");

这篇关于使用Exchange Web服务阅读电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆