ServiceResponseException: The specified object was not found in the store - c#

I am trying to upgrade an application that uses WebDAV against Exchange 2003 to return a responseXML, then it creates cases on SalesForce CRM (using web service wsdl) and puts the attachments from the emails on the cases.
We are moving to Exchange 2010 SP2 so I need to access the inbox using EWS.
I am getting a ServiceResponseException error "The specified object was not found in the store."
Here is my code:
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);// .Exchange2007_SP1);
List<SearchFilter> searchFilterCollection = new List<SearchFilter>();
searchFilterCollection.Add(new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false)));
searchFilterCollection.Add(new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.HasAttachments,true)));
SearchFilter searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.Or, searchFilterCollection.ToArray());
//creates an object that will represent the desired mailbox
Mailbox mb = new Mailbox(#"bbtest#domain");
ItemView view = new ItemView(1);
//creates a folder object that will point to inbox folder
FolderId fid = new FolderId(WellKnownFolderName.Inbox, mb);
service.Url = new Uri("https://domain/EWS/Exchange.asmx");
//this will bind the mailbox you're looking for using your service instance
Microsoft.Exchange.WebServices.Data.Folder inbox = Microsoft.Exchange.WebServices.Data.Folder.Bind(service, fid);
FindItemsResults<Item> results = service.FindItems(WellKnownFolderName.Inbox, searchFilter, view);
foreach (EmailMessage email in results)
{
Debug.Print(email.From.ToString());
Debug.Print(email.DisplayTo);
Debug.Print(email.Subject);
}
it throws the error on this line:
Microsoft.Exchange.WebServices.Data.Folder inbox = Microsoft.Exchange.WebServices.Data.Folder.Bind(service, fid);
what am I doing wrong and how can I fix this please?
Also, is there no way to get EWS to return similar XML responseStream as webDAV?

In my experience, NotFound can be returned when you do not have permission to access the object in the Exchange store. Make sure that whatever credentials you use had full mailbox access rights to the target MB. (I suppose if all you want to do is read items, you might need fewer permissions, but you can trim later on if needed.) I don't see you setting any credentials in the above, so presumably you are accessing EWS with your default creds, i.e. what you're logged in as when running this.
And no, you cannot get a WebDAV response stream from EWS: they are totally different services, and as I'm sure you're aware, WebDAV does not exist after E2007.

Related

Microsoft.Exchange.WebServices.Data.ServiceResponseException: Unable to access an account or mailbox

Sidenote 1: I've seen this topic come by a few times before, but most > are unresolved and the rest is not really related to my problem.
Sidenote 2: Don't get confused by the usage of the parameters, I did some adjustments in the code so it would fit in SO. The parameters are not the issue here.
I'm trying to develop a way for me to read emails out of a functional mailbox. At this point I want to get access to the mailboxes in general, so I'm seeing if I can read the mails in my own mailbox.
However something seems to be wrong. As soon as it tries to run the service.FindItems(inbox, fView) method, it'll give me the following error message:
Microsoft.Exchange.WebServices.Data.ServiceResponseException: Unable to access an account or mailbox.
At this point I'm not sure whether it has to do with credentials, or with access rights. Below is the code I'm using.
I'm triggering a helper method I created:
FindItemsResults<Item> items = ExchangeWebServiceHelper.GetEmailsFromFolder(inboxName);
In the ExchangeWebServiceHelper I have the following two methods:
public static ExchangeService CreateConnection()
{
string url = WebConfigurationManager.AppSettings["EWSAsmxUrlNp"];
ServicePointManager.ServerCertificateValidationCallback = delegate(
Object obj,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors errors)
{
return true;
};
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
service.Url = new Uri(url);
service.UseDefaultCredentials = true;
return service;
}
public static FindItemsResults<Item> GetEmailsFromFolder(Mailbox mailbox)
{
ExchangeService service = CreateConnection();
FolderView fView = new FolderView(100);
fView.PropertySet = new PropertySet(BasePropertySet.IdOnly, FolderSchema.DisplayName);
FolderId inbox = new FolderId(WellKnownFolderName.Inbox);
FindItemsResults<Item> results = service.FindItems(inbox, fView);
Logger.Log(mailbox.ToString());
Logger.Log(results.TotalCount.ToString());
return results;
}
According to Microsoft Dev Center, you cannot use .UseDefaultCredentials = true for Exchange OnLine.
Remarks
Setting the UseDefaultCredentials property to true automatically sets
the Credentials property to a null reference (Nothing in Visual
Basic).
You cannot use the default credentials of the logged on user if the user’s mailbox is hosted in Exchange Online or Exchange Online as part
of Office 365. Instead, use the Credentials property to set the
user’s credentials. The user’s credentials must be in user principal
name (UPN) form for Exchange Online.

Accessing a Resource Calendar with no mailbox via EWS and C#

Our Exchange Admins (Exchange 2010 SP1) have setup a shared resource calendar. There is no mailbox assigned to this resource calendar. I want to be able to read the meetings using EWS and C#.
Snippet:
ExchangeService esvc = new ExchangeService(ExchangeVersion.Exchange2010);
esvc.Credentials = new WebCredentials(username, password, "ourplace.org");
esvc.Url = new Uri("https://OWA.OURPLACE.ORG/EWS/Exchange.asmx");
FolderId shareFolderId = new FolderId(WellKnownFolderName.Calendar, "Shared Calendar Name");
CalendarFolder.Bind(esvc, shareFolderId);
the bind statement throws the error: "The SMTP address has no mailbox associated with it."
How can I read the items on a Share Resource Calendar that has no associated mailbox... or is it even possible?
Thanks !!
Bind to that Calendar with a mail-Adress
Create first of all a FolderId:
FolderId parkplatzCalendarId = new FolderId(WellKnownFolderName.Calendar,"de.calendar.name#company.com");
Then bind to this one:
CalendarFolder calendar = CalendarFolder.Bind(_service, parkplatzCalendarId);
Now you can use this calendar!
CalendarView cView = new CalendarView(start, end, int.MaxValue);
cView.PropertySet = new PropertySet(AppointmentSchema.Subject, AppointmentSchema.Start, AppointmentSchema.End, AppointmentSchema.Duration, AppointmentSchema.LastModifiedName, AppointmentSchema.Organizer, AppointmentSchema.Categories);
FindItemsResults<Appointment> appointments = calendar.FindAppointments(cView);
With something like that ;D
If the calendar is actually not in any particular mailbox, then it should be in a public folder, and you should look in a subfolder WellKnownFolderName.PublicFoldersRoot.
Otherwise, please tell where exactly does it appear in Outlook folders hierarchy.

EWS Managed API: Searching an Inbox other than the main mailbox associated with Windows login

I have an application extracting emails from 'User A', with the email address UserA#email.com. I use the following code to do so:
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);
service.UseDefaultCredentials = true;
service.Url = new Uri(ServerName);
FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(500));
This works fine if User A is logged in and runs the application.
However, lets say 'User B' has been granted access to User A's inbox and wants to run the same application to extract files from User A's Inbox. How would I change the code above to specify the email account inbox?
I know that I could hardcode the login details of User A when getting the credentials, but ideally I would avoid having hardcoded usernames but instead use the credentials of the user running the application.
This may just be my lack of understanding, but I'm relatively new to .net and very new to EWS. Any pointers would be much appreciated!
Thanks
Delegation is what I needed:
FolderId InboxId = new FolderId(WellKnownFolderName.Inbox, "UserA#email.com");
FindItemsResults<Item> findResults = service.FindItems(InboxId, new ItemView(500));

