How to access group folders / shared folders from EWS (ExchangeService) in C# - c#

How do I access group folders / shared folders from EWS (ExchangeService) - I can access my own folders - no problem - but I cannot see Mailboxes I have access to such as Team Mailboxes or group mailboxes.
I Can get my own folders this way:
ExchangeService _service = new ExchangeService();
_service.Credentials = new NetworkCredential("MY Username", "My Password");
_service.AutodiscoverUrl("My Email Address",delegate(string x) { return true; });
FolderView view = new FolderView(int.MaxValue);
view.Traversal = FolderTraversal.Shallow;
FindFoldersResults findFolderResults = service.FindFolders(id, view);
The above will only give my OWN stuff. Question is how do I get the rest of the Mailboxes that I have access to, and can see from within Outlook ?

If you know the mailbox name then DistinguishedFolderIdType.Mailbox may be what you're looking for. See this SO post regarding using delegates in EWS.
Here is an example of accessing a shared Exchange Mailbox via FolderId and Mailbox.

Related

How to remove "on behalf of " while sending mail from C# using Microsoft.Office.Interop.Outlook

This is shared Mailbox that is given access to my email for sending and receiving mails. However when I send the mails via outlook from shared mail box I am not getting the "on behalf of" message whereas when I send it from code I am getting as MyEmail.com on behalf of sharedEmail.com.
var app = new Application();
MailItem mailItem = app.CreateItem(OlItemType.olMailItem);
mailItem.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML;
var attachments = mailItem.Attachments;
mailItem.Subject = item.Capability + " - " + appSettings["Subject"].ToString();
mailItem.HTMLBody = CreateBody(appSettings);
mailItem.CC= appSettings["CCEmails"].ToString();
mailItem.Recipients.Add(appSettings["FromAddress"]);
mailItem.Recipients.ResolveAll();
var adressEntry = mailItem.Recipients[mailItem.Recipients.Count].AddressEntry;
mailItem.Recipients.Remove(mailItem.Recipients.Count);
mailItem.Sender = adressEntry;
mailItem.To = item.EmailId;
mailItem.Importance = OlImportance.olImportanceHigh;
mailItem.Send();
ReleaseObj(app);
Following Things I have already Tried.
1.Using SendUsingAccount -This did not work since the mail is configured as shared mailbox and not as shared account so this mailbox is not listing under the accounts.
2.Using SentOnBehalfOfName-This also is not working , I followed one of the stack overflow user workaround approach to set the sender name.
3.Many of the answers are suggesting to enable "Send As" or "Behalf of" Permissions from the admin but if permissions is the issue why I am getting the message "on behalf of " only through code and not via outlook client.
Thanks in advance for help
You have:
mailItem.Sender = adressEntry;
But seem to be missing the From Address, I haven't tried but adding these lines may override the "on behalf of":
EmailAddress fromSender = new EmailAddress();
fromSender.Address = _sharedOutlookMailAccount;
message.From = fromSender;
Ref: How to send email via a Shared MailBox using Exchange Web Services (EWS) API

Exchange API does not return shared calendar which are shared on other domain

