如何使用Microsoft.Exchange.WebServices?

发布于 2024-09-26 08:05:20 字数 2411 浏览 9 评论 0原文

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

错误返回行:service.AutodiscoverUrl("[电子邮件受保护] ");

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


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

        }
    }
}

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

Error return line: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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

相思故 2024-10-03 08:05:20

我知道这是一个老问题,但最近一直在努力解决这个问题以及类似的错误(包括 ISA 服务器)。已修复:

service.EnableScpLookup = false;

使用显式 URL 时不需要,但使用自动发现时需要

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

service.EnableScpLookup = false;

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

拥抱没勇气 2024-10-03 08:05:20

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

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

这解决了我同样的问题。

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.

This solved my same issue.

怎言笑 2024-10-03 08:05:20

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

如何查找您的 Exchange 网址:

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

在此处输入图像描述

哦,要使用您添加的额外代码行的网址:

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

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:

enter image description here

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

service.Url = new Uri("your url here");
仅此而已 2024-10-03 08:05:20

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

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

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.com in a web browser.

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.

小霸王臭丫头 2024-10-03 08:05:20

尝试这些概念:

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

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文