EWS只读取我的收件箱

时间:2014-03-11 11:44:07

标签: c# soap exchangewebservices exchange-server-2010

我正在尝试阅读特定的电子邮件帐户,以查找包含未读附件的商品,并在主题中包含某些字词。

我有下面的代码可以使用但是只能读取我的收件箱不是我要检查的收件箱

static void Main(string[] args)
    {
    ServicePointManager.ServerCertificateValidationCallback=CertificateValidationCallBack;
    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);// .Exchange2007_SP1);

    service.UseDefaultCredentials = true;
    service.AutodiscoverUrl("bbtest@domainname.com"); //, RedirectionUrlValidationCallback); //bbtest@bocuk.local

    ItemView view = new ItemView(1);

    string querystring = "HasAttachments:true Kind:email"; 
    //From:'Philip Livingstone' Subject:'ATTACHMENT TEST' Kind:email";

    // Find the first email message in the Inbox that has attachments. 
    // This results in a FindItem operation call to EWS.
    FindItemsResults<Item> results = service.FindItems(WellKnownFolderName.Inbox, querystring, view);

    foreach (EmailMessage email in results)

        if (email.IsRead == false) // && email.HasAttachments == true
        {
            System.Diagnostics.Debug.Write("Found attachemnt on msg with subject: " + email.Subject);

            // Request all the attachments on the email message. 
            //This results in a GetItem operation call to EWS.
            email.Load(new PropertySet(EmailMessageSchema.Attachments));

            foreach (Attachment attachment in email.Attachments)
            {
                if (attachment is FileAttachment)
...

我需要更改什么才能让我的代码读取 bbtest@domainname.com 收件箱中的邮件?

1 个答案:

答案 0 :(得分:7)

默认情况下,您的邮箱&#39;在 ExchangeService 对象上调用 FindItems 时,inbox文件夹将设置为root。假设您的域帐户具有访问其他邮箱所需的权限,则以下操作应该可以解决问题。我已经省略了服务创建部分。

//creates an object that will represent the desired mailbox
Mailbox mb = new Mailbox(@"othermailbox@domain.com");

//creates a folder object that will point to inbox folder
FolderId fid = new FolderId(WellKnownFolderName.Inbox, mb); 

//this will bind the mailbox you're looking for using your service instance
Folder inbox = Folder.Bind(service, fid);

//load items from mailbox inbox folder
if(inbox != null) 
{
    FindItemsResults<Item> items = inbox.FindItems(new ItemView(100));

    foreach(var item in items)
        Console.WriteLine(item);
}