Move emails to different folder with IMAP

This article describes how to move email messages from one folder (mailbox) to another using Mail.dll .NET IMAP library.

// C# version

using Limilabs.Mail;
using Limilabs.Client.IMAP;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        using (Imap imap = new Imap())
        {
            imap.Connect("imap.example.com");   // or ConnectSSL for SSL
            imap.UseBestLogin("user", "password");

            imap.SelectInbox();
            List<long> uids = imap.Search(Flag.Seen);
            foreach (long uid in uids)
            {
                imap.MoveByUID(uid, "Inbox/Seen");
            }
            imap.Close();
        }
    }
};
' VB.NET version:

Imports Limilabs.Mail
Imports Limilabs.Client.IMAP
Imports System.Collections.Generic

Public Module Module1
    Public Sub Main(ByVal args As String())

        Using imap As New Imap()
            imap.Connect("imap.example.com")   ' or ConnectSSL for SSL
            imap.UseBestLogin("user", "password")

            imap.SelectInbox()
            Dim uids As List(Of Long) = imap.Search(Flag.Seen)
            For Each uid As Long In uids
                imap.MoveByUID(uid, "Inbox/Seen")
            Next
            imap.Close()
        End Using

    End Sub
End Module

You can also use bulk methods to move, copy and delete emails:

// C# version

using (Imap imap = new Imap())
{
    imap.Connect("imap.example.com");
    imap.Login("user", "password");

    imap.SelectInbox();
    List<long> uids = imap.Search(Flag.Seen);
    imap.MoveByUID(uids, "Inbox/Seen");
    imap.Close();
}
' VB.NET version:

Using imap As New Imap()
	imap.Connect("imap.example.com")
	imap.Login("user", "password")

	imap.SelectInbox()
	Dim uids As List(Of Long) = imap.Search(Flag.Seen)
	imap.MoveByUID(uids, "Inbox/Seen")
	imap.Close()
End Using

Tags:    

Questions?

Consider using our Q&A forum for asking questions.