| Author |  | 
      
        | opsoftware Newbie
 
  
 
 Joined: 02 July 2012
 Online Status: Offline
 Posts: 5
 | 
          I have a web based application that needs to access multiple mailboxes at the same time.
           | Posted: 02 July 2012 at 1:02pm | IP Logged |   |  
           | 
 |  
 Once accessed all messages are downloaded and stored in  an SQL database for use later.
 
 I use the following at the moment:
 
 
 
| Code: 
 
    
    | 
      
       | Dim pop As New Pop3()
 pop.BeginConnect(_pollHost, _pollPort, True, New AsyncCallback(AddressOf Pop3ConnectCallbackAndGetMessages), pop)
 
 |  |  |  
 and
 
 
 
| Code: 
 
    
    | 
      
       | Dim pop As Pop3 = CType(result.AsyncState, Pop3)
 ' End the pending asynchronous connection request
 pop.EndConnect()
 
 pop.Login(_pollUser, _pollPass)
 
 Dim msgs As MailMessageCollection = pop.DownloadEntireMessages()
 
 For Each msg As MailMessage In msgs
 
 'msg logic goes here
 
 Next
 
 pop.DeleteMessage(uids)
 pop.Disconnect()
 
 
 |  |  |  
 The issue is only the first mailbox is ever checked, i assume that it has something to do with the pop.EndConnect() at the start of the callback but then how can I get it to truely check mutliple mailboxes in different worker threads?
 
 Many thanks for any help
 
 Ozzie
 | 
       
        | Back to Top |     | 
       
       
        |  | 
        | Alex AfterLogic Support
 
  
  
 Joined: 19 November 2003
 Online Status: Offline
 Posts: 2207
 | 
          You need to create an array of Pop3 objects and issue BeginConnect for each of them in a loop. Each BeginConnect will create its own worker thread. This way, all of your mailboxes will be checked simultaneously (because BeginConnect returns immediately).
           | Posted: 03 July 2012 at 3:38am | IP Logged |   |  
           | 
 |  
 Regards,
 Alex
 | 
       
        | Back to Top |     | 
       
       
        |  | 
        | opsoftware Newbie
 
  
 
 Joined: 02 July 2012
 Online Status: Offline
 Posts: 5
 | 
          Hi Alex,
           | Posted: 03 July 2012 at 7:38am | IP Logged |   |  
           | 
 |  
 Thanks for the reply, apreciated.
 
 Do you have an examples of the best way to create this array?
 
 Currently I do a for each loop on data returned from an SQL query and within this loop I call the BeginConnect() method.
 
 regards,
 
 Ozzie
 | 
       
        | Back to Top |     | 
       
       
        |  | 
        | Alex AfterLogic Support
 
  
  
 Joined: 19 November 2003
 Online Status: Offline
 Posts: 2207
 | 
          No, we don't have such example. Actually, creating an array (or collection, list, it's not important how you'll store multiple objects) is quite a basic thing to write an example for it. Anyway, it's something like that (in C# syntax, for other .net langs the idea is the same):
           | Posted: 03 July 2012 at 11:42am | IP Logged |   |  
           | 
 |  
 
| Code: 
 
    
    | 
      
       | Pop3[] arr = new Pop3[10]; // number of simult. threads you need
 for (int i = 0; i < 10; i++)
 {
 Pop3 pop = new Pop3();
 arr[ i ] = pop;
 pop.BeginConnect ...
 }
 
 |  |  |  
 That's how arrays of objects are created in .NET
 
 Regards,
 Alex
 | 
       
        | Back to Top |     | 
       
       
        |  | 
        | opsoftware Newbie
 
  
 
 Joined: 02 July 2012
 Online Status: Offline
 Posts: 5
 | 
          Ahh OK, I thought it may have been something specific to MailBee did not think it would be as simple as that.
           | Posted: 04 July 2012 at 12:34am | IP Logged |   |  
           | 
 |  
 Thanks for the heads up much apreciated.
 
 Ozzie
 | 
       
        | Back to Top |     | 
       
       
        |  | 
        | opsoftware Newbie
 
  
 
 Joined: 02 July 2012
 Online Status: Offline
 Posts: 5
 | 
          Hi Alex,
           | Posted: 09 July 2012 at 12:40pm | IP Logged |   |  
           | 
 |  
 This is what I have come up with and does seem to work however when I debug the pop3 async callback I get the following error: There is already a connection in progress.
 
 This would mean to me that a new thread is not being created can you see where I am going wrong with this?
 
 
 
| Code: 
 
    
    | 
      
       | Public Class PopObject
 Public Property Pop() As Pop3
 Get
 Return m_Pop
 End Get
 Set(value As Pop3)
 m_Pop = Value
 End Set
 End Property
 Private m_Pop As Pop3
 
 'ID of the POP3 Queue
 Public Property Pop3QId() As Integer
 Get
 Return m_Pop3QId
 End Get
 Set(value As Integer)
 m_Pop3QId = value
 End Set
 End Property
 Private m_Pop3QId As Integer
 
 'POP3 Server
 Public Property Pop3ServerName() As String
 Get
 Return m_Pop3ServerName
 End Get
 Set(value As String)
 m_Pop3ServerName = Value
 End Set
 End Property
 Private m_Pop3ServerName As String
 
 'POP3 Port
 Public Property Pop3Port() As Integer
 Get
 Return m_Pop3Port
 End Get
 Set(value As Integer)
 m_Pop3Port = Value
 End Set
 End Property
 Private m_Pop3Port As Integer
 
 'POP3 User
 Public Property Pop3UserName() As String
 Get
 Return m_Pop3UserName
 End Get
 Set(value As String)
 m_Pop3UserName = Value
 End Set
 End Property
 Private m_Pop3UserName As String
 
 'POP3 User Password
 Public Property Pop3Password() As String
 Get
 Return m_Pop3Password
 End Get
 Set(value As String)
 m_Pop3Password = Value
 End Set
 End Property
 Private m_Pop3Password As String
 
 'POP3 SSL?
 Public Property Pop3SSL() As Boolean
 Get
 Return m_Pop3SSL
 End Get
 Set(value As Boolean)
 m_Pop3SSL = value
 End Set
 End Property
 Private m_Pop3SSL As Boolean
 
 'POP3 Reply Email
 Public Property Pop3Email() As String
 Get
 Return m_Pop3Email
 End Get
 Set(value As String)
 m_Pop3Email = value
 End Set
 End Property
 Private m_Pop3Email As String
 
 End Class
 
 |  |  |  
 Data Connection and Polling
 
 
 
| Code: 
 
    
    | 
      
       | Dim objConnection As SqlConnection
 Dim objDataAdapter As SqlDataAdapter
 
 'Set the license Key
 MailBee.Global.LicenseKey = "MN700-F73F40613F063F493F3BD64A2F3D-C92D"
 
 objConnection = New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("OPSDConnectionString").ConnectionString)
 objDataAdapter = New SqlDataAdapter("SELECT id, pollHost, pollPort, pollUser, pollPass, pollType, emailaddress, pollSSL FROM tbldepartment WHERE pollHost IS NOT NULL", objConnection)
 
 Try
 
 Dim ds As New DataSet()
 objDataAdapter.Fill(ds)
 
 For Each dr As DataRow In ds.Tables(0).Rows
 
 If dr("pollType") = 1 Then
 
 'POP3
 Dim pop As New Pop3()
 Dim popObject As New PopObject()
 If True Then
 popObject.Pop = pop
 popObject.Pop3QId = CInt(dr("id"))
 popObject.Pop3ServerName = dr("pollHost").ToString()
 popObject.Pop3Port = CInt(dr("pollPort"))
 popObject.Pop3UserName = dr("pollUser").ToString()
 popObject.Pop3Password = dr("pollPass").ToString()
 popObject.Pop3SSL = CBool(dr("pollSSL"))
 popObject.Pop3Email = dr("emailaddress").ToString()
 End If
 
 popObject.Pop.BeginConnect(popObject.Pop3ServerName, popObject.Pop3Port, True, New AsyncCallback(AddressOf Pop3ConnectCallbackAndGetMessages), popObject)
 
 Else
 
 'IMAP
 Dim imap As New Imap()
 Dim imapObject As New ImapObject()
 If True Then
 imapObject.Imap = imap
 imapObject.ImapQId = CInt(dr("id"))
 imapObject.ImapServerName = dr("pollHost").ToString()
 imapObject.ImapPort = CInt(dr("pollPort"))
 imapObject.ImapUserName = dr("pollUser").ToString()
 imapObject.ImapPassword = dr("pollPass").ToString()
 imapObject.ImapSSL = CBool(dr("pollSSL"))
 imapObject.ImapEmail = dr("emailaddress").ToString()
 End If
 
 imapObject.Imap.BeginConnect(imapObject.ImapServerName, imapObject.ImapPort, Nothing, Nothing, New AsyncCallback(AddressOf ImapConnectCallbackAndGetMessages), imapObject)
 
 End If
 
 Next
 
 Catch ex As Exception
 
 Elmah.ErrorSignal.FromCurrentContext().Raise(ex)
 
 Finally
 
 objDataAdapter.Dispose()
 objConnection.Close()
 objConnection.Dispose()
 
 
 End Try
 
 |  |  |  
 POP3 Callback
 
 
 
