4

EDIT #2---- It compiles fine, but I get a debug error of: The URL property on the ExchangeService must be set on this line On This Line of Code 'FindItemsResults findResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(128));' End EDIT #2 ----

EDIT --- Ugh -- I didn't realize I needed 10 rep points to post an image...let me give a few of the errors.

1) Type or namespace 'FindItemsResults' could not be found
2) Type or namespace name 'Item' could not be found
3) The name 'service' does not exist in the current context
4) Type or namespace 'ItemView' could not be found

EDIT ----

I saw the post here -- How to get email body, receipt, sender and CC info using EWS? and was looking at this code sampling

public class MailItem
{
    public string From;
    public string[] Recipients;
    public string Subject;
    public string Body;
}

public MailItem[] GetUnreadMailFromInbox()
{
    FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(128));
    ServiceResponseCollection<GetItemResponse> items =
        service.BindToItems(findResults.Select(item => item.Id), new PropertySet(BasePropertySet.FirstClassProperties, EmailMessageSchema.From, EmailMessageSchema.ToRecipients));
    return items.Select(item =>
    {
        return new MailItem()
        {
            From = ((Microsoft.Exchange.WebServices.Data.EmailAddress)item.Item[EmailMessageSchema.From]).Address,
            Recipients = ((Microsoft.Exchange.WebServices.Data.EmailAddressCollection)item.Item[EmailMessageSchema.ToRecipients]).Select(recipient => recipient.Address).ToArray(),
            Subject = item.Item.Subject,
            Body = item.Item.Body.ToString(),
        };
    }).ToArray();
}

But am getting multiple compile errors. How is a very clear instructions way to read the body of an email using C#?

Community
  • 1
  • 1
Bob Goblin
  • 1,251
  • 3
  • 16
  • 33
  • 2
    post your errors, no one here can read minds so without that info we cannot help you. – Sorceri Oct 09 '13 at 18:02
  • A few error's added to original post. Sorry, I did not realize that it took 10 rep points to post an image. I thought that was with my original post. – Bob Goblin Oct 09 '13 at 18:11
  • I think it should be `FindItems` not `findItems` – Icemanind Oct 09 '13 at 18:16
  • It is set as FindItems in the code. – Bob Goblin Oct 09 '13 at 18:21
  • 1
    This code snippet is not complete for connecting to EWS. service is probably an instance of ExchangeService. A good getting started article is here http://msdn.microsoft.com/en-us/library/jj220499(v=exchg.80).aspx – Jesse Oct 09 '13 at 18:25
  • @Jesse thank you for that link. It shows me how to send an email from C#, but how would I pull the data from the email? – Bob Goblin Oct 09 '13 at 18:37

1 Answers1

1

Think you are missing a reference to MS Exchange assembly Microsoft.Exchange.WebServices.dll

or the using statement using Microsoft.Exchange.WebServices.Data;

For (3) you will need to declare and initialise the service object as shown in the linked question.

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);

Note : You can get the assemblies from Microsoft Exchange Web Services Managed API 2.0

and MSDN docs to get started + Code samples

Amitd
  • 4,769
  • 8
  • 56
  • 82
  • I added the .dll you recommend but it did not remove the error? – Bob Goblin Oct 09 '13 at 18:32
  • Did u put the using statement? `using Microsoft.Exchange.WebServices.Data;` – Amitd Oct 09 '13 at 18:48
  • Yes. It underlines the word "Exchange" in the using statement and tells me 'Exchange' does not exist in the namespace Microsoft. – Bob Goblin Oct 09 '13 at 18:49
  • The framework for the project must be set to 4.0 .. see this answer http://stackoverflow.com/questions/5270484/microsoft-exchange-webservices-is-not-working-in-vs2010 – Amitd Oct 09 '13 at 18:56
  • When I click on the property's for my project and look at the Application Tab it shows my Target Framework is .Net Framework 4 Client Profile. Is that the incorrect framework? – Bob Goblin Oct 09 '13 at 19:01
  • that got it! One other question, how would I call the above code from my 'main' method in my console app so it will run? – Bob Goblin Oct 09 '13 at 19:13
  • Just move the function GetUnreadMailFromInbox and class MailItem in your Program.cs and call the function in main().. see the C# tutorial http://www.csharp-station.com/Tutorial/CSharp/Lesson05 for more – Amitd Oct 09 '13 at 19:21
  • When I try to call the Function I get a compile error of An object reference is requred for GetUnreadMailFromInbox. – Bob Goblin Oct 09 '13 at 19:51
  • That is because main() is static.. u will need to change `public MailItem[] GetUnreadMailFromInbox()` to `public static MailItem[] GetUnreadMailFromInbox()` – Amitd Oct 09 '13 at 19:56