New command for copying mails

Feb 12, 2009 at 12:27 PM

Below is an addition to the file ImapCommand.cs.

Now, a mail (or a range of mails) can be copied from one folder (mailbox) to another folder (mailbox).

This can also be used to move mails between folders. Just copy them first, then delete the original afterwards.
To delete a mail, use the SetDeleted method first and then the Expunge method afterwards.

......

/// <summary>
/// Copies some messages from the current mailbox to another mailbox.
/// </summary>
/// <param name="destinationMailbox">The name of the destination mailbox.</param>
/// <param name="begin">ID of the first message to copy.</param>
/// <param name="end">ID of the last message to copy.</param>
/// <returns>void</returns>
public void Copy(string destinationMailbox, int begin, int end)
{
if (!(Connection.ConnectionState == ConnectionState.Open))
NoOpenConnection();
string cmd = string.Format("COPY {0}:{1} \"{2}\"\r\n", begin, end, destinationMailbox);
Connection.Write(cmd);
string response = Connection.Read();
}

------------------------
I made this while writing a small program to fetch mails from a Microsoft Exchange Server.
------------------------

Nov 16, 2009 at 8:25 PM
Edited Nov 16, 2009 at 8:27 PM

I want to move single message to different folder

Command.Copy("XYZ", message.ID, message.ID);

Command.SetDeleted(message.ID,true);

Is this correct?

 

Nov 17, 2009 at 7:17 AM

The two lines of code you have written are correct.

But the SetDeleted method only marks for deletion. It does not perform the actual deletion. Use the Expunge method to perform the deletion.

Therefore, you also have to add a third line of code in order to delete the original message that you marked for deletion:

 

command.Expunge();

Nov 17, 2009 at 12:41 PM

Thanks I had that line too in my code.

Issue was with regular expression used to check response returned after message is copied successfully.  Works fine now.

Thanks for your prompt reply.

Apr 25, 2010 at 1:48 PM

I had to enter the following change in ImapCommand.cs:

bool IsResponseEnd(string response)        
{
            return (Regex.IsMatch(response, OK_COMPLETE) ||
                Regex.IsMatch(response, FETCH_NOT_OK) ||
                Regex.IsMatch(response, OK_SUCCESS) 
		//Fixed because of Copy command response 
		|| Regex.IsMatch(response, @"kw\d+\WOK \[[Cc][Oo][Pp][Yy][Uu][Ii][Dd] \d+ \d+ \d+\] \([Ss][Uu][Cc][Cc][Ee][Ss][Ss]\)$") 
		);
}