Imap Exception class which implements Imap releted exceptions
Inheritance: System.ApplicationException
Exemplo n.º 1
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="sslEnabled"> </param>
	    /// <exception cref="IMAP_ERR_LOGIN"
	    /// <exception cref="IMAP_ERR_INVALIDPARAM"
	    public void Login(string sHost, ushort nPort, string sUserId, string sPassword, bool sslEnabled=false ) 
		{
			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.Error ("Invalid Host name");
				throw e_invalidparam;
			}

			if (sUserId.Length == 0)
			{
				log.Error ("Invalid User Id");
				throw e_invalidparam;
			}

			if (sPassword.Length == 0)
			{
				log.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.Info ("Connected and Logged in already");
							return;
						}
						else
							LogOut();
					}
					else Disconnect();
				}
			}
				
			m_bIsConnected = false;
			m_bIsLoggedIn = false;
			
			try 
			{
                eImapResponse = Connect(sHost, nPort, sslEnabled);
				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.º 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>
		void Restore(bool bSelectFolder)
		{
			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);
				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>
        /// Connect to specified host and port
        /// </summary>
        /// <param name="sHost">Imap host</param>
        /// <param name="nPort">Imap port</param>
        /// <param name="sslEnabled"> </param>
        /// <returns>ImapResponseEnum type</returns>
        protected ImapResponseEnum Connect(string sHost, ushort nPort, bool sslEnabled = false)
        {
            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);
                if (sslEnabled)
                {
                    var sslStrm = new SslStream(m_oImapServ.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
                    //m_SSLStream.ReadTimeout = 30000;

                    try
                    {
                        sslStrm.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_oNetStrm = (Stream)sslStrm;
                    m_oRdStrm  = new StreamReader(sslStrm);
                }
                else
                {
                    m_oNetStrm = m_oImapServ.GetStream();
                    m_oRdStrm  = new StreamReader(m_oImapServ.GetStream());
                }


                string sResult = m_oRdStrm.ReadLine();
                if ((sResult.StartsWith(IMAP_OK_SERVER_RESPONSE) == true) || (sResult.StartsWith(IMAP_CAPABILITY_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;
            m_bSSLEnabled = sslEnabled;
            return(eImapResponse);
        }
Exemplo n.º 4
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);
				}
				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.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;

			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.º 5
0
        /// <summary>
        /// Connect to specified host and port
        /// </summary>
        /// <param name="sHost">Imap host</param>
        /// <param name="nPort">Imap port</param>
        /// <param name="sslEnabled"> </param>
        /// <returns>ImapResponseEnum type</returns>
        protected ImapResponseEnum Connect(string sHost, ushort nPort, bool sslEnabled=false)
        {
            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);
                if(sslEnabled)
                {

                    var sslStrm = new SslStream(m_oImapServ.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
                    //m_SSLStream.ReadTimeout = 30000;

                    try
                    {
                        sslStrm.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_oNetStrm = (Stream) sslStrm;
                    m_oRdStrm = new StreamReader(sslStrm);
                }else
                {
                    m_oNetStrm = m_oImapServ.GetStream();
                    m_oRdStrm = new StreamReader(m_oImapServ.GetStream());
                }

                string sResult = m_oRdStrm.ReadLine();
                if ((sResult.StartsWith(IMAP_OK_SERVER_RESPONSE) == true) || (sResult.StartsWith(IMAP_CAPABILITY_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;
            m_bSSLEnabled = sslEnabled;
            return eImapResponse;
        }