visual-studio 如何使用 Microsoft.Exchange.WebServices?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3890986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 12:34:11  来源:igfitidea点击:

How to use Microsoft.Exchange.WebServices?

c#visual-studioexchange-serverexchange-server-2007ews-managed-api

提问by Penguen

i try to use : Microsoft.Exchange.WebServices.dll to use outlook. but connection return error

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

Error return line:service.AutodiscoverUrl("[email protected]");

错误返回行:service.AutodiscoverUrl("[email protected]");

The Autodiscover service could not be located.my codes:

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


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("[email protected]", "mypassword", "xxxx.com");
                service.TraceEnabled = true;
                service.AutodiscoverUrl("[email protected]");

                // Create the e-mail message, set its properties, and send it to [email protected], 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("[email protected]");
                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

替代文字

回答by quick_dry

I know this is an old question, but recently wrestled with this and similar looking error (including ISA server). It was fixed with:

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

service.EnableScpLookup = false;

This was not required when working with an explicit URL, but was when using AutoDiscover

这在使用显式 URL 时不是必需的,但在使用 AutoDiscover 时是必需的

回答by ewall

The code suggest that you have an Exchange 2007 server... Is it properly configured for using the Autodiscover features? Confirm that you can ping autodiscover.XXXX.com and view https://autodiscover.XXXX.comin a web browser.

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

Alternately, you may need to use your internal domain name for autodiscovery and login. For example, in my office the external email addresses are on a domain like CompanyX.com, but the internal Active Directory domain is like CompanyX.local, and we do not have autodiscover on the open Internet, so my EWS needs to locate Autodiscover.CompanyX.local.

或者,您可能需要使用内部域名进行自动发现和登录。例如,在我的办公室的外部电子邮件地址是像一个域名CompanyX.com,但内部Active Directory域是喜欢CompanyX.local,而且我们没有自动发现在开放的互联网上,所以我的EWS需求来定位Autodiscover.CompanyX.local

回答by Gurpreet Singh

This is a common problem , Autodiscover service error is encountered when this autodiscover service by exchange is down

这是一个常见的问题,当这个自动发现服务被交换器关闭时,会遇到自动发现服务错误。

Resolution is to provide actual URL for exchange location , rather than autodiscovering it.

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

This solved my same issue.

这解决了我同样的问题。

回答by Dave

this is an old post but maybe someone will need it. do not use auto discover, it is rly slow.

这是一个旧帖子,但也许有人会需要它。不要使用自动发现,它很慢。

how to find your exchange url:

如何找到您的交换网址:

  • -open your outlook application and connect to your exchange
  • -hold Ctrl key and right click on the outlook icon in the system tray
  • -select "test e-mail auto configuration"
  • -click the test button
  • -look for the following line:
  • - 打开您的 Outlook 应用程序并连接到您的交易所
  • - 按住 Ctrl 键并右键单击系统托盘中的 Outlook 图标
  • - 选择“测试电子邮件自动配置”
  • - 单击测试按钮
  • - 查找以下行:

enter image description here

在此处输入图片说明

oh and to use the url you trop that extra line of code:

哦,要使用 url 来添加额外的代码行:

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

回答by Aksndr

try these concept:

试试这些概念:

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;
}