Esempio n. 1
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
			{
				_bIsLoggedIn = false;
				Login(m_sHost, m_nPort, m_sUserId, m_sPassword);
				if (bSelectFolder && _mailboxName.Length > 0)
				{
					if (_isFolderSelected)
					{
						_isFolderSelected = false;
						SelectFolder(_mailboxName);
					}
					else if (_isFolderExamined)
					{
						_isFolderExamined = false;
						ExamineFolder(_mailboxName);
					}
					else SelectFolder(_mailboxName);
				}
			}
			catch (Exception e)
			{
				throw e;
			}
			
		}
Esempio n. 2
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>
	    /// <exception cref="IMAP_ERR_LOGIN"></exception>
	    /// <exception cref="IMAP_ERR_INVALIDPARAM"></exception>
	    public void Login(string sHost, ushort nPort, string sUserId, string sPassword ) 
		{
			ImapResponseEnum eImapResponse;
			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 (_bIsLoggedIn)
				{
					if (m_sHost == sHost && m_nPort == nPort)
					{
					    if (m_sUserId == sUserId &&
							m_sPassword == sPassword ) 
						{
							Log (LogTypeEnum.INFO, "Connected and Logged in already");
							return;
						}
					    LogOut();
					}
					else Disconnect();
				}
			}
				
			m_bIsConnected = false;
			_bIsLoggedIn = false;
			
			try 
			{
				eImapResponse = Connect(sHost, nPort);
				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)
				{
					_bIsLoggedIn = true;
					m_sUserId = sUserId;
					m_sPassword = sPassword;
				}
				else throw e_login;
				
			}
			catch (Exception e)
			{
				throw e;
			}
		}
Esempio n. 3
0
/// <summary>
/// Examine the sFolder/mailbox after login
/// </summary>
/// <param name="sFolder">Mailbox folder</param>
		public void ExamineFolder(string sFolder) 
		{
			if (!_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 (_isFolderExamined)
			{
				if (_mailboxName == sFolder)
				{
					Log(LogTypeEnum.INFO,"Folder is already selected");
					return;
				}
				else _isFolderExamined = 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)
				{
					_mailboxName = sFolder;
					_isFolderExamined = 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);

		}