我正在使用Outllok Interop将电子邮件从一个文件夹移动到另一个文件夹(在获取所有附件之后,但这有效)但它并未复制所有电子邮件。我已经尝试过等待,但它没有效果。首先它会移动6,然后移动3,然后移动1.可以有人告诉我为什么它不会移动它们吗?
相关代码如下:
Application oOutlook = new Application();
NameSpace oNs = oOutlook.GetNamespace("MAPI");
Recipient oRep = oNs.CreateRecipient("ContentHelp");
MAPIFolder inbox = oNs.GetSharedDefaultFolder(oRep, OlDefaultFolders.olFolderInbox);
MAPIFolder nihSub = inbox.Folders["NIH"];
MAPIFolder nihArchive = inbox.Folders["NIHarchive"];
Items nihItems = nihSub.Items;
MailItem moveMail = null;
//inboxItems = inboxItems.Restrict("[Unread] = false");
int increment = 0;
try
{
foreach (object collectionItem in nihItems)
{
moveMail = collectionItem as MailItem;
if (moveMail != null)
{
Console.WriteLine("Moving {0}", moveMail.Subject.ToString());
string titleSubject = (string)moveMail.Subject;
moveMail.Move(nihArchive);
}
}
}
答案 0 :(得分:5)
每次循环移动时索引都会重置,因此您永远不会超过一半的项目。 使用从olItems.Count到1的While循环或倒计时。
答案 1 :(得分:2)
向后循环是从最大值到最小值的循环。
IE:
for(int i = 10; i>0; i--)
{
Console.WriteLine(i);
}
对于这个例子,你可以使用类似的东西:(请注意我没有使用outlook对象进行测试,因此可能需要进行一些调整)
for (int i=nihItems.count; i >= 0; i--)
{
moveMail collectionItem = nihItems[i] as MailItem
if (moveMail != null)
{
Console.WriteLine("Moving {0}", moveMail.Subject.ToString());
string titleSubject = (string)moveMail.Subject;
moveMail.Move(nihArchive);
}
}
答案 2 :(得分:0)
不太好,但它确实有效:
Outlook.Folder FolderInbox = NameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) as Outlook.Folder;
List<Outlook.MailItem> MailItemList = new List<Outlook.MailItem>();
foreach (Outlook.MailItem item in FolderInbox.Items.OfType<Outlook.MailItem>())
MailItemList.Add(item);
foreach (Outlook.MailItem item in MailItemList)
ProcessMail(item);
答案 3 :(得分:0)
for (int j= items.Count();j>=1; j--){
dynamic item = newItems[j] ;
if (item is MailItem){
item.Move();
}
}