c# - 通过 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);
}
解决方案
处理错误的电子邮件后,将标志“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.
}
}
推荐阅读
- c - 无法将字符添加到分配给“”的字符指针
- javascript - 如何将 js 字符串转换为类似于 AST 树的东西?
- javascript - ClassList.toggle 在 ul (HTML/CSS/JavaScript) 中不起作用
- python - Colorama 和 Pyinstaller
- loops - 获取循环中项目的索引以匹配存储的索引
- php - 我如何确定每个访问者正在执行 jquery ajax 调用?
- java - 设置第二个值时,ArrayLists 的 ArrayList 被覆盖
- c# - LINQ - 列表中的条件导航
- asynchronous - 异步 Vala 示例
- c - 为什么 flex-lexer 不输出任何东西?