Wrong mailbox items being retrieved using Exchange Web Services managed API in C#

I'm trying to retrieve Inbox items from a specific mailbox (in which i have permissions), using Exchange Web Services managed API. I've tested the code first using my own email address via AutodiscoverUrl, and it works fine. However when i tried using the other email address, EWS still retrieves my own inbox items. Is this due to a cache or something?
My code is as follows:
ExchangeService ex = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
ex.AutodiscoverUrl("someothermailbox#company.com");
FindItemsResults<Item> findResults = ex.FindItems(WellKnownFolderName.Inbox, new ItemView(10));
foreach (Item item in findResults.Items)
Console.WriteLine(item.Subject);
The e-mail address given to AutodiscoverUrl has nothing to do with which mailbox you are binding to.
There are (at least) two ways to get the inbox items from another users mailbox: Delegate access and impersonation.
If you have delegate access to the other users mailbox, you can specify the mailbox as a parameter in the call to FindItems:
FindItemsResults<Item> findResults = ex.FindItems(
new FolderId(WellKnownFolderName.Inbox, new Mailbox("someothermailbox#company.com")),
new ItemView(10));
If you have the permissions to impersonate the other user, you can impersonate the other user when connecting to the EWS and the following call to FindItem will work on the inbox of the impersonated user:
ExchangeService ex = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
ex.AutodiscoverUrl("someothermailbox#company.com");
ex.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "someothermailbox#company.com");
ItemsResults<Item> findResults = ex.FindItems(WellKnownFolderName.Inbox, new ItemView(10));
Disclaimer: I have written the above code without actually testing it on a real Exchange server.
if you want to send email using only delegates permission save the email first before sending it. it will set the smtp address that is required to send the message.
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
service.Credentials = new WebCredentials("user1", "1234", "domain.com");
service.AutodiscoverUrl("user2#domain.com");
EmailMessage email = new EmailMessage(service);
email.ToRecipients.Add("user2#domain.com");
email.Subject = "HelloWorld";
email.Body = new MessageBody("Sent by using the EWS Managed API");
//save it first!
email.Save(new FolderId(WellKnownFolderName.Drafts, "user1#domain.com"));
email.Send();
i used it to avoid this error:
"When making a request as an account that does not have a mailbox, you must specify the mailbox primary SMTP address for any distinguished folder Ids."

Exchange Server Login Credentials Setup

I just want to pass this by you and make sure that I am doing this right. My code:
ExchangeService _mailService = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
_mailService.Credentials = new System.Net.NetworkCredential(#"userName", #"password", #"mydomainInfo");
//_mailService.UseDefaultCredentials = true;
_mailService.Url = new Uri("https://webmail.mydomain.com/ews/exchange.asmx");
try
{
ItemView allItems = new ItemView(100);
SearchFilter searchFilterInbox = new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false);
Folder _inbox = Folder.Bind(_mailService, WellKnownFolderName.Inbox);
// <SNIP>
So I get this far and the Bind returns an Authentication Fail (401). If I comment out the '.Credentials' and uncomment '.UseDefaultCredentials', I log in just fine. But I need to access a different inbox folder. I pulled the UserName and Domain Name from my account on Outlook. Even if I use my own login information, I still get this error. To me that says I'm just not providing the right information. Thoughts? I can log into the web side of the service just fine.
Well I solved my own problem. It was what I was suspecting. I just didn't have the correct data in the "domain" and "username" fields. I was actually calling the server the Exchange Server was on. So I changed it to the domain that my computer is logged into and changed the user id from "me#email.com" to "me" and it works! So I had it all setup right, just wrong information.

Categories