| Code: 
 
    
    | 
      
       | 
 Private Shared Sub Pop3ConnectCallbackAndGetMessages(ByVal result As IAsyncResult)
 
 Dim popObject As PopObject = CType(result.AsyncState, PopObject)
 
 ' End the pending asynchronous connection request
 'popObject.Pop.EndConnect()
 
 If popObject.Pop3SSL = True Then
 
 'Set SSL Mode to On
 popObject.Pop.SslMode = SslStartupMode.OnConnect
 
 End If
 
 Try
 'Supply username and password
 popObject.Pop.Login(popObject.Pop3UserName, popObject.Pop3Password) '<-- Error is triggered here
 
 Catch ex As Exception
 
 Elmah.ErrorSignal.FromCurrentContext().Raise(ex)
 
 End Try
 
 Try
 End Sub
 
 |  |  |  
 Emails are indeed polled, stored in the database and removed from the inbox, however the first connection is made and if I uncomment popObject.Pop.EndConnect() then the connection is closed and the second mailbox is polled instead.
 
 This means that I am not creating a new thread and I cannot see where I am going wrong :-(
 
 Thanks for any pointers you can give.
 
 Regards,
 
 Ozzie
 
 | 
       
        | Back to Top |     | 
       
       
        |  | 
        | Alex AfterLogic Support
 
  
  
 Joined: 19 November 2003
 Online Status: Offline
 Posts: 2207
 | 
          First of all, I don't see where you store your Pop3 objects. You create a new object each time the loop iterates and never store it in an array or collection. Garbage collector can remove these objects at any time then.
           | Posted: 10 July 2012 at 4:48am | IP Logged |   |  
           | 
 |  
 Also, you're not catching exception at popObject.Pop.EndConnect() line so the thread simply dies. Instead you should catch an exception to check what's going on there.
 
 Regards,
 Alex
 | 
       
        | Back to Top |     | 
       
       
        |  |