MailClient.Move Method


Move a specified email from selected folder to another folder on IMAP4/Exchange server.

[Visual Basic]
Public Sub Move( _
    info As MailInfo, _
    destFolder As Imap4Folder _
)

Public Function MoveAsync( _
    info As MailInfo, _
    destFolder As Imap4Folder _
) As Task
[C#]
public void Move(
    MailInfo info,
    Imap4Folder destFolder
);

public async Task MoveAsync(
    MailInfo info,
    Imap4Folder destFolder
);
[C++]
public: void Move(
    MailInfo^ info,
    Imap4Folder^ destFolder
);
[JScript]
public function Move(
    info : MailInfo,
    destFolder: Imap4Folder
);

Parameters

info
The email to Move.
destFolder
The destination IMAP4 folder.

Example

[Visual Basic, C#, C++] The following example demonstrates how to move every email in "INBOX" to "Deleted Items" with EAGetMail POP3 & IMAP Component. To get the full samples of EAGetMail, please refer to Samples section.

[VB - Move email to specified folder]
Imports System
Imports System.Globalization
Imports System.IO
Imports System.Text
Imports EAGetMail

Public Class TestClass
    ' if you want to search sub folder, use parentfolder\subfolder as folderPath
    ' for example: inbox\mysubfolder
    Function FindFolder(ByVal folderPath As String, ByRef folders As Imap4Folder()) As Imap4Folder
        For i As Integer = 0 To folders.Length - 1
            Dim folder As Imap4Folder = folders(i)
            ' Folder was found.
            If String.Compare(folder.LocalPath, folderPath, True) = 0 Then
                Return folder
            End If

            folder = FindFolder(folderPath, folder.SubFolders)
            If Not (folder Is Nothing) Then
                Return folder
            End If
        Next

        ' No folder found
        Return Nothing
    End Function

    Public Sub MoveEmail(server As String, user As String, password As String, useSsl As Boolean)
        Try
            ' ExchangeEWS Or ExchangeWebDAV protocol also supports Move method.

            ' Most modern email server require SSL/TLS connection, 
            ' set useSsl to true Is recommended.
            Dim oServer As New MailServer(server, user, password, useSsl,
                        ServerAuthType.AuthLogin, ServerProtocol.Imap4)

            ' IMAP4 port Is 143,  IMAP4 SSL port Is 993.
            ' EWS/WebDAV, please ignore Port property.
            oServer.Port = If(useSsl, 993, 143)

            Console.WriteLine("Connecting server ...")
            Dim oClient As New MailClient("TryIt")
            oClient.Connect(oServer)

            Dim folders() As Imap4Folder = oClient.GetFolders()

            ' Find source folder based on path.
            Dim folder As Imap4Folder = FindFolder("Inbox", folders)
            If folder Is Nothing Then
                Throw New Exception("Source folder was not found")
            End If

            ' select source folder
            oClient.SelectFolder(folder)

            ' find dest folder
            Dim destFolder As Imap4Folder = FindFolder("Deleted Items", folders)
            If (destFolder Is Nothing) Then
                Throw New Exception("Dest folder not found!")
            End If

            Dim infos() As MailInfo = oClient.GetMailInfos()
            Console.WriteLine("Total {0} email(s) in source folder", infos.Length)

            For i As Integer = 0 To infos.Length - 1
                Dim info As MailInfo = infos(i)

                Console.WriteLine("Moving {0}/{1} ...", i + 1, infos.Length)
                oClient.Move(info, destFolder)
            Next

            ' Because IMAP only marks source emails as deleted with move method
            ' Use Expunge method to expunge source emails permanently
            If oServer.Protocol = ServerProtocol.Imap4 Then
                oClient.Expunge()
            End If

            oClient.Logout()
            Console.WriteLine("Completed!")
        Catch ep As Exception
            Console.WriteLine(ep.Message)
        End Try

    End Sub

End Class


[C# - Move email to specified folder] using System; using System.IO; using System.Globalization; using System.Text; using EAGetMail; class TestClass { // if you want to search sub folder, use parentfolder\subfolder as folderPath // for example: inbox\mysubfolder static Imap4Folder FindFolder(string folderPath, Imap4Folder[] folders) { int count = folders.Length; for (int i = 0; i < count; i++) { Imap4Folder folder = folders[i]; if (string.Compare(folder.LocalPath, folderPath, true) == 0) { return folder; } folder = FindFolder(folderPath, folder.SubFolders); if (folder != null) { return folder; } } // No folder found return null; } public void MoveEmail(string server, string user, string password, bool useSsl) { try { // ExchangeEWS Or ExchangeWebDAV protocol also supports Move method. // Most modern email servers require SSL/TLS connection, // set useSsl to true is recommended. MailServer oServer = new MailServer(server, user, password, useSsl, ServerAuthType.AuthLogin, ServerProtocol.Imap4); // IMAP4 port is 143, IMAP4 SSL port is 993. // EWS/WebDAV, please ignore Port property. oServer.Port = (useSsl) ? 993 : 143; Console.WriteLine("Connecting server ..."); MailClient oClient = new MailClient("TryIt"); oClient.Connect(oServer); Imap4Folder[] folders = oClient.GetFolders(); // find source folder Imap4Folder folder = FindFolder("Inbox", folders); if (folder == null) { throw new Exception("Source folder not found!"); } // select source folder oClient.SelectFolder(folder); // find dest folder Imap4Folder destFolder = FindFolder("Deleted Items", folders); if (destFolder == null) { throw new Exception("Dest folder not found!"); } MailInfo[] infos = oClient.GetMailInfos(); Console.WriteLine("Total {0} email(s) in source folder", infos.Length); for (int i = 0; i < infos.Length; i++) { MailInfo info = infos[i]; Console.WriteLine("Moving {0}/{1} ...", i + 1, infos.Length); oClient.Move(info, destFolder); } // Because IMAP only marks source emails as deleted with move method // Use Expunge method to expunge source emails permanently if (oServer.Protocol == ServerProtocol.Imap4) { oClient.Expunge(); } oClient.Logout(); Console.WriteLine("Completed!"); } catch (Exception ep) { Console.WriteLine("Error: {0}", ep.Message); } } }
[C++/CLI - Move email to specified folder] using namespace System; using namespace System::Globalization; using namespace System::IO; using namespace EAGetMail; //add EAGetMail namespace // if you want to search sub folder, use parentfolder\\subfolder as folderPath // for example: inbox\\mysubfolder Imap4Folder^ FindFolder(String^ folderPath, array<Imap4Folder^> ^folders) { for (int i = 0; i < folders->Length; i++) { Imap4Folder^ folder = folders[i]; // Folder was found. if (String::Compare(folder->LocalPath, folderPath, true) == 0) return folder; folder = FindFolder(folderPath, folder->SubFolders); if (folder != nullptr) return folder; } // No folder found return nullptr; } void MoveEmail(String ^server, String ^user, String ^password, bool useSsl) { try { // ExchangeEWS or ExchangeWebDAV protocol also supports Move method. // Most modern email servers require SSL/TLS connection, // set useSsl to true is recommended. MailServer ^oServer = gcnew MailServer(server, user, password, useSsl, ServerAuthType::AuthLogin, ServerProtocol::Imap4); // IMAP4 port is 143, IMAP4 SSL port is 993. // EWS/WebDAV, please ignore Port property. oServer->Port = (useSsl) ? 993 : 143; Console::WriteLine("Connecting server ..."); MailClient ^oClient = gcnew MailClient("TryIt"); oClient->Connect(oServer); array<Imap4Folder^> ^folders = oClient->GetFolders(); // find source folder Imap4Folder ^ folder = FindFolder("Inbox", folders); if (folder == nullptr) { throw gcnew Exception("source folder not found!"); } // select source folder oClient->SelectFolder(folder); // find dest folder Imap4Folder ^destFolder = FindFolder("Deleted Items", folders); if (destFolder == nullptr) { throw gcnew Exception("Dest folder not found!"); } array<MailInfo^> ^infos = oClient->GetMailInfos(); Console::WriteLine("Total {0} email(s) in source folder", infos->Length); for (int i = 0; i < infos->Length; i++) { MailInfo ^info = infos[i]; Console::WriteLine("Moving {0}/{1} ...", i + 1, infos->Length); oClient->Move(info, destFolder); } // Because IMAP only marks source emails as deleted with move method // Use Expunge method to expunge source emails permanently if (oServer->Protocol == ServerProtocol::Imap4) { oClient->Expunge(); } oClient->Logout(); Console::WriteLine("Completed!"); } catch (Exception ^ep) { Console::WriteLine("Error: {0}", ep->Message); } }

See Also

MailClient.Copy Method
MailClient.SelectFolder Method