【问题标题】:How to get the list of email addresses present in Outlook?如何获取 Outlook 中存在的电子邮件地址列表?
【发布时间】:2020-03-31 03:50:31
【问题描述】:

我使用 .Net 核心创建了一个项目,如果我在我的一个页面中的“用户”文本框字段中输入任何名称或电子邮件,我必须匹配名称并获取可用的用户列表Outlook 的组织域。那么如何获取 Outlook 中可用的所有电子邮件地址或用户列表呢?

我尝试过以下方法,但我不熟悉此功能,我不知道如何连接我们的 Outlook 帐户并获取地址列表。谁能帮我解决这个问题?

public static void DisplayAccountInformation(Application application)
{

        // The Namespace Object (Session) has a collection of accounts.
        Accounts accounts = application.Session.Accounts;

        // Concatenate a message with information about all accounts.
        StringBuilder builder = new StringBuilder();

        // Loop over all accounts and print detail account information.
        // All properties of the Account object are read-only.
        foreach (Account account in accounts)
        {

            // The DisplayName property represents the friendly name of the account.
            builder.AppendFormat("DisplayName: {0}\n", account.DisplayName);

            // The UserName property provides an account-based context to determine identity.
            builder.AppendFormat("UserName: {0}\n", account.UserName);

            // The SmtpAddress property provides the SMTP address for the account.
            builder.AppendFormat("SmtpAddress: {0}\n", account.SmtpAddress);

            // The AccountType property indicates the type of the account.
            builder.Append("AccountType: ");
            switch (account.AccountType)
            {

                case OlAccountType.olExchange:
                    builder.AppendLine("Exchange");
                    break;

                case OlAccountType.olHttp:
                    builder.AppendLine("Http");
                    break;

                case OlAccountType.olImap:
                    builder.AppendLine("Imap");
                    break;

                case OlAccountType.olOtherAccount:
                    builder.AppendLine("Other");
                    break;

                case OlAccountType.olPop3:
                    builder.AppendLine("Pop3");
                    break;
            }

            builder.AppendLine();
        }

        // Display the account information.

 }

我通过传递我的域名来调用函数,

var application = new Application();
application.CreateObject("outlook.office365.com");
DisplayAccountInformation(application);

【问题讨论】:

  • 你有什么错误吗?你能解释一下为什么你的代码没有按预期工作吗?
  • 错误:System.Runtime.InteropServices.COMException:'操作失败。' - 和代码行:“application.CreateObject("outlook.office365.com");"。这里的事情是我不知道如何指定我的域和服务器,我的代码是对还是错。

标签: c# outlook azure-active-directory office365 email-validation


【解决方案1】:
    [HttpGet]
    public JsonResult getADEmail(string query = "")
    {
        DirectorySearcher dirSearcher = new DirectorySearcher();
        dirSearcher.Filter = "(&(objectClass=user)(objectcategory=person)(mail=" + query + "*))";

        SearchResult srEmail = dirSearcher.FindOne();
        SearchResultCollection SearchResultCollection = dirSearcher.FindAll();
        string propName = "mail";
        ResultPropertyValueCollection valColl = srEmail.Properties[propName];

        List<string> results = new List<string>();
        foreach (SearchResult sr in SearchResultCollection)
        {
            ResultPropertyValueCollection val = sr.Properties[propName];
            results.Add(val[0].ToString());
        }

        return Json(results);
    }

【讨论】:

  • 此代码在我的本地运行良好,但在 IIS 服务器中无法运行。 (即)将我的应用程序托管到 IIS 服务器后,由于某些权限问题,目录搜索器无法正常工作。
【解决方案2】:

您没有使用正确的方法来获取用户列表。

最简单的方法是listing users through Microsoft Graph API:

GET https://graph.microsoft.com/v1.0/users

您可以在Microsoft Graph Explorer 进行快速测试。

如果您想了解如何使用 Microsoft Graph SDK 在 C# 中实现,请参阅example。 (选择 C# 选项卡)

但是,我仍然认为没有必要自己匹配名称如果您正在尝试实现登录过程。

OAuth 请求应使用 Microsoft 身份验证库 (MSAL) 处理,详情请参阅 here。

您需要仔细查看OAuth2 Authorization Code Grant flow with Azure AD。

【讨论】:

  • 如何传递参数authprovider?它是来自 Active Directory 的不记名令牌吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-27
  • 1970-01-01
  • 2017-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多