首页 > 解决方案 > 通过 id 移动邮件列表的邮件

问题描述

我需要从班级阅读的 5 封邮件列表中移动一封邮件。此邮件已由我创建的逻辑处理并满足创建的条件。问题是它移动了这 5 封电子邮件,其中一些不符合条件。如果满足条件的邮件设法将数据输入数据库,则必须将其移至已处理邮件文件夹,否则必须将其移至错误文件夹。

这是接收电子邮件的类

    int bufferLength = 5;
    int indiceMail = 0;
    string from = "mail@gmail.com>";
    do
        {
            emailList.getEmails(bufferLength);
        while(indiceMail<emailList.emails.Count)
        {
            indiceMail++;
        }
        Console.WriteLine("Reading: {0}", emailList.emails.Count);
    }while (emailList.MoreAvailable);

这是移动邮件的条件

    string bodyMail = emailList.emails[indiceMail].body;
    match3 = Regex.Match(bodyMail, @"(?<=Status:) (\S+\b)");
    statuscompare = match3.Value;

    List<String> statusList = new List<string> { "i2", "i3", "i4", "i8" };
    bool ex = false;
    foreach (string item1 in statusList)
    {
        if (item1.Contains(statuscompare.Trim()))
        {
            ex = true;
            if (item1.Contains("i4"))
            {

                bool moveEmail = false;
                foreach (Email item in emailList.emails)
                {
                    if (emailList.emails[indiceMail].body.Contains("i4"))
                    {
                        // if (item.body.Contains(item1))
                        //{
                        moveEmail = true;
                        emailList.moveMail(item.id, emailList.config.PathSuccess);
                        break;
                        // }
                    }
                    if (moveEmail)
                    {
                        continue;
                    }
                }
            }
        }
    }

这是移动邮件类的一部分

        public void moveMail(string emailId, string folderPath)
        {
            string folderId = getMailFolderId(folderPath);
            EmailMessage message = EmailMessage.Bind(service, emailId);
            message.Move(folderId);
        }

标签: c#listemailmove

解决方案


处理错误的电子邮件后,将标志“emailbool”设置为 false,这会将所有其他电子邮件移至 PathSuccess。除了确保只有一封电子邮件进入错误路径之外,不确定 emailbool 实际如何服务于任何目的。

foreach (Email item in emailList.emails)
{
    // This IF statement will either be true of false for ALL items.
    if (!Cmmd.Parameters["p_retorno"].Value.ToString().Equals("0")) 
    {
        emailList.moveMail(item.id, emailList.config.PathError);
    }
    else
    {
        emailList.moveMail(item.id, emailList.config.PathSuccess); 
    }
}

编辑:
对于您发布的新代码,请尝试此操作以移动包含其正文中 statusList 中的任何字符串的所有电子邮件。

    // This will move all emails that have one or more statusList in their body
    foreach (Email item in emailList.emails)
    {
        if (statusList.Where(x => item.body.Contains(x)).Count > 0)
        {
            emailList.moveMail(item.id, emailList.config.PathSuccess);
            break; // This statement will stop processing of any other emails.
        }
    }

推荐阅读