MailSystem.Net删除消息,IndexOnServer属性= 0 [英] MailSystem.Net Delete Message, IndexOnServer Property = 0

查看:81
本文介绍了MailSystem.Net删除消息,IndexOnServer属性= 0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 MailSystem.NET 并尝试从服务器中删除邮件.问题是IndexOnServer属性为0,并且出现以下错误:

I'm using MailSystem.NET and trying to delete a message from the server. The problem is the IndexOnServer property is 0, and I get the following error:

Command "store 0 +flags.silent (\Deleted)" failed : 121031084812790 BAD Error in IMAP command STORE: Invalid messageset

这是我的代码:

    Imap4Client client = new Imap4Client();
    client.Connect(account.Server, account.Username, account.Password);
    var inbox = client.SelectMailbox("Inbox");
    MessageCollection messages = inbox.SearchParse("SINCE " + DateTime.Now.AddHours(-hours).ToString("dd-MMM-yyyy"));

    foreach (Message newMessage in messages)
    {
        inbox.DeleteMessage(newMessage.IndexOnServer, true);
    }

如何获取邮件的正确索引,以便将其删除?

How do I get the correct index of the message so I can delete it?

使用基于标准1的循环的建议存在的问题是计数器索引将与消息索引不同步,因为在我的情况下,我仅搜索消息的特定子集(因为我了解).

The problem with the suggestions using the standard 1-based loop is that the counter index won't be in sync with the message index, since in my case I'm searching to retrieve a specific subset of messages only (as I understand it).

谢谢.

推荐答案

您可以尝试通过 UID 进行删除,该删除应该更可靠并且对每条消息都是唯一的.过去,这对我来说效果很好.

You can try deleting by the UID which should be more reliable and unique to each message. This has worked well for me in the past.

编辑:由于删除邮件会导致所有索引向下移动一个,因此可以使用两个单独的计数器.一个跟踪过去何时遍历整个框(messagesLeft),另一个跟踪当前的消息索引,如果删除了一条消息,该索引将减少1(因为它将在一行中向上移动一位).

Since deleting the message causes all the indexes to shift down by one, you can use two separate counters. One to keep track of when you have iterated through the entire box (messagesLeft) and the other will keep track of the current message index which will be decreased by 1 if a message is deleted (since it would move up one place in line).

Mailbox box = client.AllMailboxes["inbox"];
Fetch fetch = box.Fetch;
int messagesLeft = box.Count;
int msgIndex = 0;

while (messagesLeft > 0)
{
    msgIndex++;
    messagesLeft--;
    Message email = fetch.MessageObject(msgIndex);

    if (criteria)
    {
        box.UidDeleteMessage(fetch.Uid(msgIndex), true);
        msgIndex--;
    }
}


为回应您的评论,下面是一个更完整的示例,说明如何使用UID删除而不关心数字位置/索引.


In response to your comment, here is a fuller example of how you can use the UID for deletion without being concerned with the numeric position / index.

class Email
{
       int UID { get; set; }
       DateTime Sent { get; set; }
       public string Body { get; set; }
       // put whichever properties you will need
}

List<Email> GetEmails(string mailbox);
{
    Mailbox box = client.AllMailboxes[mailbox];
    Fetch fetch = box.Fetch;

    List<Email> list = new List<Email>();
    for (int x = 1; x <= box.MessageCount; x++)
    {
        Message msg = fetch.MessageObject(x);
        list.Add(new Email() { } // set properties from the msg object
    }

    return list;
}

void DeleteEmail(Email email, string mailbox)
{
       Mailbox box = client.AllMailboxes[mailbox];
       box.UidDeleteMessage(email.Uid, true);
}

static void Main()
{
    List<Email> emails = GetEmails("inbox");
    emails = emails.Where(email => email.Sent < DateTime.Now.AddHours(-hours))
    foreach (Email email in emails)
         DeleteEmail(email);
}

这篇关于MailSystem.Net删除消息,IndexOnServer属性= 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