This project is read-only.

Example: Archive Email in Gmail (Set any IMAP Flag)

Aug 16, 2011 at 8:28 PM
Edited Aug 16, 2011 at 8:43 PM

In Gmail, setting the DELETED flag archives the email. The email will not be deleted or sent to the Trash.

 

 

         //action_id is the Message.MessageId of the email 
         //action flag is the flag to set such as SEEN, DELETED, etc.
         //to remove a flag (to mark unread remove SEEN) change "flags" to "-flags" in the IMAP command
         //to add labels remove the backslash (\\) from the IMAP command      
        public static void set_flag(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("store " + ids[i].ToString() + " flags \\" + action_flag);
                        break;
                    }
                }
            }
        }