简体   繁体   English

如何使用C#以编程方式在IMAP或POP中删除邮件

[英]How to delete the Mail Programmatically in IMAP or POP using C#

This is my code to delete the mail. 这是我删除邮件的代码。 But, it doest working, 但是,它不起作用,

 public object Delete(int index)
        {
            StreamWriter.WriteLine("$ DELE {0}"+index);
            StreamWriter.Flush();
            return Response();    
        }
private string Response()
        {
            byte[] data = new byte[TcpClient.ReceiveBufferSize];
            int ret = NetworkStream.Read(data, 0, data.Length);
            return Encoding.ASCII.GetString(data).TrimEnd();
        }

Why are you adding these two items together: 为什么将这两个项目加在一起:

StreamWriter.WriteLine("$ DELE {0}"+index);

Surely you mean something like this: 当然,您的意思是这样的:

StreamWriter.WriteLine(String.Format("$ DELE {0}",index));

That said, this doesn't look like a valid IMAP command anyway. 就是说,这看起来并不像一个有效的IMAP命令。 This looks more like POP, and even then, you shouldn't have a $ in the command string. 这看起来更像POP,即使这样,您也不应在命令字符串中包含$ What specification are you following? 您遵循什么规格?

If you're connected to a POP3 server, the command should look something like this: 如果连接到POP3服务器,则命令应如下所示:

StreamWriter.WriteLine(String.Format("DELE {0}",index));

If you're connected to an IMAP server, I believe deleting requires first flagging the message as deleted, and then expunging it. 如果您已连接到IMAP服务器,我相信删除操作首先需要将邮件标记为已删除,然后删除它。 Also, all commands in IMAP require a unique identifier which you will need to generate. 同样,IMAP中的所有命令都需要一个唯一的标识符,您将需要生成该标识符。 From the spec: 从规格:

Each client command is prefixed with an identifier (typically a short alphanumeric string, eg, A0001, A0002, etc.) called a "tag". 每个客户端命令都以称为“标签”的标识符(通常是短字母数字字符串,例如A0001,A0002等)为前缀。 A different tag is generated by the client for each command. 客户端为每个命令生成一个不同的标记。

Then to flag a message as deleted you would do something like this: 然后,将邮件标记为已删除,您可以执行以下操作:

A003 STORE 35 +FLAGS (\Deleted)

Where A003 is the tag prefix, and 35 is the sequence number of the message you want to delete. 其中A003是标签前缀,而35是要删除的消息的序列号。 But then to permanently delete all flagged messages you'll also need to send and EXPUNGE command, that might look like this: 但是,要永久删除所有标记的消息,您还需要发送和EXPUNGE命令,如下所示:

A004 EXPUNGE

Note that EXPUNGE will potentially result in message sequence numbers changing so you can't assume that the message associated with a particular sequence number will be the same after the EXPUNGE command has completed. 请注意, EXPUNGE可能会导致消息序列号发生更改,因此您不能假定与特定序列号关联的消息在EXPUNGE命令完成后将是相同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM