读取电子邮件 Outlook 365 文件夹 - EWS
Read Email Outlook 365 folder - EWS
我想访问我之前在 Microsoft Exchange 中创建的名为“EIMS”的新文件夹中的电子邮件。
我只知道如何访问收件箱,但我无法在特定文件夹中访问。
这是我的 C# 代码:
static void GetInboxMail(string emailAddress, string pass)
{
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
service.Credentials = new NetworkCredential(emailAddress, pass);
service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
if (service != null)
{
FindItemsResults<Item> result = service.FindItems(WellKnownFolderName.Inbox, new ItemView(100));
foreach (Item item in result)
{
EmailMessage message = EmailMessage.Bind(service, item.Id);
string body = message.Body.Text;
string from = message.From.Name.ToString();
string subject = message.Subject.ToString();
Console.WriteLine("Email Sender :" + from);
Console.WriteLine("Email Body" + body);
Console.WriteLine("Email Subject" + subject);
}
}
}
有什么建议可以添加到我的代码中吗?
您需要先找到 TargetFolders FolderId,然后才能对该文件夹进行搜索。人们可以通过几种不同的方式来执行此操作,例如 Get to an Exchange folder by path using EWS
中概述了一些
我经常使用的方法是输入路径,然后进行浅搜索以找到目标文件夹,例如 GetFolderFromPath(service,"mailbox@domaim.com","\Inbox\folder")
internal static Folder GetFolderFromPath(ExchangeService service,String MailboxName,String FolderPath)
{
FolderId folderid = new FolderId(WellKnownFolderName.MsgFolderRoot,MailboxName);
Folder tfTargetFolder = Folder.Bind(service,folderid);
PropertySet psPropset = new PropertySet(BasePropertySet.FirstClassProperties);
String[] fldArray = FolderPath.Split('\');
for (Int32 lint = 1; lint < fldArray.Length; lint++) {
FolderView fvFolderView = new FolderView(1);
fvFolderView.PropertySet = psPropset;
SearchFilter SfSearchFilter = new SearchFilter.IsEqualTo(FolderSchema.DisplayName,fldArray[lint]);
FindFoldersResults findFolderResults = service.FindFolders(tfTargetFolder.Id,SfSearchFilter,fvFolderView);
if (findFolderResults.TotalCount > 0){
foreach(Folder folder in findFolderResults.Folders){
tfTargetFolder = folder;
}
}
else{
tfTargetFolder = null;
break;
}
}
if (tfTargetFolder != null)
{
return tfTargetFolder;
}
else
{
throw new Exception("Folder Not found");
}
}
我想访问我之前在 Microsoft Exchange 中创建的名为“EIMS”的新文件夹中的电子邮件。
我只知道如何访问收件箱,但我无法在特定文件夹中访问。
这是我的 C# 代码:
static void GetInboxMail(string emailAddress, string pass)
{
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
service.Credentials = new NetworkCredential(emailAddress, pass);
service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
if (service != null)
{
FindItemsResults<Item> result = service.FindItems(WellKnownFolderName.Inbox, new ItemView(100));
foreach (Item item in result)
{
EmailMessage message = EmailMessage.Bind(service, item.Id);
string body = message.Body.Text;
string from = message.From.Name.ToString();
string subject = message.Subject.ToString();
Console.WriteLine("Email Sender :" + from);
Console.WriteLine("Email Body" + body);
Console.WriteLine("Email Subject" + subject);
}
}
}
有什么建议可以添加到我的代码中吗?
您需要先找到 TargetFolders FolderId,然后才能对该文件夹进行搜索。人们可以通过几种不同的方式来执行此操作,例如 Get to an Exchange folder by path using EWS
中概述了一些我经常使用的方法是输入路径,然后进行浅搜索以找到目标文件夹,例如 GetFolderFromPath(service,"mailbox@domaim.com","\Inbox\folder")
internal static Folder GetFolderFromPath(ExchangeService service,String MailboxName,String FolderPath)
{
FolderId folderid = new FolderId(WellKnownFolderName.MsgFolderRoot,MailboxName);
Folder tfTargetFolder = Folder.Bind(service,folderid);
PropertySet psPropset = new PropertySet(BasePropertySet.FirstClassProperties);
String[] fldArray = FolderPath.Split('\');
for (Int32 lint = 1; lint < fldArray.Length; lint++) {
FolderView fvFolderView = new FolderView(1);
fvFolderView.PropertySet = psPropset;
SearchFilter SfSearchFilter = new SearchFilter.IsEqualTo(FolderSchema.DisplayName,fldArray[lint]);
FindFoldersResults findFolderResults = service.FindFolders(tfTargetFolder.Id,SfSearchFilter,fvFolderView);
if (findFolderResults.TotalCount > 0){
foreach(Folder folder in findFolderResults.Folders){
tfTargetFolder = folder;
}
}
else{
tfTargetFolder = null;
break;
}
}
if (tfTargetFolder != null)
{
return tfTargetFolder;
}
else
{
throw new Exception("Folder Not found");
}
}