Quantcast
Channel: Exchange Server Development forum
Viewing all articles
Browse latest Browse all 7132

Fetching all mails in Inbox from Exchange Web Services Managed API and storing them as a .eml files

$
0
0

I want to fetch all mails in the Inbox folder using EWS Managed API and store them as .eml. I can store file once I get the file content as a byte[] will not be difficult, as I can do:

File.WriteAllBytes("c:\\mails\\"+mail.Subject+".eml",content);

The problem will be to fetch (1) all mails with (2) all headers (like from, to, subject) (I am keeping information of those values of fromto and other properties somewhere else, so I need them too) and (3)byte[] EmailMessage.MimeContent.Content. Actually I am lacking understanding of

  • Microsoft.Exchange.WebServices.Data.ItemView,
  • Microsoft.Exchange.WebServices.Data.BasePropertySet and
  • Microsoft.Exchange.WebServices.Data.ItemSchema

thats why I am finding it difficult.

My primary code is:

When I create PropertySet as follows:

PropertySet properties = new PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.MimeContent);

I get following exception:

The property MimeContent can't be used in FindItem requests.

I dont understand

(Q1) What these ItemSchema and BasePropertySet are

(Q2) And how we are supposed to use them

So I removed ItemSchema.MimeContent:

PropertySet properties = new PropertySet(BasePropertySet.FirstClassProperties);

I wrote simple following code to get all mails in inbox:

ItemView view = new ItemView(50);
view.PropertySet = properties;
FindItemsResults<Item> findResults; 
List<EmailMessage> emails = new List<EmailMessage>();

do
{    
    findResults = service.FindItems(WellKnownFolderName.Inbox, view);
    foreach (var item in findResults.Items)
    {
        emails.Add((EmailMessage)item);
    }
    Console.WriteLine("Loop");
    view.Offset = 50;
}
while (findResults.MoreAvailable);

Above I kept page size of ItemView to 50, to retrieve no more than 50 mails at a time, and then offsetting it by 50 to get next 50 mails if there are any. However it goes in infinite loop and continuously prints Loop on console. So I must be understanding pagesize and offset wrong. I want to understand

(Q3) what pagesizeoffset and offsetbasepoint in ItemView constructor means

(Q4) how they behave and

(Q5) how to use them to retrieve all mails in the inbox

I didnt found any article online nicely explaining these but just giving code samples. Will appreciate question-wise explanation despite it may turn long.



Viewing all articles
Browse latest Browse all 7132

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>