Exemplo n.º 1
0
        /// <summary>
        /// Connect to specified host and port
        /// </summary>
        /// <param name="sHost">Imap host</param>
        /// <param name="nPort">Imap port</param>
        /// <param name="useSSL">Use a secure connection</param>
        /// <returns>ImapResponseEnum type</returns>
        protected IMAPResponseEnum Connect(string sHost, ushort nPort, bool useSSL)
        {
            IMAP_COMMAND_VAL = 0;
            IMAPResponseEnum eImapResponse = IMAPResponseEnum.IMAP_SUCCESS_RESPONSE;
            IMAPException    e_connect     = new IMAPException(IMAPException.IMAPErrorEnum.IMAP_ERR_CONNECT, sHost);

            try
            {
                m_oImapServ = new TcpClient(sHost, nPort);
                m_oNetStrm  = m_oImapServ.GetStream();
                m_oRdStrm   = new StreamReader(m_oImapServ.GetStream());
                if (useSSL)
                {
                    m_SSLStream = new SslStream(m_oImapServ.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
                    //m_SSLStream.ReadTimeout = 30000;

                    try
                    {
                        m_SSLStream.AuthenticateAsClient(sHost);
                    }
                    catch (AuthenticationException e)
                    {
                        Console.WriteLine("Exception: {0}", e.Message);
                        if (e.InnerException != null)
                        {
                            Console.WriteLine("Inner exception: {0}", e.InnerException.Message);
                        }
                        Console.WriteLine("Authentication failed - closing the connection.");
                        m_oImapServ.Close();
                        return(IMAPResponseEnum.IMAP_FAILURE_RESPONSE);
                    }

                    m_rdr = new StreamReader(m_SSLStream);
                }



                string sResult = useSSL ? m_rdr.ReadLine() : m_oRdStrm.ReadLine();
                if (sResult.StartsWith(IMAP_OK_SERVER_RESPONSE) == true)
                {
                    Log(LogTypeEnum.IMAP, sResult);
                    eImapResponse = IMAPResponseEnum.IMAP_SUCCESS_RESPONSE;
                    Capability();
                }
                else
                {
                    Log(LogTypeEnum.IMAP, sResult);
                    eImapResponse = IMAPResponseEnum.IMAP_FAILURE_RESPONSE;
                }
            }
            catch
            {
                throw e_connect;
            }
            m_sHost = sHost;
            m_nPort = nPort;
            return(eImapResponse);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Restore the connection using available old data
        /// Select the sFolder if previously selected
        /// </summary>
        /// <param name="bSelectFolder">If true then it selects the folder</param>
        /// <param name="useSSL">Set to true if using a secure connection</param>
        void Restore(bool bSelectFolder, bool useSSL)
        {
            IMAPException e_insufficiantdata = new IMAPException(IMAPException.IMAPErrorEnum.IMAP_ERR_INSUFFICIENT_DATA);
            if (m_sHost.Length == 0 ||
                m_sUserId.Length == 0 ||
                m_sPassword.Length == 0)
            {
                throw e_insufficiantdata;
            }
            try
            {
                m_bIsLoggedIn = false;
                Login(m_sHost, m_nPort, m_sUserId, m_sPassword, useSSL);
                if (bSelectFolder && m_sMailboxName.Length > 0)
                {
                    if (m_bIsFolderSelected)
                    {
                        m_bIsFolderSelected = false;
                        SelectFolder(m_sMailboxName);
                    }
                    else if (m_bIsFolderExamined)
                    {
                        m_bIsFolderExamined = false;
                        ExamineFolder(m_sMailboxName);
                    }
                    else SelectFolder(m_sMailboxName);
                }
            }
            catch (Exception e)
            {
                throw e;
            }

        }
Exemplo n.º 3
0
        /// <summary>
        /// Examine the sFolder/mailbox after login
        /// </summary>
        /// <param name="sFolder">Mailbox folder</param>
        public void ExamineFolder(string sFolder)
        {
            if (!m_bIsLoggedIn)
            {
                try
                {
                    Restore(false, m_useSSL);
                }
                catch (IMAPException e)
                {
                    if (e.Type != IMAPException.IMAPErrorEnum.IMAP_ERR_INSUFFICIENT_DATA)
                        throw e;

                    throw new IMAPException(IMAPException.IMAPErrorEnum.IMAP_ERR_NOTCONNECTED);
                }
            }
            IMAPResponseEnum eImapResponse = IMAPResponseEnum.IMAP_SUCCESS_RESPONSE;
            IMAPException e_examine = new IMAPException(IMAPException.IMAPErrorEnum.IMAP_ERR_EXAMINE, sFolder);
            IMAPException e_invalidparam = new IMAPException(IMAPException.IMAPErrorEnum.IMAP_ERR_INVALIDPARAM);
            if (sFolder.Length == 0)
            {
                throw e_invalidparam;
            }
            if (m_bIsFolderExamined)
            {
                if (m_sMailboxName == sFolder)
                {
                    Log(LogTypeEnum.INFO, "Folder is already selected");
                    return;
                }
                else m_bIsFolderExamined = false;
            }
            ArrayList asResultArray = new ArrayList();
            string sCommand = IMAP_EXAMINE_COMMAND;
            sCommand += " " + sFolder + IMAP_COMMAND_EOL;
            try
            {
                eImapResponse = SendAndReceive(sCommand, ref asResultArray);
                if (eImapResponse == IMAPResponseEnum.IMAP_SUCCESS_RESPONSE)
                {
                    m_sMailboxName = sFolder;
                    m_bIsFolderExamined = true;
                }
                else throw e_examine;
            }
            catch (Exception e)
            {
                throw e;
            }
            //-------------------------
            // PARSE RESPONSE

            bool bResult = false;
            foreach (string sLine in asResultArray)
            {
                // If this is an unsolicited response starting with '*'
                if (sLine.IndexOf(IMAP_UNTAGGED_RESPONSE_PREFIX) != -1)
                {
                    // parse the line by space
                    string[] asTokens;
                    asTokens = sLine.Split(' ');
                    if (asTokens[2] == "EXISTS")
                    {
                        // The line will look like "* 2 EXISTS"
                        m_nTotalMessages = Convert.ToInt32(asTokens[1]);
                    }
                    else if (asTokens[2] == "RECENT")
                    {
                        // The line will look like "* 2 RECENT"
                        m_nRecentMessages = Convert.ToInt32(asTokens[1]);
                    }
                    else if (asTokens[2] == "[UNSEEN")
                    {
                        // The line will look like "* OK [UNSEEN 2]"
                        string sUIDPart = asTokens[3].Substring(0, asTokens[3].Length - 1);
                        m_nFirstUnSeenMsgUID = Convert.ToInt32(sUIDPart);
                    }
                }
                // If this line looks like "<command-tag> OK ..."
                else if (sLine.IndexOf(IMAP_SERVER_RESPONSE_OK) != -1)
                {
                    bResult = true;
                    break;
                }
            }

            if (!bResult)
                throw e_examine;

            m_examinedFolder = sFolder;

            string sLogStr = "TotalMessages[" + m_nTotalMessages.ToString() + "] ,";
            sLogStr += "RecentMessages[" + m_nRecentMessages.ToString() + "] ,";
            if (m_nFirstUnSeenMsgUID > 0)
                sLogStr += "FirstUnSeenMsgUID[" + m_nFirstUnSeenMsgUID.ToString() + "] ,";
            //Log(LogTypeEnum.INFO, sLogStr);

        }
Exemplo n.º 4
0
        /// <summary>
        /// Login to specified Imap host and port
        /// </summary>
        /// <param name="sHost">Imap server name</param>
        /// <param name="nPort">Imap server port</param>
        /// <param name="sUserId">User's login id</param>
        /// <param name="sPassword">User's password</param>        
        /// <param name="useSSL">Use secure connection</param>
        public void Login(string sHost, ushort nPort, string sUserId, string sPassword, bool useSSL)
        {
            m_useSSL = useSSL;

            IMAPResponseEnum eImapResponse = IMAPResponseEnum.IMAP_SUCCESS_RESPONSE;
            IMAPException e_login = new IMAPException(IMAPException.IMAPErrorEnum.IMAP_ERR_LOGIN, m_sUserId);
            IMAPException e_invalidparam = new IMAPException(IMAPException.IMAPErrorEnum.IMAP_ERR_INVALIDPARAM);

            if (sHost.Length == 0)
            {
                Log(LogTypeEnum.ERROR, "Invalid m_sHost name");
                throw e_invalidparam;
            }

            if (sUserId.Length == 0)
            {
                Log(LogTypeEnum.ERROR, "Invalid m_sUserId");
                throw e_invalidparam;
            }

            if (sPassword.Length == 0)
            {
                Log(LogTypeEnum.ERROR, "Invalid Password");
                throw e_invalidparam;
            }            

            if (m_bIsConnected)
            {
                if (m_bIsLoggedIn)
                {
                    if (m_sHost == sHost && m_nPort == nPort)
                    {
                        if (m_sUserId == sUserId &&
                            m_sPassword == sPassword)
                        {
                            Log(LogTypeEnum.INFO, "Connected and Logged in already");
                            return;
                        }
                        else
                            LogOut();
                    }
                    else Disconnect();
                }
            }

            m_bIsConnected = false;
            m_bIsLoggedIn = false;

            try
            {
                eImapResponse = Connect(sHost, nPort, useSSL);
                if (eImapResponse == IMAPResponseEnum.IMAP_SUCCESS_RESPONSE)
                {
                    m_bIsConnected = true;
                }
                else return;
            }
            catch (Exception e)
            {
                throw e;
            }

            ArrayList asResultArray = new ArrayList();
            string sCommand = IMAP_LOGIN_COMMAND;

            

            sCommand += " " + sUserId + " " + sPassword;
            sCommand += IMAP_COMMAND_EOL;
            try
            {
                eImapResponse = SendAndReceive(sCommand, ref asResultArray);
                if (eImapResponse == IMAPResponseEnum.IMAP_SUCCESS_RESPONSE)
                {
                    m_bIsLoggedIn = true;
                    m_sUserId = sUserId;
                    m_sPassword = sPassword;
                }
                else throw e_login;

            }
            catch (Exception e)
            {
                throw e;
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Connect to specified host and port
        /// </summary>
        /// <param name="sHost">Imap host</param>
        /// <param name="nPort">Imap port</param>
        /// <param name="useSSL">Use a secure connection</param>
        /// <returns>ImapResponseEnum type</returns>
        protected IMAPResponseEnum Connect(string sHost, ushort nPort, bool useSSL)
        {
            IMAP_COMMAND_VAL = 0;
            IMAPResponseEnum eImapResponse = IMAPResponseEnum.IMAP_SUCCESS_RESPONSE;
            IMAPException e_connect = new IMAPException(IMAPException.IMAPErrorEnum.IMAP_ERR_CONNECT, sHost);
            try
            {
                m_oImapServ = new TcpClient(sHost, nPort);
                m_oNetStrm = m_oImapServ.GetStream();
                m_oRdStrm = new StreamReader(m_oImapServ.GetStream());
                if (useSSL)
                {
                    m_SSLStream = new SslStream(m_oImapServ.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
                    //m_SSLStream.ReadTimeout = 30000;

                    try
                    {
                        m_SSLStream.AuthenticateAsClient(sHost);
                    }
                    catch (AuthenticationException e)
                    {
                        Console.WriteLine("Exception: {0}", e.Message);
                        if (e.InnerException != null)
                        {
                            Console.WriteLine("Inner exception: {0}", e.InnerException.Message);
                        }
                        Console.WriteLine("Authentication failed - closing the connection.");
                        m_oImapServ.Close();
                        return IMAPResponseEnum.IMAP_FAILURE_RESPONSE;
                    }

                    m_rdr = new StreamReader(m_SSLStream);
                }

                string sResult = useSSL ? m_rdr.ReadLine() : m_oRdStrm.ReadLine();
                if (sResult.StartsWith(IMAP_OK_SERVER_RESPONSE) == true)
                {
                    Log(LogTypeEnum.IMAP, sResult);
                    eImapResponse = IMAPResponseEnum.IMAP_SUCCESS_RESPONSE;
                    Capability();
                }
                else
                {
                    Log(LogTypeEnum.IMAP, sResult);
                    eImapResponse = IMAPResponseEnum.IMAP_FAILURE_RESPONSE;
                }
            }
            catch
            {
                throw e_connect;
            }
            m_sHost = sHost;
            m_nPort = nPort;
            return eImapResponse;
        }