2

Exchange WebサービスAPIを使用して、 someone @ mydomain.comなどのメールボックス/電子メールアドレスが組織内に存在するかどうかを判断することは可能ですか?

もしそうなら、これを行う最も簡単な方法はどれですか?なりすましを使用せずにそれは可能ですか?

ケース: Windowsサービスは、組織内の人々に定期的に電子メールを送信します。それは彼らの電子メールアドレスについての明確な知識を持っていません。ユーザー名のみを認識し、電子メールアドレスはユーザー名@mydomain.comであると想定します。これは、メールボックスを持たない一部のユーザーを除くすべてのユーザーに当てはまります。このような場合、そもそも電子メールの送信を試みるべきではありません。

解決:

mathieuが提案したように、代わりにActiveDirectoryでユーザーと電子メールアドレスを探します。この関数は仕事を成し遂げます:

using System.DirectoryServices.AccountManagement;
// ...

public static bool TryGetUserEmailAddress(string userName, out string email)
{
  using (PrincipalContext domainContext = 
    new PrincipalContext(ContextType.Domain, Environment.UserDomainName))
  using (UserPrincipal user = 
    UserPrincipal.FindByIdentity(domainContext, userName))
  {
    if (user != null && !string.IsNullOrWhiteSpace(user.EmailAddress))
    {
      email = user.EmailAddress;
      return true;
    }
  }
  email = null;
  return false; // user not found or no e-mail address specified
}
4

2 に答える 2

1

ユーザーがEWSのみのメールボックスを持っているかどうかを判断することは、特になりすましがなければ、予想よりも複雑になる可能性があります。

Active Directoryドメインを使用している場合は、DirectoryEntry情報に基づいてユーザーのメールボックスを決定し、それに応じて電子メールを送信する必要があります。ユーザーログインを取得した場合、関連するDirectoryEntryを取得するのは非常に簡単です。

于 2012-07-24T11:16:35.967 に答える
0

次のコードのようにユーザーの可用性を確認することで、これを行う簡単な方法があります。私はこれを試しました、そしてそれは私のために働いています。

可用性の結果がエラーを返す他のケースについてはわかりませんが、電子メールが正しくない場合は確かにエラーが返されます

交換サービスを定義するには、次を参照してください:https ://docs.microsoft.com/en-us/exchange/client-developer/exchange-web-services/get-started-with-ews-managed-api-client-applications

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);//You version
service.Credentials = new WebCredentials("user1@contoso.com", "password");
service.AutodiscoverUrl("user1@contoso.com", RedirectionUrlValidationCallback);
string email = "TEST@YOUR.COM";

// Get User Availability after 6 months 
AttendeeInfo attendee = new AttendeeInfo(email);
var attnds = new List<AttendeeInfo>();
attnds.Add(attendee);
var freeTime = service.GetUserAvailability(attnds, new 
TimeWindow(DateTime.Now.AddMonths(6), DateTime.Now.AddMonths(6).AddDays(1)), AvailabilityData.FreeBusyAndSuggestions);
//if you receive result with error then there is a big possibility that the email is not right
if(freetimes.AttendeesAvailability.OverallResult == ServiceResult.Error)
{
     return false;
}
return true;
于 2018-11-26T05:24:09.767 に答える