【问题标题】:Not moving one email in outlook inbox using foreach loop in asp.net win forms在asp.net winforms中使用foreach循环不移动Outlook收件箱中的一封电子邮件
【发布时间】:2018-02-21 07:18:44
【问题描述】:

我正在尝试将所有电子邮件移动到 Outlook 中另一个文件夹中,主题为:

“消息传递失败”

每次foreach 循环运行时,最后一封电子邮件总是留在收件箱中并且循环中断。

OutLook.MAPIFolder inBox = mOulook.Application.ActiveExplorer().Session.GetDefaultFolder(OutLook.OlDefaultFolders.olFolderInbox);
OutLook.Items inBoxItems = inBox.Items;
OutLook.MailItem newEmail = null;
//Creating destination folder where emails are going to be moved
Microsoft.Office.Interop.Outlook.MAPIFolder destFolder = null;
if (destFolder == null)
{
    destFolder = inBox.Folders["Test"];
}
//moving each email from source to destination
foreach (object eMail in inBoxItems)
{
    try
    {
        newEmail = eMail as Microsoft.Office.Interop.Outlook.MailItem;
        if (newEmail != null)
        {
            string titleSubject = (string)newEmail.Subject;
            if (titleSubject == "Message Delivery Failure")
            {
                newEmail.Move(destFolder);
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

【问题讨论】:

  • 请注意,它不存在任何asp.net win forms。你这里说的是winforms,asp.net是另一个世界

标签: c# winforms visual-studio outlook


【解决方案1】:

这可能是由于if (titleSubject == "Message Delivery Failure") 的条件。你可以考虑只循环那些需要的,比如

foreach (OutLook.MailItem eMail in ((Microsoft.Office.Interop.Outlook.MailItem)inBoxItems).Select(em => em.Subject == "Message Delivery Failure").ToList())
{

【讨论】:

  • 试过你的答案,还是不行。收件箱中留下的最后一封主题为“Message Delivery Failed”的电子邮件
【解决方案2】:

在迭代集合的项目时,您正在更改集合(通过调用 Move)。

改用向下的“for”循环(从 Items.Count 到 1)。

【讨论】:

    【解决方案3】:

    它的动态 COM_object。 尝试循环管理。

    问题是当它未读电子邮件时,它也会减少循环中的 .Count()。

    尝试跟踪您的所有 Mailitems 并通过 ConversationID/MessageId 找到它并将所有 Mailitems 添加到列表中:

    List MailItems = new List();

    MailItem 电子邮件 = MailItems.Where(z => z.ConversationID.Equals("ConversationID")).First();

    Email.Move(FolderToMove);

    【讨论】:

    • 请为您的回答提供更多背景信息。
    • @DMart:抱歉!添加了示例说明。
    猜你喜欢
    • 1970-01-01
    • 2017-07-02
    • 1970-01-01
    • 2010-12-27
    • 1970-01-01
    • 1970-01-01
    • 2011-02-28
    • 1970-01-01
    • 2015-10-03
    相关资源
    最近更新 更多