+1 vote

How can I move a mail from Inbox to Trash. I tried with the following which didn't worked

using (Imap imap = new Imap())
{
imap.Connect("imap.example.com");   // or ConnectSSL for SSL
imap.UseBestLogin("user", "password");

imap.Select("[Gmail]/Trash");
List<long> uids = imap.Search(Flag.Seen);
foreach (long uid in uids)
{
    imap.MoveByUID(uid, "[Gmail]/Trash");
}
imap.Close();
}
by (1.0k points)
retagged by

1 Answer

0 votes
 
Best answer

If you want to move emails from INBOX to trash, select INBOX (source) folder first.

You may also use CommonFolders class to identify IMAP's server trash folder.

using (Imap imap = new Imap())
{
    imap.Connect("imap.example.com");   // or ConnectSSL for SSL
    imap.UseBestLogin("user", "password");

    // identify trash folder:
    CommonFolders common = new CommonFolders(imap.GetFolders());

    // select source folder:
    imap.SelectInbox();

    List<long> uids = imap.Search(Flag.Seen);

    // move to destination (note the bulk method usage):
    imap.MoveByUID(uids , common.Trash);

    imap.Close();
}
by (297k points)
selected by
If I want to move from Trash to Inbox then also we need to select source folder as imap.SelectInbox(); or imap.SelectInbox(common.Trash);
No, if you want to move from trash to INBOX: select trash first (imap.Select(common.Trash)) and use INBOX as MoveByUID parameter (imap.MoveByUID(uid, common.Inbox));
Move the e-mail another folder
...