2011-05-13 8 views
9

Próbuję użyć następującego kodu, aby połączyć się i pobrać załącznik z wiadomości e-mail w skrzynce odbiorczej przy użyciu C# i Exchange Web Services, ale pojawia się błąd "System.ArgumentOutOfRangeException" i nie mogę zobacz dlaczego. Mam wpisane do Google hasło, ale nie mogę znaleźć ani znaleźć odpowiedzi na bardzo stare wersje EWS.Pobierz załącznik z programu Exchange za pomocą usługi Exchange Web Services

Wiem, że reszta kodu zasadniczo działa tak, jak mogę uzyskać dostęp do innych informacji dotyczących wiadomości e-mail, po prostu nie uzyskuję dostępu do załącznika.

Czy ktoś mi pokazuje błąd moich metod?

Dzięki z góry,

James

static void Main(string[] args) 
    { 
     ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1); 
     service.Credentials = new NetworkCredential("MYLOGIN", "MYPASSWORD", "MYDOMAIN"); 

     service.Url = new Uri("https://MYMAILSERVER/EWS/Exchange.asmx"); 

     ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; 

     FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(1000)); 

     foreach (Item item in findResults.Items) 
     { 
      if (item.HasAttachments && item.Attachments[0] is FileAttachment) 
      { 
       FileAttachment fileAttachment = item.Attachments[0] as FileAttachment; 
       fileAttachment.Load("C:\\temp\\" + fileAttachment.Name); 
      } 

     } 
    } 
} 

Solved ale nowy problem

Mam klasyfikowane teraz problem poprzez zmianę 'foreach (pozycja elementu w findResults.Items)' do "foreach (element EmailMessage w findResults.Items)", ale teraz muszę się dowiedzieć, jak wyliczyć w załącznikach - czy ktoś ma jakieś pomysły?

+0

Skąd masz wyjątek? Jaki argument jest poza zakresem? Informacje te stanowią zwykle część wyjątku. –

+0

to był błąd z indeksu. Rozwiązałem teraz problem, zmieniając "foreach (element pozycji w findResults.Items)" na "foreach (element EmailMessage w findResults.Items)" –

+0

Część wyjątku "InnerException" może być warta zobaczenia info też –

Odpowiedz

1

O ile nie brakuje mi czegoś oczywistego, wszystko, co musisz zrobić, to przeliczać przez item.Attachments.

Kliknij przycisk here i przewiń w dół do miejsca, w którym znajduje się nagłówek Example.

5

Sprawdź swój profil. Jeśli korzystasz z trybu oświetlenia, załączniki nie są pobierane z wiadomością.

Dodaj następujący wiersz

item.Load() // loads the entire message with attachment 
+1

Dzięki. To był mój problem. –

2

Odpowiedź do nowego problemu jest

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1); 

    //service.Credentials = new NetworkCredential("{Active Directory ID}", "{Password}", "{Domain Name}"); 

    service.AutodiscoverUrl("[email protected]"); 

     FindItemsResults<Item> findResults = service.FindItems(
      WellKnownFolderName.Inbox, 
      new ItemView(10)); 

     foreach (Item item in findResults.Items) 
     { 
      Console.WriteLine(item.Subject); 
      item.Load(); 
      if(item.HasAttachments) 
      { 
       foreach (var i in item.Attachments) 
       { 
        FileAttachment fileAttachment = i as FileAttachment; 
        fileAttachment.Load(); 
        Console.WriteLine("FileName: " + fileAttachment.Name); 

       } 
      } 

     } 
1

Roztwór do pobierania wszystkich załączników z podaną ilością e-maili:

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013); 
    service.Credentials = new NetworkCredential("login", "password"); 

    service.Url = new Uri("https://mail.Yourservername.com/EWS/Exchange.asmx"); 

    ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; 

    FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(10)); 

    if (findResults != null && findResults.Items != null && findResults.Items.Count > 0) 
     foreach (EmailMessage item in findResults) 
     { 
     EmailMessage message = EmailMessage.Bind(service, item.Id, new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments, ItemSchema.HasAttachments)); 
      foreach (Attachment attachment in message.Attachment 
      { 
       if (attachment is FileAttachment) 
        { 
        FileAttachment fileAttachment = attachment as FileAttachment;  
        fileAttachment.Load(@"Folder\file.name"); 
        } 
      } 
     } 

nie zapomnij przekazać poprawną wersję programu Exchange Server do konstruktora ExchangeService.

0

Jest to metoda GetAttachmentsFromEmail, której można użyć do pobrania załączników.

public static void GetAttachmentsFromEmail(ExchangeService service, ItemId itemId) 
    { 
     // Bind to an existing message item and retrieve the attachments collection. 
     // This method results in an GetItem call to EWS. 
     EmailMessage message = EmailMessage.Bind(service, itemId, new PropertySet(ItemSchema.Attachments)); 

     // Iterate through the attachments collection and load each attachment. 
     foreach (Attachment attachment in message.Attachments) 
     { 
      if (attachment is FileAttachment) 
      { 
       FileAttachment fileAttachment = attachment as FileAttachment; 

       // Load the attachment into a file. 
       // This call results in a GetAttachment call to EWS. 
       fileAttachment.Load("C:\\temp\\" + fileAttachment.Name); 

       Console.WriteLine("File attachment name: " + fileAttachment.Name); 
      } 
      else // Attachment is an item attachment. 
      { 
       ItemAttachment itemAttachment = attachment as ItemAttachment; 

       // Load attachment into memory and write out the subject. 
       // This does not save the file like it does with a file attachment. 
       // This call results in a GetAttachment call to EWS. 
       itemAttachment.Load(); 

       Console.WriteLine("Item attachment name: " + itemAttachment.Name); 
      } 
     } 
    } 
Powiązane problemy