![](/img/trans.png)
[英]How to send email via a Shared MailBox using Exchange Web Services (EWS) API
[英]Determine if mailbox exists using Exchange Web Services API
使用Exchange Web Services API,是否可以确定组织内是否存在邮箱/电子邮件地址(例如someone@mydomain.com) ?
如果是这样,这是最简单的方法,而且不使用模拟方法是否有可能?
案例: Windows服务会定期向组织内的人员发送电子邮件。 它对他们的电子邮件地址没有任何明确的了解。 它仅知道其用户名,并假定其电子邮件地址为用户名 @ mydomain.com。 对于所有用户来说都是如此,除了一些没有邮箱的用户。 在这些情况下,它不应该首先尝试发送电子邮件。
如mathieu所建议:而是在Active Directory中查找用户和电子邮件地址。 此功能可以完成工作:
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
}
确定用户是否仅具有带有EWS的邮箱可能比预期的要复杂得多,尤其是在没有模拟的情况下。
如果您在Active Directory域中,则应依靠DirectoryEntry信息来确定用户的邮箱,并相应地发送电子邮件。 如果您获得了用户登录名,则获取关联的DirectoryEntry真的很容易。
有一种简单的方法可以通过检查用户的可用性来做到这一点,例如以下代码。 我试过了,它对我有用。
我不确定可用性结果返回错误的其他情况,但是可以确定当电子邮件不正确时会这样做
要定义您的交换服务,请参考以下网址 : https : //docs.microsoft.com/zh-cn/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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.