如何使用ExchangeService访问共享邮箱(Outlook 2013)

本文关键字:Outlook 2013 何使用 ExchangeService 访问共享 | 更新日期: 2023-09-27 18:16:20

我正在尝试使用ExchangeService访问outlook中的第二个邮箱。但当我运行应用程序时,它会抓取我的主邮件。值得注意的是,我在这里使用了windows身份验证,并且我需要这样做才能正常工作。

var useExchangeServer = new UseExchangeServer("SECONDEMAIL@mycompany.com");
var messages = useExchangeServer.LoadMessages(1);
foreach (var message in messages){ //At this point message has the wrong email...
...
}

这是我的"UseExchangeServer"类

public UseExchangeServer(string mailBox)
    {
        _service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
        _service.UseDefaultCredentials = true;
        _service.AutodiscoverUrl(mailBox);
    }
public FindItemsResults<Item> GetLastItems(int numberOfItems)
    {
        return _service.FindItems(WellKnownFolderName.Inbox, new ItemView(numberOfItems));
    }
public IEnumerable<EmailMessage> LoadMessages(int numberOfMessages)
    {
        var findResults = GetLastItems(numberOfMessages);
        foreach (var item in findResults.Items)
        {
            var message = EmailMessage.Bind(_service, item.Id, new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments));
            message.Load();
            yield return message;
        }
    }

如何使用ExchangeService访问共享邮箱(Outlook 2013)

您需要使用FolderId重载来指定您想要访问的邮箱,否则将使用属于您正在使用的凭据的邮箱,因此您需要对代码执行以下操作

public UseExchangeServer(string mailBox)
{
    _service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
    _service.UseDefaultCredentials = true;
    _service.AutodiscoverUrl(mailBox);
}
 public FindItemsResults<Item> GetLastItems(int numberOfItems,string mailBox)
{
    FolderId FolderToAccess = new FolderId(WellKnownFolderName.Inbox,mailBox);
    return _service.FindItems(FolderToAccess, new ItemView(numberOfItems));
}
public IEnumerable<EmailMessage> LoadMessages(int numberOfMessages)
{
    var findResults = GetLastItems(numberOfMessages);
    foreach (var item in findResults.Items)
    {
        var message = EmailMessage.Bind(_service, item.Id, new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments));
        message.Load();
        yield return message;
    }
}

干杯格伦