如何使用Microsoft.Exchange.WebServices?

时间:2010-10-08 13:29:16

标签: c# visual-studio exchange-server exchange-server-2007 ews-managed-api

我尝试使用:Microsoft.Exchange.WebServices.dll来使用outlook。但连接返回错误

错误返回行: service.AutodiscoverUrl(“myusernamek@xxxx.com”);

无法找到自动发现服务。我的代码:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
using Microsoft.Exchange.WebServices.Data;
using Microsoft.Exchange.WebServices.Autodiscover;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;

namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                // Connect to Exchange Web Services as user1 at contoso.com.
                ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
                service.Credentials = new WebCredentials("myusernamek@xxxx.com", "mypassword", "xxxx.com");
                service.TraceEnabled = true;
                service.AutodiscoverUrl("myusernamek@xxxx.com");

                // Create the e-mail message, set its properties, and send it to user2@contoso.com, saving a copy to the Sent Items folder. 
                EmailMessage message = new EmailMessage(service);
                message.Subject = "Interesting";
                message.Body = "The proposition has been considered.";
                message.ToRecipients.Add("recipientname@xxxx.aero");
                message.SendAndSaveCopy();

                // Write confirmation message to console window.
                Console.WriteLine("Message sent!");
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
                Console.ReadLine();
            }

        }
    }
}

alt text

5 个答案:

答案 0 :(得分:2)

我知道这是一个老问题,但最近还遇到了这个和类似的错误(包括ISA服务器)。它修复了:

service.EnableScpLookup = false;

使用显式网址时不需要这样做,但在使用自动发现时

答案 1 :(得分:0)

代码表明您拥有Exchange 2007服务器...是否已正确配置以使用自动发现功能?确认您可以ping自动发现.XXXX.com并在网络浏览器中查看https://autodiscover.XXXX.com

或者,您可能需要使用内部域名进行自动发现和登录。例如,在我的办公室中,外部电子邮件地址位于CompanyX.com之类的域中,但内部Active Directory域类似于CompanyX.local,我们在开放的Internet上没有自动发现,因此我的EWS需要找到Autodiscover.CompanyX.local

答案 2 :(得分:0)

这是一个常见问题,当此次自动发现服务被关闭时遇到自动发现服务错误

解决方案是提供交换位置的实际网址,而不是自动发现它。

这解决了我同样的问题。

答案 3 :(得分:0)

这是一篇旧文章,但也许​​有人会需要它。 不要使用自动发现,它运行缓慢。

如何找到您的交易所网址:

  • 打开您的Outlook应用程序并连接到您的交易所
  • 按住Ctrl键并右键单击系统任务栏中的Outlook图标
  • -选择“测试电子邮件自动配置”
  • 单击测试按钮
  • -查找以下行:

enter image description here

哦,要使用该网址,您需要多写一行代码:

service.Url = new Uri("your url here");

答案 4 :(得分:-2)

尝试这些概念:

private static ExchangeService getService(String userEmail, String login, String password, String hostName)
{
    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010); 
    AutodiscoverService auservice = new AutodiscoverService(hostName);
    if (auservice.ServerInfo != null) 
    {
        try
        {
            service.AutodiscoverUrl(userEmail, RedirectionUrlValidationCallback);
        }
        catch (AutodiscoverRemoteException ex)
        {
            Console.WriteLine("Exception thrown: " + ex.Error.Message);
        }

    }
    else
    {
        service.Url = new Uri("https://" + hostName + "/EWS/Exchange.asmx");
    }

    service.UseDefaultCredentials = true;


    if (service.ServerInfo == null)
    {
        service.Credentials = new WebCredentials(login, password);
    }
    return service;
}