execIMAP4Command Method

Executes an IMAP4 command.

[Syntax]
C++: HRESULT execIMAP4Command( BSTR Command, BSTR* pVal )
Visual Basic: execIMAP4Command( Command As String ) As String
C#: string execIMAP4Command( string Command )

Parameters

Command

Command line to be executed.

Return Value

This method returns the server response.

Remarks

The POPMAIN object was built based on POP3 protocol before, although now it supports retrieving, deleting email from IMAP4 server. However, it can't implement all features of IMAP4 protocol by old methods. execIMAP4Command method helps developers to implement the all features of IMAP4 such as "Create Folder", "Delete Folder", "Move Email", "Copy Email" and etc. To learn more detail, please refer to RFC 2060 and IMAP4Folder sample in anpop installation package.

Usage Example

[Visual Baisc]
Function IsGoodResponse(ByVal s As String, ByVal id As String) As Boolean
    id = id & " OK"
    If InStr(1, s, id, vbTextCompare) > 0 Then
        IsGoodResponse = True
    Else
        IsGoodResponse = False
    End If
End Function
		
Sub QueryFolders( imap4Server As String, imap4User As String, imap4Password As String )
  Dim oImap4 As ANPOPLib.POPMAIN
  Set oImap4 = new ANPOPLib.POPMAIN 'Create object instance

  oImap4.IMAP4Connection = 1
  oImap4.ServerPort = 143
 
  nRet = oImap4.Connect( imap4Server, imap4User, imap4Password ) 'Connect IMAP4 server
  If nRet <> 0 Then
    MsgBox "error with connecting server"
    goto ErrorHandler
  End If

  Dim cmdId, response, command
  cmdId = "A001"
  command = cmdId & " LIST """" *" & Chr(13) & Chr(10)
    
  response = Trim(oImap4.execIMAP4Command(command))
    
  If response = "" Then
     MsgBox "Networking timeout, please try it later!"
     GoTo ErrorHandle
  End If
    
  If Not IsGoodResponse(response, cmdId) Then
      MsgBox response
      GoTo ErrorHandle
  End If
  
  Dim arLine
  arLine = Split(response, Chr(13) & Chr(10))
 
  Dim i, nCount
  nCount = UBound(arLine)
  Dim folder, s
  For i = LBound(arLine) To nCount
    s = Trim(arLine(i))
    s = Replace(s, Chr(10), "")
    s = Replace(s, Chr(13), "")
        
    If Trim(oImap4.ParseIMAP4Response(s, 0)) = "*" Then
      folder = oImap4.ParseIMAP4Response(s, 4)
      folder = oImap4.DecodeIMAP4Folder(folder)
      folder = Replace(folder, """", "")
      MsgBox folder
    End If
  Next
 
ErrorHandler:
  Call oImap4.Close() 'Close connection
  Set oImap4 = Nothing

End Sub
[C#]
public void VerifyResponse( string szId, string response )
{
  string s;
  s = szId.ToUpper();
  s += " OK";
  string r = response.ToUpper();
  if( r.Length == 0 )
  {
    throw new Exception( "Networking timeout, please try it later!" );
  }

  if( r.IndexOf( s ) < 0 )
  {
    throw new Exception( response );
  }
}		

public void QueryFolders( string imap4Server, string imap4User, string imap4Password  )
{
  POPMAINClass oImap4 = new POPMAINClass();  //Create object instance
  int nRet = 0;
    
  try
  {
    oImap4.IMAP4Connection = 1;
    oImap4.ServerPort = 143;
      
    nRet = oImap4.Connect( imap4Server, 
                          imap4User, 
                          imap4Password ); //Connect IMAP4 server
    if( nRet != 0 )
      throw new Exception( "error with Connect" );
    
    string cmdId, response, command;
    cmdId = "A001";
    command = String.Format( "{0} LIST \"\" *\r\n", cmdId );
    response = oImap4.execIMAP4Command( command ).Trim();
    VerifyResponse( cmdId, response ); 
    
	string[] ar = response.Split( "\n".ToCharArray() );
	for( int i = 0; i < ar.Length; i++ )
	{
      string s = ar[i].Trim("\r\n".ToCharArray());
      if( s.Length == 0 )
        continue;
        
      if( s[0] != '*' )
        continue;
      
      string folder = oImap4.ParseIMAP4Response(s, 4);
      folder = oImap4.DecodeIMAP4Folder(folder);
      folder = folder.Trim( "\"".ToCharArray());
      Console.WriteLine( folder );
	}    
  }
  catch( Exception e )
  {
    Console.WriteLine( e.Message );
  }
  
  oImap4.Close(); //Close connection
  oImap4 = null;
}

See Also

EncodeIMAP4Folder Method
DecodeIMAP4Folder Method
ParseIMAP4Response Method


2001-2007 © Copyright AdminSystem Software Limited. All rights reserved.