In synchronous mode, once a method is called, it returns to application after the method is complete. Therefore, if the runtime is long, your application cannot do anything before this method ends, which results "my application is blocked or halted". In contrast, in asynchronous mode, as method works in background, all methods return to application immediately no matter the running method is complete or not. The return value will pass to application via fired event.
Both of synchronous mode and asynchronous mode of POPMAIN object have the same performance. The difference is your application can do other thing while receving email with asynchronouse mode. Notice that ASP doesn't support event handle, so asynchronous mode can't be used in ASP. To learn how to receive email in ASP asynchronously, please click here.
How to implement asynchronous mode? We'll demonstrate it with Visual Basic.
First of all, you should add a reference of ANPOP into your project. Please click here to learn how to add reference of ANPOP to your current project. Then you MUST declare POPMAIN object as a member variable like this
Option Explicit Private WithEvents m_oPop3 As ANPOPLib.POPMAIN Sub Form_Load() Set m_oPop3 = New ANPOPLib.POPMAIN m_oPop3.Asynchronous = 1 End Sub
Why it MUST be declared as a member variable?
That is because of POPMAIN object maintains an inner worker thread to send email in background, if it is declared as a temporary variable, the POPMAIN object instance would be destroyed by Visual Basic before receiving email is finished.
Handle the events.
Secondly, you should add the following implementation in your code to handle the events fired by POPMAIN.
Private Sub m_oPop3_OnClosed() 'add your implementation code. End Sub Private Sub m_oPop3_OnConnected() 'add your implementation code. End Sub Private Sub m_oPop3_OnDeleted(ByVal lMsg As Long) 'add your implementation code. End Sub Private Sub m_oPop3_OnError(ByVal lError As Long, ByVal ErrDescription As String) 'add your implementation code. End Sub Private Sub m_oPop3_OnGetMsgID(ByVal sMsgID As String, ByVal lMsg As Long) 'add your implementation code. End Sub Private Sub m_oPop3_OnGetMsgSize(ByVal lSize As Long, ByVal lMsg As Long) 'add your implementation code. End Sub Private Sub m_oPop3_OnGetTotalOfMails(ByVal lCount As Long) 'add your implementation code. End Sub Private Sub m_oPop3_OnRetrieved(ByVal sData As String, ByVal lCount As Long) 'add your implementation code. End Sub
We didn't list all events of POPMAIN in above code, you can just implement the event you need in your application. The following sample demonstrates how to get the total count of emails on POP3 server in asynchronous mode.
Option Explicit
Private WithEvents m_oPop3 As ANPOPLib.POPMAIN
Private m_errStr
Sub Form_Load()
Set m_oPop3 = New ANPOPLib.POPMAIN
m_oPop3.Asynchronous = 1
End Sub
Sub Connect( server As String, user As String, password As String )
m_errStr = ""
m_oPop3.Connect server, user, password
End Sub
Private Sub m_oPop3_OnClosed()
If m_errStr <> "" Then
MsgBox m_errStr
End If
End Sub
Private Sub m_oPop3_OnConnected()
'get total count of emails asynchronously, the result will return in OnGetTotalOfMails
m_Pop3.GetTotalOfMails
End Sub
Private Sub m_oPop3_OnError(ByVal lError As Long, ByVal ErrDescription As String)
m_errStr = ErrDescription
End Sub
Private Sub m_oPop3_OnGetTotalOfMails(ByVal lCount As Long)
MsgBox "Total " & lCount & " email(s) on server"
m_oPop3.Close 'close connection.
End Sub
Note*: You SHOULD NOT invoke Connect method any more while POPMAIN is connecting with pop3 server, otherwise this connection would be terminated automatically. If yuu want to use POPMAIN object to access multiple POP3 account concurrently, please refer to Programming with Multiple Threadings.
2001-2007 © Copyright AdminSystem Software Limited. All rights reserved.