I am using EWS Managed exchange api in c#.
When I fetch all calendar using below code, it's not returning me all calendar.
It's only return my calendarm but there is more calendar which are shared by other.
SearchFilter sfSearchFilter = new SearchFilter.IsEqualTo(
FolderSchema.FolderClass, "IPF.Appointment");
FindFoldersResults ffoldres = service.FindFolders(
new FolderId(WellKnownFolderName.Root, "emailId"),
sfSearchFilter, fvFolderView);
We have calendar call as below.
service.FindFolders(new FolderId(WellKnownFolderName.Root, "email")
In this call we pass my email id. (My credential email).
If i need to access other user calendar(Shared calendar). I need to pass other user email address here. like service.FindFolders(new FolderId(WellKnownFolderName.Root, "Other user email").
Service object is created by my credential and just pass email of other user. Now you can get all calendar.

Get Mailbox Permissions using EWS

I'm creating an app that needs to get the permissions for each MailBox, and I can't seem to get it to work. I've noticed in the VS Object Browser that the Permissions property is on the Folder class.
So I'm guessing I need to get the MailBox object and then get the root/default folder so I can get the Permissions.
This is what I've done so far, but when it calls Folder rootfolder = Folder.Bind(service, sharedMailboxRootFolderId); I get the following exception:
"The Client Access Server version does not match the accessed
resource's Mailbox Server version."
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
string exchangeServerWebServicesUrl = "https://example.com/EWS/Exchange.asmx";
service.Url = new Uri(exchangeServerWebServicesUrl);
string username = "*********************";
string password = "*********************";
service.UseDefaultCredentials = false;
service.Credentials = new WebCredentials(username, password);
Mailbox sharedMailbox = new Mailbox("shared#example.com");
FolderId sharedMailboxRootFolderId = new FolderId(WellKnownFolderName.Root, sharedMailbox);
Folder rootfolder = Folder.Bind(service, sharedMailboxRootFolderId);
var permissions = rootfolder.Permissions;
I've tried passing in different ExchangeVersion enum values, but they don't work either. But passing ExchangeVersion.Exchange2007_SP1 does work when I try and get the Public Folders root folder.
Folder rootfolder = Folder.Bind(service, WellKnownFolderName.PublicFoldersRoot);
So the question is how can I get a MailBoxes permissions using EWS?
I've just found the Find out which users have Full Access on a mailbox post that says
You can't using EWS (or any of the Exchange Mailbox API's) you can
only access the Folder level DACL's what you need to read is the
Mailbox DACL which can only be either accessed via the Exchange
Management Shell (Get-MailboxPermissions) or via reading the
msexchmailboxsecuritydescriptor from Active Directory.
So it looks like it's not possible to get the MailBox permissions using EWS.
Shame.

ExchangeService: connecting without credentials, how to retrieve user information?

I am analyzing a users Exchange mailbox with calls to the ExchangeService. This tool needs to run on the client environment periodically and by ommiting the credentials to the service I am connecting to the Exchange Service as the logged in Windows User. I can succesfully loop thrue the folders and items.
Now I want tot retrieve the information about the mailbox being used. Username and (main) E-mail should suffice. But I cannot find anything about how to retrieve this information. Every example provides credentails for the user, or auto-discovering the Exchange service from the e-mail adres. I do not want the user to configure anything :-).
Any suggestions?
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
service.Url = new Uri("https://FQDN/EWS/Exchange.asmx");
???
FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.SentItems, new ItemView(100)); // this works
I've tried using service.ResolveName, but that can give multiple answers, even using Environment.UserName
The easiest method to do this is to use ConvertId operation and use unresolvable address (blah#blah.com always works for me) in the Mailbox element. Exchange should convert this to the actual Mailbox in the response. eg
Folder chk = Folder.Bind(service, WellKnownFolderName.Inbox);
AlternateId aiItem = new AlternateId();
aiItem.Mailbox = "Blah#Blah.com";
aiItem.UniqueId = chk.Id.UniqueId;
aiItem.Format = IdFormat.EwsId;
String CasServer = service.Url.Host.ToString();
AlternateIdBase caid = service.ConvertId(aiItem, IdFormat.HexEntryId);
Console.WriteLine(((AlternateId)caid).Mailbox);
Cheers
Glen

EWS Get mailbox type (user/resource/shared)

Given just an email address, I connect to the Mailbox that receives the emails sent to that address:
ews.autodiscoverUrl(email);
var m = new Mailbox(email);
var folderId = new FolderId(WellKnownFolderName.Inbox, m);
var folder = Folder.Bind(ews,folderId);
Is there a way find out what kind of mailbox I opened - whether it is a user, a resource or a shared mailbox?
You can find the mailbox type given the email address by calling ResolveName:
var mailboxes = ews.ResolveName(email);
if (mailboxes.Any()) {
var mailbox = mailboxes.First();
if (mailbox.MailboxType == MailboxType.PublicFolder) {
// your magic
}
}
See this MSDN page for the possible values of the MailboxType.
Update
There is no distinction between a regular mailbox and the mailbox from a room. If you want to know what rooms exist in an Exchange server, you can use the EWS GetRoomLists and GetRooms functions.
By combining the snippet above with GetRoomLists/GetRooms, you can get the mailbox for a room. But keep in mind: there may be rooms without a mailbox.
Update 2
The approach using GetRoomLists/GetRooms is the only one that will allow you to identify a room's mailbox using EWS. But it does require that you have the rooms added to a room list, which may require you to bribe an administrator or two.
The alternative is to look up the relevant information in Active Directory, which is apparently what Outlook does. See this MSDN thread for a more elaborate explanation, but this is the gist:
Outlook has its own directory interfaces it doesn't use EWS to get this information. If your going to use only EWS you need to get your administrator to create a roomlist for you to use.
The last alternative is to use PowerShell to get the mailbox type. This is what I just used to get the rooms in my Exchange Online server:
PS H:\> Get-Mailbox | Where {$_.ResourceType -eq "Room"}
The output:
Name Alias ServerName ProhibitSendQuota
---- ----- ---------- -----------------
Frank's room franksroom db3pr03mb058 49.5 GB (53,150,220,288 bytes)
Another example that generates the same output:
PS H:\> Get-Mailbox -Filter '(RecipientTypeDetails -eq "RoomMailBox")'
With this output I can then get the mailbox with either of these:
exchange.ResolveName("Frank's room");
exchange.ResolveName("franksroom#myorganization.onmicrosoft.com");
A few relevant links for that:
PowerShell commands for Room mailbox management
Connect to Exchange Online using PowerShell

Categories

Resources