Exchange Web 服务 (EWS) C# - 获取类别列表
Exchange Web Services (EWS) C# - Get list of categories
我可以使用 item.Categories、
为邮箱的每个项目获取一个类别
有谁知道是否可以在我的邮箱中获取唯一的类别列表?
我认为这可能是不可能的,但如果有人以前做到过,我将不胜感激。
类别列表作为隐藏对象存储在默认日历文件夹中。您可以使用配置名称 "CategoryList"(其 XML 属性)的 GetUserConfiguration 请求来请求它。这在 Exchange 2010 及更高版本上对我有用。
抱歉,我无法提供任何代码示例,但我不懂 C#。
由于@frangge 建议使用以下代码,我最近有同样的要求找到了我正在寻找的东西,包括解决一个奇怪的错误,在某些响应中有一些不可预测的前导字符。
using Flurl;
using Microsoft.Exchange.WebServices.Data;
...
public Categories GetCategories()
{
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
string ewsUrl = RootUrl.AppendPathSegment("EWS/Exchange.asmx");
ExchangeService ewsservice = new Microsoft.Exchange.WebServices.Data.ExchangeService
{
Credentials = new System.Net.NetworkCredential(Username, Password),
Url = new Uri(ewsUrl)
};
UserConfiguration usrConfig = UserConfiguration.Bind(ewsservice,
"CategoryList",
WellKnownFolderName.Calendar,
UserConfigurationProperties.All);
byte[] xmlData = usrConfig.XmlData;
//found some nasty leading chars in XmlData which was breaking deserialization
//it looked something like ï»À<?xml version... o worse an unprintable char
if (usrConfig.XmlData[0].Equals(239) && usrConfig.XmlData[1].Equals(187) && usrConfig.XmlData[2].Equals(191))
{
xmlData = usrConfig.XmlData.Skip(3).ToArray();
}
string xml = Encoding.UTF8.GetString(xmlData);
XmlSerializer serializer = new XmlSerializer(typeof(Categories));
using (StringReader reader = new StringReader(xml))
{
var categories = (Categories)serializer.Deserialize(reader);
return categories;
}
}
类别定义为
[XmlRoot(ElementName = "categories", Namespace = "CategoryList.xsd")]
public class Categories
{
[XmlElement(ElementName = "category")]
public List<Category> Category { get; set; }
[XmlAttribute(AttributeName = "default")]
public string Default { get; set; }
[XmlAttribute(AttributeName = "lastSavedSession")]
public int LastSavedSession { get; set; }
[XmlAttribute(AttributeName = "lastSavedTime")]
public DateTime LastSavedTime { get; set; }
[XmlAttribute(AttributeName = "xmlns")]
public string Xmlns { get; set; }
}
我可以使用 item.Categories、
为邮箱的每个项目获取一个类别有谁知道是否可以在我的邮箱中获取唯一的类别列表?
我认为这可能是不可能的,但如果有人以前做到过,我将不胜感激。
类别列表作为隐藏对象存储在默认日历文件夹中。您可以使用配置名称 "CategoryList"(其 XML 属性)的 GetUserConfiguration 请求来请求它。这在 Exchange 2010 及更高版本上对我有用。
抱歉,我无法提供任何代码示例,但我不懂 C#。
由于@frangge 建议使用以下代码,我最近有同样的要求找到了我正在寻找的东西,包括解决一个奇怪的错误,在某些响应中有一些不可预测的前导字符。
using Flurl;
using Microsoft.Exchange.WebServices.Data;
...
public Categories GetCategories()
{
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
string ewsUrl = RootUrl.AppendPathSegment("EWS/Exchange.asmx");
ExchangeService ewsservice = new Microsoft.Exchange.WebServices.Data.ExchangeService
{
Credentials = new System.Net.NetworkCredential(Username, Password),
Url = new Uri(ewsUrl)
};
UserConfiguration usrConfig = UserConfiguration.Bind(ewsservice,
"CategoryList",
WellKnownFolderName.Calendar,
UserConfigurationProperties.All);
byte[] xmlData = usrConfig.XmlData;
//found some nasty leading chars in XmlData which was breaking deserialization
//it looked something like ï»À<?xml version... o worse an unprintable char
if (usrConfig.XmlData[0].Equals(239) && usrConfig.XmlData[1].Equals(187) && usrConfig.XmlData[2].Equals(191))
{
xmlData = usrConfig.XmlData.Skip(3).ToArray();
}
string xml = Encoding.UTF8.GetString(xmlData);
XmlSerializer serializer = new XmlSerializer(typeof(Categories));
using (StringReader reader = new StringReader(xml))
{
var categories = (Categories)serializer.Deserialize(reader);
return categories;
}
}
类别定义为
[XmlRoot(ElementName = "categories", Namespace = "CategoryList.xsd")]
public class Categories
{
[XmlElement(ElementName = "category")]
public List<Category> Category { get; set; }
[XmlAttribute(AttributeName = "default")]
public string Default { get; set; }
[XmlAttribute(AttributeName = "lastSavedSession")]
public int LastSavedSession { get; set; }
[XmlAttribute(AttributeName = "lastSavedTime")]
public DateTime LastSavedTime { get; set; }
[XmlAttribute(AttributeName = "xmlns")]
public string Xmlns { get; set; }
}