ANPOP Developers Center > Using ANPOP in Visual C#.NET

You should download the anpop installer and install it on your machine at first. If you want to distribute or deploy anpop without anpop installer, please click here to learn more.

Add reference of ANPOP to your project

First of all, make sure ANPOP has been installed on your machine. Then create a Windows Form project in Visual Studio.NET: select menu "Project" -> "Add Reference" -> COM, and select "ANPOP POP3 COMPONENT BUILD".

Declare and define objects

using ANPOPLib;
....
private POPMAINClass m_oPop3 = new POPMAINClass();
private POPMSGClass m_oMsg = new POPMSGClass();	
private MSGSTOREClass m_oStore = new MSGSTOREClass();

Although you can create new object instance each time, we recommend to declare object as member variable so that you just need to create it once and then enjoy  reuse those member variables without re-create it again.

How to retrieve email and parse email

The following code demonstrates how to retrieve and parse email.

public void Retrieve( string pop3Server, string pop3User, string pop3Password )
{
  POPMAINClass oPop3 = new POPMAINClass();  //Create object instance
  POPMSGClass oMsg = new POPMSGClass();
  int i = 0, nRet = 0, nCount = 0;
  string emailContent = "";
  string emailSubject = "", emailBody = "";
  
  try
  {
    nRet = oPop3.Connect( pop3Server, 
                          pop3User, 
                          pop3Password ); //Connect pop3 server
    if( nRet != 0 )
      throw new Exception( "error with Connect" );
  
    nCount = oPop3.GetTotalOfMails(); //Get total count of emails
    if( nCount == -1 )
      throw new Exception( "error with GetTotalOfMails" );
    else if( nCount == 0 )
      throw new Exception( "no email" );
  
    for( i = 1; i <= nCount; i++ )
    {
      emailContent = oPop3.Retrieve(i); //Retrieve email
      if( emailContent == null )
        throw new Exception( "error with Retrieve" );
    
      oMsg.RawContent = emailContent; //Pass email content to POPMSG
      emailSubject = oMsg.GetSubject();  //Parse email subject
      emailBody = oMsg.GetBodyText(); //Parse email body
      nRet = oMsg.ExportFile( 
               String.Format( "c:\\pop3Test_{0}.eml", i )); //Save email
      if( nRet != 0 )
        throw new Exception( "error with ExportFile" );
    }
  }
  catch( Exception e )
  {
    Console.WriteLine( e.Message );
  }
  
  oPop3.Close(); //Close connection
  oPop3 = null;
  oMsg = null;
}

Asynchronous mode and event driving

The following code demonstrates how to bind events of ANPOP. 
Note: you should define the corresponding methods to handle those events.

m_oPop3.Asynchronous = 1;
#region Add event handlers for POPMAIN object.
_IEventThreadObjEvents_OnClosedEventHandler OnClosedEventHandler = 
  new _IEventThreadObjEvents_OnClosedEventHandler( POPMAIN_OnClosed );
m_oPop3.OnClosed += OnClosedEventHandler;
		    
_IEventThreadObjEvents_OnConnectedEventHandler OnConnectedEventHandler =
  new _IEventThreadObjEvents_OnConnectedEventHandler( POPMAIN_OnConnected );
m_oPop3.OnConnected += OnConnectedEventHandler;

_IEventThreadObjEvents_OnDeletedEventHandler OnDeletedEventHandler =
  new _IEventThreadObjEvents_OnDeletedEventHandler( POPMAIN_OnDeleted );
m_oPop3.OnDeleted += OnDeletedEventHandler;

_IEventThreadObjEvents_OnErrorEventHandler OnErrorEventHandler =
  new _IEventThreadObjEvents_OnErrorEventHandler( POPMAIN_OnError );
m_oPop3.OnError += OnErrorEventHandler;

_IEventThreadObjEvents_OnGetMsgHeaderEventHandler OnGetMsgHeaderEventHandler =
  new _IEventThreadObjEvents_OnGetMsgHeaderEventHandler( POPMAIN_OnGetMsgHeader );
m_oPop3.OnGetMsgHeader += OnGetMsgHeaderEventHandler;
			
_IEventThreadObjEvents_OnGetMsgIDEventHandler OnGetMsgIDEventHandler = 
  new _IEventThreadObjEvents_OnGetMsgIDEventHandler( POPMAIN_OnGetMsgID );
m_oPop3.OnGetMsgID += OnGetMsgIDEventHandler;
		
_IEventThreadObjEvents_OnGetMsgSizeEventHandler OnGetMsgSizeEventHandler =
  new _IEventThreadObjEvents_OnGetMsgSizeEventHandler( POPMAIN_OnGetMsgSize );
m_oPop3.OnGetMsgSize += OnGetMsgSizeEventHandler;

_IEventThreadObjEvents_OnGetSizeOfMailsEventHandler OnGetSizeOfMailsEventHandler =
  new _IEventThreadObjEvents_OnGetSizeOfMailsEventHandler( POPMAIN_OnGetSizeOfMails );
m_oPop3.OnGetSizeOfMails += OnGetSizeOfMailsEventHandler;

_IEventThreadObjEvents_OnGetTotalOfMailsEventHandler OnGetTotalOfMailsEventHandler =
  new _IEventThreadObjEvents_OnGetTotalOfMailsEventHandler( POPMAIN_OnGetTotalOfMails );
m_oPop3.OnGetTotalOfMails += OnGetTotalOfMailsEventHandler;
			
_IEventThreadObjEvents_OnReceivingEventHandler OnReceivingEventHandler =
  new _IEventThreadObjEvents_OnReceivingEventHandler( POPMAIN_OnReceiving );
m_oPop3.OnReceiving += OnReceivingEventHandler;

_IEventThreadObjEvents_OnRetrievedEventHandler OnRetrievedEventHandler =
  new _IEventThreadObjEvents_OnRetrievedEventHandler( POPMAIN_OnRetrieved );
m_oPop3.OnRetrieved += OnRetrievedEventHandler;

_IEventThreadObjEvents_OnRetrieveEventHandler OnRetrieveEventHandler =
  new _IEventThreadObjEvents_OnRetrieveEventHandler( POPMAIN_OnRetrieve );
m_oPop3.OnRetrieve += OnRetrieveEventHandler;
#endregion

Note*: You SHOULD NOT invoke Connect method any more while POPMAIN is connecting with pop3 server, otherwise this connection would be terminated automatically.

A Total Sample

Please refer to CSHARPSAMPLE1 in ANPOP installation package.

Free Email Support

Not enough? Please contact our technical support team.

Support@EmailArchitect.NET
VIP@EmailArchitect.NET(Registered User)

Remarks
We usually reply emails in 24hours. The reason for getting no response is likely that your smtp server bounced our reply. In this case, please try to use another email address to contact us. Your Hotmail or Yahoo email account is recommended.



2001-2010 © Copyright AdminSystem Software Limited. All rights reserved.   About us  Site Map