Example: Delete email in Gmail (Move email to another folder)

Aug 16, 2011 at 7:36 PM
Edited Aug 16, 2011 at 7:43 PM

Simply copying the message to the "[Gmail]/Trash" removes it from the inbox, without the need for deleting the message.

For a list of Gmail's IMAP special folders just search for that term.

 

        //action_id is the Message.MessageId of the email
        //action_flag is the Gmail special folder to move to (Trash)
        //copying to Trash removes the email from the inbox, but it can't be moved back once done, even from the web interface
        public static void move_msg_to(string action_id, string action_flag)
        {
            Imap4Client imap = new Imap4Client();
            imap.ConnectSsl("imap.gmail.com", 993);
            imap.Login("heythatsme@gmail.com", "heythatsmypassword");

            imap.Command("capability");

            Mailbox inbox = imap.SelectMailbox("inbox");
            int[] ids = inbox.Search("ALL");
            if (ids.Length > 0)
            {
                Message msg = null;
                for (var i = 0; i < ids.Length; i++)
                {
                    msg = inbox.Fetch.MessageObject(ids[i]);
                    if (msg.MessageId == action_id)
                    {
                        imap.Command("copy " + ids[i].ToString() + " [Gmail]/" + action_flag);
                        break;
                    }
                }
            }
        }