使用c#将未读电子邮件移动到Outlook收件箱中的其他文件夹 [英] Move unread emails to other folder in Outlook inbox using c#
本文介绍了使用c#将未读电子邮件移动到Outlook收件箱中的其他文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,
我使用下面的代码将所有未读电子邮件从Outlook的收件箱中移动到另一个文件夹。
但是此代码没有正常工作,例如,如果4封电子邮件处于未读状态,它只会向此文件夹移动2封电子邮件。所以剩下2封邮件就失败了。
请你指导解决问题。< br $> b $ b
Hello,
I am using below code to move all unread emails to another folder from inbox in outlook.
But this code was not working properly, for example if 4 emails are in unread condition it moves only 2 emails to this folder .so it was failed for remaining 2 mails .
Can u please guide to resolve the issue.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Outlook;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Microsoft.Office.Interop.Outlook.Application myApp = new Microsoft.Office.Interop.Outlook.ApplicationClass();
Microsoft.Office.Interop.Outlook.NameSpace mapiNameSpace = myApp.GetNamespace("MAPI");
Microsoft.Office.Interop.Outlook.MAPIFolder myInbox = mapiNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
// Microsoft.Office.Interop.Outlook.MailItem newEmail = null;
Microsoft.Office.Interop.Outlook.MailItem movemail = null;
Microsoft.Office.Interop.Outlook.Items items = (Microsoft.Office.Interop.Outlook.Items)myInbox.Items;
Microsoft.Office.Interop.Outlook.MAPIFolder destFolder = myInbox.Folders["Arch"];
items.Restrict("[UnRead] = true");
// Microsoft.Office.Interop.Outlook.MAPIFolder destFolder1 = mapiNameSpace.Folders["CQ Web"];
foreach (object eMail in items.Restrict("[UnRead] = true"))
{
movemail = eMail as Microsoft.Office.Interop.Outlook.MailItem;
if (movemail != null)
{
movemail.Move(destFolder);
}
}
}
}
}
推荐答案
问题来了,因为每次移动时收件箱文件夹中的电子邮件数量都是度数。要修复此错误,请不要使用foreach函数,请使用for。贝娄是我使用的代码:
That issue come because number of email in inbox folder will be degree each time you moved.To fix this error, don't use foreach function, please using for. Bellow is code I was used:
MAPIFolder inBox = this.Application.ActiveExplorer().Session.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
Items items = inBox.Items;
//MailItem moveMail = null;
//items.Restrict("[UnRead] = true");
MAPIFolder destFolder = inBox.Folders["Fishing"];
MessageBox.Show(items.Count.ToString());
//MessageBox.Show(items.ToString());
//while (items.Count != 0)
//foreach (MailItem eMail in items)
int ItemsCount = items.Count;
for(int i=ItemsCount;i>0;i--)
{
try
{
{
if (items[i].Subject.Contains("Fishing"))
{
items[i].Move(destFolder);
MessageBox.Show("Done!");
}
}
}
catch (System.Exception ex)
{
MessageBox.Show("Error when moving email: "+ex);
}
}
出于某种原因
items.Restrict("[UnRead] = true")
将无法正常工作。我在编写outlook宏时遇到了这个问题。对此不太好的解决方法是使用while循环:
won't work that way. I encountered this problem writing an outlook macro. The not-so-nice workaround to this is to use a while loop:
while(items.Restrict("[UnRead] = true").Count > 0) {
foreach(...)
上面的代码是正确的,它将所有未读的电子邮件移动到所需的子文件夹位置,但同时它很难删除未读的电子邮件&电子邮件被丢弃在从服务器恢复已删除的电子邮件
请提供解决方案而不会将未读电子邮件转发到已恢复的服务器删除电子邮件
谢谢
The above code which is correct it moves all the unread email to desired subfolder location , but at the same time it hard deleting unread email & email are getting dumped in "Restored deleted email from server "
Please provide me solution without getting dumped unread email to "restored delete email from server"
Thanks
这篇关于使用c#将未读电子邮件移动到Outlook收件箱中的其他文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文