![](/img/trans.png)
[英]C# How to access a shared outlook/exchange mailbox programmatically from a C# console application to move emails from inbox to another folder
[英]Fetch emails received on certain day from non-Well-known folder in shared inbox, Exchange 2016 EWS in C#
我正在嘗試使用C#中的EWS托管API從用戶指定的日期在Exchange 2016服務器上獲取所有消息。
我通過以下方式進行身份驗證:
public static void Login(string username, string password)
{
service.UseDefaultCredentials = false;
service.Credentials = new WebCredentials(username, password);
service.AutodiscoverUrl(username, RedirectionUrlValidationCallback);
}
然后使用
sharedMailbox = new Mailbox(Properties.Settings.Default.Inbox);
SMTP地址存儲在Settings.settings中。
然后,使用以下命令(從此線程 )找到所需的文件夾:
targetFolderId = new FolderId(WellKnownFolderName.Inbox, sharedMailbox);
// set folder view
view.PropertySet = new PropertySet(BasePropertySet.FirstClassProperties);
view.PropertySet.Add(FolderSchema.DisplayName);
view.Traversal = FolderTraversal.Deep;
folderResults = service.FindFolders(WellKnownFolderName.Inbox, view);
foreach(Folder f in folderResults)
{
if(f.DisplayName == "Invoices")
{
targetFolderId = f.Id;
//tried showing a message box here
}
}
和(從過濾器代碼中使用下列這里 ,並從Exchange服務器代碼檢索細節這里 )來得到我想要的信息:
public static void FetchUnreadMessages(DateTime searchDate)
{
SearchFilter greaterthanfilter = new SearchFilter.IsGreaterThanOrEqualTo(ItemSchema.DateTimeReceived, searchDate);
SearchFilter lessthanfilter = new SearchFilter.IsLessThan(ItemSchema.DateTimeReceived, searchDate.AddDays(1));
SearchFilter dayFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, greaterthanfilter, lessthanfilter);
results = service.FindItems(targetFolderId, dayFilter, view);
foreach(var item in results.Items)
{
emails.Add((EmailMessage)item);
}
PropertySet properties = (BasePropertySet.FirstClassProperties);
service.LoadPropertiesForItems(emails, properties);
}
我不確定這在哪里崩潰。 我嘗試在foreach循環中顯示一個消息框,該消息框查找具有指定名稱的文件夾,但似乎從未找到該文件夾。 我知道共享收件箱中有一個顯示名稱的文件夾。
我不太擅長調試,很遺憾,我對EWS API的掌握非常不穩定。 關於我所缺少的任何建議都歡迎。
我將與收件箱相關的所有內容都保存在一個靜態類中,因此我只需要擔心一個實例。
您的代碼優先出現問題
然后使用sharedMailbox = new Mailbox(Properties.Settings.Default.Inbox);選擇適當的收件箱。 targetFolderId = new FolderId(WellKnownFolderName.Inbox,sharedMailbox);
這段代碼沒什么錯,但是您可能需要了解這里發生的事情。 這段代碼不會對服務器進行任何調用,它只是設置一個FolderId類,您可以在調用中使用它來獲取一個眾所周知的文件夾。
view.PropertySet = new PropertySet(BasePropertySet.FirstClassProperties); view.PropertySet.Add(FolderSchema.DisplayName); view.Traversal = FolderTraversal.Deep; folderResults = service.FindFolders(WellKnownFolderName.Inbox, view);
此代碼將僅搜索您正在使用其憑據的郵箱的“收件箱”子文件夾。 例如
service.Credentials =新的WebCredentials(用戶名,密碼);
如果要搜索SharedMailbox收件箱的子文件夾,請使用
view.PropertySet = new PropertySet(BasePropertySet.FirstClassProperties);
view.PropertySet.Add(FolderSchema.DisplayName);
view.Traversal = FolderTraversal.Deep;
folderResults = service.FindFolders(targetFolderId , view);
因為您正在FindFolder操作中使用targetFolderId,該操作告訴Exchange搜索共享郵箱,而不是與您使用的憑據關聯的郵箱。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.