protected virtual bool IsServerCompatible( PopClient client )
 {
     foreach( string capability in client.Capabilities() )
     {
         if( 0 == string.Compare( capability, "uidl", true ) )
             return true;
     }
     return false;
 }
Exemple #2
0
        private int FetchMessages()
        {
            int count = 0;

            using(
            PopClient client = new PopClient()
            {
                Host = ConnectInfo.Host,
                Port = ConnectInfo.TcpPort,
                Username = ConnectInfo.Username,
                Password = ConnectInfo.Password,
                SslNegotiationMode = ConnectInfo.SslNegotiationMode,
                SslProtocol = ConnectInfo.SslProtocol,
                ReceiveTimeOut = ConnectInfo.Timeout,
                SendTimeOut = ConnectInfo.Timeout
            } )
            {
                string name = null;
                try
                {
                    client.ConnectAndAuthenticate();
                    if( !IsServerCompatible( client ) )
                        throw new NotSupportedException( "The server is not compatible. Please use a server that supports UIDL." );

                    OnCountReceived( client.GetMessageCount().ToString() );
                    _errorCount = 0; //if we get here, then the server is talking to us.

                    foreach( PopUid uid in client.GetMessageUids() )
                    {
                        /*
                        if( count >= 20 )  //DEBUG: short circuit here to watch threads exit
                            return 0;
                        */
                        if( MessageFileExists( uid.UniqueIdentifier ) )
                        {
                            OnPreviousDownloaded( uid.UniqueIdentifier );
                            client.DeleteMessage( uid.MessageNumber );
                        }
                        else
                        {
                            OnMessageRequested( uid.MessageNumber + " -> " + uid.UniqueIdentifier );
                            string temp = ConnectInfo.PathInfo.TempDownloadFile;

                            using( Stream stream = File.Create( temp ) )
                                client.WriteMessageToStream( stream, uid.MessageNumber );

                            name = StripIllegalFilenameChars( uid.UniqueIdentifier );
                            name = ConnectInfo.PathInfo.QueueDirectory + Path.DirectorySeparatorChar + name + ".eml";

                            File.Move( temp, name );
                            client.DeleteMessage( uid.MessageNumber );
                            count++;
                            OnMessageReceived( name );
                        }
                    }
                }
                catch( IOException ex )
                {
                    _errorCount++;
                    client.Disconnect();
                    SocketException se;
                    if( null != (se = ex.InnerException as SocketException) )
                    {
                        if( ((int)SocketError.TimedOut == se.ErrorCode) )
                        {

                            if( ConnectInfo.Timeout < PopConnectInfo.MaxTimeout )
                            {
                                if( ConnectInfo.Timeout < PopConnectInfo.MinTimeout )
                                    ConnectInfo.Timeout = PopConnectInfo.MinTimeout;
                                else
                                    ConnectInfo.Timeout *= 2;
                            }
                        }
                    }
                    Debug.WriteLine( ex );
                    OnServerError( ex.Message, ex );
                    if( null == name )
                        File.Delete( name );

                    if( _errorCount < MaxErrors )
                    {
                        Thread.Sleep( ErrorSleepTimeout );
                        return FetchMessages();
                    }
                    else
                        Die( ex );
                }
                catch( PopServerResponseErrException ex )
                {
                    _errorCount++;
                    client.Disconnect();
                    Debug.WriteLine( ex );
                    OnServerError( ex.Message, ex );
                    if( null == name )
                        File.Delete( name );

                    if( _errorCount < MaxErrors )
                    {
                        Thread.Sleep( ErrorSleepTimeout );
                        return FetchMessages();
                    }
                    else
                        Die( ex );
                }
            }
            return count;
        }