【问题标题】:How to test connection to an Exchange Server?如何测试与 Exchange Server 的连接?
【发布时间】:2015-02-19 03:28:58
【问题描述】:

我正在开发一个使用 Exchange API 发送电子邮件的 C# 项目。

到目前为止,我完全可以发送电子邮件,我只是想知道如何测试我与 Exchange Server 的连接。

我需要一个名为 TestConnection 的方法,在调用该方法时测试与交换服务器的连接。这可能吗?

我在 Exchange API 上找不到任何可以实现这一点的东西。下面是我用来连接和发送电子邮件的部分代码。

ExchangeService service = new ExchangeService();
if (String.IsNullOrEmpty(DomainName))
    service.Credentials = new WebCredentials(FromEmail,Password);
service.Credentials = new WebCredentials(FromEmail, Password, DomainName);
service.AutodiscoverUrl(FromEmail, RedirectionUrlValidationCallback);

//Email Setup
EmailMessage emailMessage = new EmailMessage(service);

//EmailMessageSetting:

emailMessage.Save();
emailMessage.SendAndSaveCopy();

提前致谢!!

【问题讨论】:

    标签: c# email exchange-server


    【解决方案1】:

    经过一番挖掘,我意识到如果连接参数有问题,service.AutodiscoverUrl 会抛出异常。

    如果您输入了错误的域名、电子邮件或密码,AutodiscoverUrl 将抛出错误。

    我在 TestConnection 方法中所做的是调用 AutodiscoverUrl。此方法将使用 Exchange Web 服务 Url 设置 ExchangeService.Url 属性,用于作为参数的电子邮件,如果一切正常,如果电子邮件、密码或域名有问题则抛出异常。

    最后,如果 Service.url 不为 null 或为空,则连接测试成功。

    附: 尽管 AutodiscoverUrl 仅使用电子邮件作为参数,但它也会验证 Service.Credentials 中的密码。

    【讨论】:

    • 可能迟到了聚会,但在我的应用程序中,我通过简单地绑定到收件箱文件夹来测试连接(网址和凭据)。每个邮箱都有一个,因此足以验证所有数据是否良好。并且它不会在 Exchange 上创建“虚拟”对象,因此测试不会更改邮箱状态。只是我的 2 美分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-25
    • 2023-04-02
    • 2016-05-18
    • 1970-01-01
    • 1970-01-01
    • 2015-10-03
    相关资源
    最近更新 更多