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;
                    }
                }
            }
        }
Jun 20 at 2:07 PM
Edited Jun 20 at 2:10 PM
I am facing issue that ....the above code is deleting every odd email from gmail...
suppose i sent 7 mails and trying deleting them its deleting
1,3,5,7

I am trying to delete every email from a particular email with a subject except the latest one but its not working out

Please help on this asap

Regards
Ajay

Here is my method to delete :

public static void DeleteMails(Imap4Client imap, string from, string subjectKeyword, Mailbox inbox)
    {
        try
        {
            var emails = inbox.Search("From \" " + from + " \" Subject \"" + subjectKeyword + "\"");

            System.Console.WriteLine("# of emails: " + emails.Count());
            if (emails.Count() > 0)
            {
                if (latestmail == 0)
                    latestmail = emails.LastOrDefault();

                imap.Command("capability");
                foreach (var email in emails)
                {
                    if (email != latestmail)
                    {
                        imap.Command("copy " + email.ToString() + " [Gmail]/Trash");
                    }
                }
            }

        }
        catch (Exception ex)
        {
            System.Console.WriteLine(ex.Message);
        }
    }
Jun 21 at 7:03 AM
As no one replied so I tried by myself and achieved it following is the code to delete :

public static void DeleteMails(Imap4Client imap, string from, string subjectKeyword, Mailbox inbox)
    {
        try
        {
            var actionids = inbox.Search("From \" " + from + " \" Subject \"" + subjectKeyword + "\"");
            var emailcount = actionids.Count();

            System.Console.WriteLine("# of emails: " + emailcount);
            if (emailcount > 0)
            {
                if (latestmail == 0)
                    latestmail = actionids.LastOrDefault();
                foreach (var actionid in actionids)
                {
                    if (actionid != latestmail)
                    {
                        Delete(imap, actionid.ToString());
                        break;
                    }
                }

                actionids = inbox.Search("From \" " + from + " \" Subject \"" + subjectKeyword + "\"");
                if (actionids.Count() > 1)
                    DeleteMails(imap, from, subjectKeyword, inbox);
                else
                    System.Console.WriteLine("Emails deleted");
            }

        }
        catch (Exception ex)
        {
            System.Console.WriteLine(ex.Message);
        }
    }


    private static void Delete(Imap4Client imap, string actionid)
    {
        try
        {
            imap.Command("copy " + actionid + " [Gmail]/Trash");
        }
        catch (Exception ex)
        {
            System.Console.WriteLine(ex.Message);
        }
    }