具有模拟配置的Exchange服务帐户以获取其他用户的电子邮件

时间:2018-07-09 12:43:37

标签: c# email exchange-server impersonation

下面是我编写的代码,以获取交换中其他帐户的电子邮件详细信息

第一步:我创建了具有模拟角色的其他交换帐户和服务帐户

第一个功能 在Windows窗体的按钮单击上,我正在连接我的交换帐户,并添加了电子邮件列表以为接收的电子邮件预订活动

private void btnStreamingNotification_Click(object sender, EventArgs e)
{
    _service.UseDefaultCredentials = true;
    _service.Credentials = new WebCredentials("ServiceAccount@**********", "******");
    _service.AutodiscoverUrl("ServiceAccount@**************", RedirectionCallback);


    #region streaming Notification
    List<string> mailBoxes = new List<string>();
    mailBoxes.Add("*******@******.onmicrosoft.com");
    mailBoxes.Add("******@*****.onmicrosoft.com");
    mailBoxes.Add("******@******.onmicrosoft.com");

    AutoResetEvent autoResetEvent = new AutoResetEvent(false);

    StreamingSubscriptionConnection connection = new StreamingSubscriptionConnection(_service, 30);

    foreach (String email in mailBoxes)
    {
        _service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, email);

        List<FolderId> userFolder = new List<FolderId>();
        Mailbox userMailbox = new Mailbox(email);
        FolderId userFolderId = new FolderId(WellKnownFolderName.Inbox, userMailbox);
        userFolder.Add(userFolderId);
        StreamingSubscription StreamingSubscription = _service.SubscribeToStreamingNotifications(
               userFolder, EventType.NewMail, EventType.Modified);
        connection.AddSubscription(StreamingSubscription);    
    }

    connection.OnNotificationEvent += OnNotificationEvent          
    connection.OnDisconnect += OnDisconnect;
    connection.OnSubscriptionError += OnError;

    connection.Open();

    autoResetEvent.WaitOne();

    #endregion

    ReadOnlineEmails(_service);

}

步骤2: 第二功能 当新邮件发送到上述电子邮件之一时,此功能会被点击

static void OnNotificationEvent(object sender, NotificationEventArgs args)
{
    StreamingSubscription subscription = args.Subscription;

    // Extract the item IDs for all NewMail events in the list.
    var newMails = from e in args.Events.OfType<ItemEvent>()
                   where e.EventType == EventType.NewMail //|| e.EventType == EventType.Modified
                           select e.ItemId;
    if (newMails.Count() == 0)
        return;
    // Get the GUID for the property set.
    Guid EmailSentimentScorePropertySetId = new Guid("{C11FF724-AA03-4555-9952-8FA248A11C3E}");

    // Create a definition for the extended property.
    ExtendedPropertyDefinition sentimentScoreProperty =
        new ExtendedPropertyDefinition(EmailSentimentScorePropertySetId, "Sentiment Score", MapiPropertyType.String);


    // Note: For the sake of simplicity, error handling is ommited here. 
    // Just assume everything went fine.
    PropertySet propertySet = new PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.UniqueBody, ItemSchema.DateTimeReceived, ItemSchema.Subject, sentimentScoreProperty);
    propertySet.RequestedBodyType = BodyType.Text;
    propertySet.RequestedUniqueBodyType = BodyType.Text;
    var response = _service.BindToItems(newMails, propertySet);

    var items = response.Select(itemResponse => itemResponse.Item);
    foreach (var item in items)
    {
    }
}

错误 当我向前两个电子邮件ID发送邮件时,出现此错误

  

“访问被拒绝。检查凭据,然后重试。,此过程   无法获得正确的属性。”

对于最近的电子邮件,我没有收到任何错误,我正在成功获取数据

enter image description here

0 个答案:

没有答案