MailClient.Copy Method


Copies a specified email from selected folder to another folder on IMAP4 server.

[VisualĀ Basic 6.0]
Public Sub Copy( _
    info As MailInfo, _
    destFolder As Imap4Folder _
)
[Visual C++]
public: HRESULT Copy(
    IMailInfo* info,
    IImap4Folder* destFolder
);

Parameters

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

Example

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

[Visual Basic 6.0]
Public Sub CopyMail(ByVal Server As String, _
ByVal User As String, _
ByVal Password As String, _
ByVal SSLConnection As Boolean)
Const MailServerPop3 = 0
Const MailServerImap4 = 1

On Error GoTo ErrorHandle
    Dim oServer As New EAGetMailObjLib.MailServer
    oServer.Server = Server
    oServer.User = User
    oServer.Password = Password
    oServer.SSLConnection = SSLConnection
    oServer.Protocol = MailServerImap4
    If oServer.SSLConnection = True Then
        oServer.Port = 993
    Else
        oServer.Port = 143
    End If
    
    Dim oClient As New EAGetMailObjLib.MailClient
    oClient.LicenseCode = "TryIt"
    
    oClient.Connect oServer
    
    Dim arFolder
    arFolder = oClient.Imap4Folders
    Dim i
    Dim oFolder As EAGetMailObjLib.Imap4Folder
    For i = LBound(arFolder) To UBound(arFolder)
        Set oFolder = arFolder(i)
        If StrComp(oFolder.Name, "Inbox", 1) = 0 Then
            'select "INBOX" folder
            oClient.SelectFolder oFolder
            Exit For
        End If
    Next
    
    Dim destFolder As EAGetMailObjLib.Imap4Folder
    For i = LBound(arFolder) To UBound(arFolder)
        Set oFolder = arFolder(i)
        If StrComp(oFolder.Name, "Deleted Items", 1) = 0 Then
            'find "Deleted Items" folder
            Set destFolder = oFolder
            Exit For
        End If
    Next
    
    If destFolder Is Nothing Then
        MsgBox "Deleted Items not found!"
        Exit Sub
    End If
    
    Dim infos
    infos = oClient.GetMailInfos()
    For i = LBound(infos) To UBound(infos)
        'copy to  "Deleted Items" folder
        oClient.Copy infos(i), destFolder
    Next
    
    oClient.Logout
    
    Exit Sub
ErrorHandle:
    MsgBox Err.Description
End Sub

[VBScript]
Sub CopyMail(ByVal Server, _
ByVal User, _
ByVal Password, _
ByVal SSLConnection)

    Const MailServerPop3 = 0
    Const MailServerImap4 = 1

    Dim oServer
    Set oServer = CreateObject("EAGetMailObj.MailServer")
    oServer.Server = Server
    oServer.User = User
    oServer.Password = Password
    oServer.SSLConnection = SSLConnection
    oServer.Protocol = MailServerImap4
    If oServer.SSLConnection = True Then
        oServer.Port = 993
    Else
        oServer.Port = 143
    End If
    
    Dim oClient
    Set oClient = CreateObject("EAGetMailObj.MailClient")
    oClient.LicenseCode = "TryIt"
    
    oClient.Connect oServer
    
    Dim arFolder
    arFolder = oClient.Imap4Folders
    Dim i
    Dim oFolder
    For i = LBound(arFolder) To UBound(arFolder)
        Set oFolder = arFolder(i)
        If StrComp(oFolder.Name, "Inbox", 1) = 0 Then
            'select "INBOX" folder
            oClient.SelectFolder oFolder
            Exit For
        End If
    Next
    
    Dim destFolder
    For i = LBound(arFolder) To UBound(arFolder)
        Set oFolder = arFolder(i)
        If StrComp(oFolder.Name, "Deleted Items", 1) = 0 Then
            'find "Deleted Items" folder
            Set destFolder = oFolder
            Exit For
        End If
    Next
    
    If destFolder Is Nothing Then
        MsgBox "Deleted Items not found!"
        Exit Sub
    End If
    
    Dim infos
    infos = oClient.GetMailInfos()
    For i = LBound(infos) To UBound(infos)
        'copy to  "Deleted Items" folder
        oClient.Copy infos(i), destFolder
    Next
    
    oClient.Logout
End Sub

[Visual C++]
#include "eagetmailobj.tlh"
using namespace EAGetMailObjLib;

void CopyMail(LPCTSTR lpszServer,
    LPCTSTR lpszUser,
    LPCTSTR lpszPassword,
    VARIANT_BOOL SSLConnection )
{
    ::CoInitialize( NULL );

    const int MailServerPop3 = 0;
    const int MailServerImap4 = 1;

    try
    {
        IMailServerPtr oServer = NULL;
        oServer.CreateInstance("EAGetMailObj.MailServer");
        
        oServer->Server = lpszServer;
        oServer->User = lpszUser;
        oServer->Password = lpszPassword;
        oServer->SSLConnection = SSLConnection;
        oServer->Protocol = MailServerImap4;

        if( SSLConnection == VARIANT_TRUE )
            oServer->Port = 993;
        else
            oServer->Port = 143;

        IMailClientPtr oClient = NULL;
        oClient.CreateInstance("EAGetMailObj.MailClient");
        oClient->LicenseCode = _T("TryIt");
        oClient->Connect( oServer );

        _variant_t vtFolder = oClient->Imap4Folders ;
        SAFEARRAY *psa = vtFolder.parray;
        LONG UBound = 0, LBound = 0;
        SafeArrayGetLBound( psa, 1, &LBound );
        SafeArrayGetUBound( psa, 1, &UBound );
		        
        for( long i = LBound; i <= UBound; i++ )
        {
            _variant_t vt;
            SafeArrayGetElement( psa, &i, &vt );
            IImap4FolderPtr oFolder;
            vt.pdispVal->QueryInterface( __uuidof(IImap4Folder), (void**)&oFolder);
            if( _tcsicmp( (TCHAR*)oFolder->Name, _T("Inbox")) == 0 )
            {
                //select "INBOX" folder
                oClient->SelectFolder( oFolder );
                break;
            }
        }

        IImap4FolderPtr destFolder;
        for( long i = LBound; i <= UBound; i++ )
        {
            _variant_t vt;
            SafeArrayGetElement( psa, &i, &vt );
            IImap4FolderPtr oFolder;
            vt.pdispVal->QueryInterface( __uuidof(IImap4Folder), (void**)&oFolder);
            if( _tcsicmp( (TCHAR*)oFolder->Name, _T("Deleted Items") ) == 0 )
            {
                //find "Deleted Items" folder
                destFolder = oFolder;
                break;
            }
        }

        if( destFolder == NULL )
        {
            //"Deleted Items" not found
            throw _com_error( E_FAIL );
        }

        _variant_t vtInfos = oClient->GetMailInfos();
        psa = vtInfos.parray;
        
        SafeArrayGetLBound( psa, 1, &LBound );
        SafeArrayGetUBound( psa, 1, &UBound );
        for( long i = LBound; i <= UBound; i++ )
        {
            _variant_t vt;
            SafeArrayGetElement( psa, &i, &vt );
            IMailInfoPtr oInfo;
            vt.pdispVal->QueryInterface( __uuidof(IMailInfo), (void**)&oInfo);
            
            oClient->Copy( oInfo, destFolder );
        }

        oClient->Logout();
    }
    catch( _com_error &ep )
    {
        _tprintf( _T("%s\r\n"), (TCHAR*)ep.Description());
    }
    ::CoUninitialize();
}