上一篇介绍的Exchange2007客户端程序中怎么发送邮件。
现在,我来简单介绍一下怎么收取邮件。
来看代码:
现在,我来简单介绍一下怎么收取邮件。
来看代码:
public
Hashtable GetAllMails(DateTime StartDate, DateTime EndDate)
{
try
{
if (null == this.m_esb) return null;
DistinguishedFolderIdType[] dfit = new DistinguishedFolderIdType[1];
//get the root folder ID
dfit[0] = new DistinguishedFolderIdType();
dfit[0].Id = DistinguishedFolderIdNameType.inbox;
//set the props that we want to retrieve
FolderResponseShapeType frst = new FolderResponseShapeType();
frst.BaseShape = DefaultShapeNamesType.AllProperties;
//get the folder
GetFolderType gftRoot = new GetFolderType();
gftRoot.FolderIds = dfit;
gftRoot.FolderShape = frst;
GetFolderResponseType gfrt = this.m_esb.GetFolder(gftRoot);
FolderInfoResponseMessageType firmt = ((FolderInfoResponseMessageType)gfrt.ResponseMessages.Items[0]);
FolderIdType[] fita = new FolderIdType[1];
fita[0] = new FolderIdType();
if (firmt.ResponseClass == ResponseClassType.Success)
{
fita[0].Id = ((FolderInfoResponseMessageType)gfrt.ResponseMessages.Items[0]).Folders[0].FolderId.Id;
}
if (fita[0].Id == null) return new Hashtable();
//request AllProperties
PathToUnindexedFieldType[] ptufta = new PathToUnindexedFieldType[1];
ptufta[0] = new PathToUnindexedFieldType();
ptufta[0].FieldURI = UnindexedFieldURIType.itemItemClass;
ItemResponseShapeType irst = new ItemResponseShapeType();
irst.BaseShape = DefaultShapeNamesType.AllProperties;
irst.IncludeMimeContent = false;
irst.AdditionalProperties = ptufta;
//restrict the returned items to just items of a specific message class
PathToUnindexedFieldType MsgClassField = new
PathToUnindexedFieldType();
MsgClassField.FieldURI = UnindexedFieldURIType.itemItemClass;
ConstantValueType MsgClassToGet = new ConstantValueType();
MsgClassToGet.Value = "IPM.NOTE";
FieldURIOrConstantType MsgClassConstant = new FieldURIOrConstantType();
MsgClassConstant.Item = MsgClassToGet;
IsEqualToType iett = new IsEqualToType();
iett.FieldURIOrConstant = MsgClassConstant;
iett.Item = MsgClassField;
//restrict the returned items greater than a specified date
PathToUnindexedFieldType StartDateReceivedField = new PathToUnindexedFieldType();
StartDateReceivedField.FieldURI = UnindexedFieldURIType.itemDateTimeReceived;
ConstantValueType StartDateReceivedToGet = new ConstantValueType();
StartDateReceivedToGet.Value = StartDate.ToUniversalTime().ToString();
FieldURIOrConstantType StartDateReceivedConstant = new FieldURIOrConstantType();
StartDateReceivedConstant.Item = StartDateReceivedToGet;
IsGreaterThanOrEqualToType igtett = new IsGreaterThanOrEqualToType();
igtett.FieldURIOrConstant = StartDateReceivedConstant;
igtett.Item = StartDateReceivedField;
//restrict the returned items less than a specified date
PathToUnindexedFieldType EndDateReceivedField = new PathToUnindexedFieldType();
EndDateReceivedField.FieldURI = UnindexedFieldURIType.itemDateTimeReceived;
ConstantValueType EndDateReceivedToGet = new ConstantValueType();
EndDateReceivedToGet.Value = EndDate.ToUniversalTime().ToString();
FieldURIOrConstantType EndDateReceivedConstant = new FieldURIOrConstantType();
EndDateReceivedConstant.Item = EndDateReceivedToGet;
IsLessThanOrEqualToType iltett = new IsLessThanOrEqualToType();
iltett.FieldURIOrConstant = EndDateReceivedConstant;
iltett.Item = EndDateReceivedField;
AndType at = new AndType();
at.Items = new SearchExpressionType[3];
at.Items[0] = igtett;
at.Items[1] = iltett;
at.Items[2] = iett;
//TODO: Uncomment the following line if you want to display only email items from folder
// If the following line is commented then all items would be returned from folder,
// for e.g. Reports, Appointment, Meeting Requests, Contacts etc.
RestrictionType rt = new RestrictionType();
rt.Item = at;
//find the items
FindItemType fit = new FindItemType();
fit.ItemShape = irst;
fit.ParentFolderIds = fita;
fit.Restriction = rt;
FindItemResponseType firt = m_esb.FindItem(fit);
// That's it!
ItemType[] list = ((ArrayOfRealItemsType)((FindItemResponseMessageType)firt.ResponseMessages.Items[0]).RootFolder.Item).Items; // Emails We want to.
//foreach (ItemType i in list)
//{
// SetIsReadFlag(i.ItemId);//set is read flag.
//}
Hashtable ht = new Hashtable();
if (list != null)
{
foreach (ItemType itemOne in list)
{
ItemType Item = new ItemType();
ht.Add(Item.ItemId, Item);
}
}
return ht;
}
catch (Exception e)
{
throw new Exception("Error :" + e.Message);
}
}
{
try
{
if (null == this.m_esb) return null;
DistinguishedFolderIdType[] dfit = new DistinguishedFolderIdType[1];
//get the root folder ID
dfit[0] = new DistinguishedFolderIdType();
dfit[0].Id = DistinguishedFolderIdNameType.inbox;
//set the props that we want to retrieve
FolderResponseShapeType frst = new FolderResponseShapeType();
frst.BaseShape = DefaultShapeNamesType.AllProperties;
//get the folder
GetFolderType gftRoot = new GetFolderType();
gftRoot.FolderIds = dfit;
gftRoot.FolderShape = frst;
GetFolderResponseType gfrt = this.m_esb.GetFolder(gftRoot);
FolderInfoResponseMessageType firmt = ((FolderInfoResponseMessageType)gfrt.ResponseMessages.Items[0]);
FolderIdType[] fita = new FolderIdType[1];
fita[0] = new FolderIdType();
if (firmt.ResponseClass == ResponseClassType.Success)
{
fita[0].Id = ((FolderInfoResponseMessageType)gfrt.ResponseMessages.Items[0]).Folders[0].FolderId.Id;
}
if (fita[0].Id == null) return new Hashtable();
//request AllProperties
PathToUnindexedFieldType[] ptufta = new PathToUnindexedFieldType[1];
ptufta[0] = new PathToUnindexedFieldType();
ptufta[0].FieldURI = UnindexedFieldURIType.itemItemClass;
ItemResponseShapeType irst = new ItemResponseShapeType();
irst.BaseShape = DefaultShapeNamesType.AllProperties;
irst.IncludeMimeContent = false;
irst.AdditionalProperties = ptufta;
//restrict the returned items to just items of a specific message class
PathToUnindexedFieldType MsgClassField = new
PathToUnindexedFieldType();
MsgClassField.FieldURI = UnindexedFieldURIType.itemItemClass;
ConstantValueType MsgClassToGet = new ConstantValueType();
MsgClassToGet.Value = "IPM.NOTE";
FieldURIOrConstantType MsgClassConstant = new FieldURIOrConstantType();
MsgClassConstant.Item = MsgClassToGet;
IsEqualToType iett = new IsEqualToType();
iett.FieldURIOrConstant = MsgClassConstant;
iett.Item = MsgClassField;
//restrict the returned items greater than a specified date
PathToUnindexedFieldType StartDateReceivedField = new PathToUnindexedFieldType();
StartDateReceivedField.FieldURI = UnindexedFieldURIType.itemDateTimeReceived;
ConstantValueType StartDateReceivedToGet = new ConstantValueType();
StartDateReceivedToGet.Value = StartDate.ToUniversalTime().ToString();
FieldURIOrConstantType StartDateReceivedConstant = new FieldURIOrConstantType();
StartDateReceivedConstant.Item = StartDateReceivedToGet;
IsGreaterThanOrEqualToType igtett = new IsGreaterThanOrEqualToType();
igtett.FieldURIOrConstant = StartDateReceivedConstant;
igtett.Item = StartDateReceivedField;
//restrict the returned items less than a specified date
PathToUnindexedFieldType EndDateReceivedField = new PathToUnindexedFieldType();
EndDateReceivedField.FieldURI = UnindexedFieldURIType.itemDateTimeReceived;
ConstantValueType EndDateReceivedToGet = new ConstantValueType();
EndDateReceivedToGet.Value = EndDate.ToUniversalTime().ToString();
FieldURIOrConstantType EndDateReceivedConstant = new FieldURIOrConstantType();
EndDateReceivedConstant.Item = EndDateReceivedToGet;
IsLessThanOrEqualToType iltett = new IsLessThanOrEqualToType();
iltett.FieldURIOrConstant = EndDateReceivedConstant;
iltett.Item = EndDateReceivedField;
AndType at = new AndType();
at.Items = new SearchExpressionType[3];
at.Items[0] = igtett;
at.Items[1] = iltett;
at.Items[2] = iett;
//TODO: Uncomment the following line if you want to display only email items from folder
// If the following line is commented then all items would be returned from folder,
// for e.g. Reports, Appointment, Meeting Requests, Contacts etc.
RestrictionType rt = new RestrictionType();
rt.Item = at;
//find the items
FindItemType fit = new FindItemType();
fit.ItemShape = irst;
fit.ParentFolderIds = fita;
fit.Restriction = rt;
FindItemResponseType firt = m_esb.FindItem(fit);
// That's it!
ItemType[] list = ((ArrayOfRealItemsType)((FindItemResponseMessageType)firt.ResponseMessages.Items[0]).RootFolder.Item).Items; // Emails We want to.
//foreach (ItemType i in list)
//{
// SetIsReadFlag(i.ItemId);//set is read flag.
//}
Hashtable ht = new Hashtable();
if (list != null)
{
foreach (ItemType itemOne in list)
{
ItemType Item = new ItemType();
ht.Add(Item.ItemId, Item);
}
}
return ht;
}
catch (Exception e)
{
throw new Exception("Error :" + e.Message);
}
}