简体   繁体   中英

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. This looks more like POP, and even then, you shouldn't have a $ in the command string. What specification are you following?

If you're connected to a POP3 server, the command should look something like this:

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. Also, all commands in IMAP require a unique identifier which you will need to generate. From the spec:

Each client command is prefixed with an identifier (typically a short alphanumeric string, eg, A0001, A0002, etc.) called a "tag". 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. But then to permanently delete all flagged messages you'll also need to send and EXPUNGE command, that might look like this:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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