public void ImapFullSslPlainTest() { _connection = new ImapConnect(_sslhost, 993, true); _authentication = new ImapAuthenticate(_connection, _ssluser, _sslpass); _command = new ImapCommand(_connection); Assert.IsTrue(_connection.Open()); Assert.IsTrue(_connection.State == ConnectionState.Connected); _connection.LoginType = LoginType.PLAIN; _authentication.Login(); Assert.IsTrue(_connection.State == ConnectionState.Open); _mailbox = _command.Examine("Inbox"); Assert.IsTrue(_mailbox.Exist != 0); _authentication.Logout(); Assert.IsTrue(_connection.State == ConnectionState.Closed); _connection.Close(); _connection.Dispose(); }
public void ImapFullCramMd5Test() { _connection = new ImapConnect(_stdhost); _authentication = new ImapAuthenticate(_connection, _stduser, _stdpass); _command = new ImapCommand(_connection); _connection.LoginType = LoginType.CRAM_MD5; Assert.IsTrue(_connection.Open()); Assert.IsTrue(_connection.State == ConnectionState.Connected); _authentication.Login(); Assert.IsTrue(_connection.State == ConnectionState.Open); _mailbox = _command.Examine("Inbox"); Assert.IsTrue(_mailbox.Exist != 0); _authentication.Logout(); Assert.IsTrue(_connection.State == ConnectionState.Closed); _connection.Close(); _connection.Dispose(); }
private ImapMailbox ParseMailbox(string mailbox) { ImapMailbox Mailbox = null; string response = Connection.Read(); if (response.StartsWith("*")) { Mailbox = new ImapMailbox(mailbox); Mailbox.Flags = new ImapMessageFlags(); do { Match match; if ((match = Regex.Match(response, @"(\d+) EXISTS")).Success) Mailbox.Exist = Convert.ToInt32(match.Groups[1].ToString()); else if ((match = Regex.Match(response, @"(\d+) RECENT")).Success) Mailbox.Recent = Convert.ToInt32(match.Groups[1].ToString()); else if ((match = Regex.Match(response, @" FLAGS \((.*?)\)")).Success) Mailbox.Flags.ParseFlags(match.Groups[1].ToString()); response = Connection.Read(); } while (response.StartsWith("*")); if ((response.StartsWith("OK") || response.Substring(7, 2) == "OK") && (response.ToUpper().Contains("READ/WRITE") || response.ToUpper().Contains("READ-WRITE"))) Mailbox.ReadWrite = true; } return Mailbox; }
private void ParseMessages(ref ImapMailbox Mailbox) { string response = string.Empty; if (Mailbox.Messages == null) Mailbox.Messages = new List<ImapMailboxMessage>(); do { response += Connection.Read(); } while (!(response.EndsWith("))") || Regex.IsMatch(response, patFetchComplete) || Regex.IsMatch(response, patFetchNotOk))); if (response.StartsWith("*")) { do { ImapMailboxMessage Message = new ImapMailboxMessage(); Message.Flags = new ImapMessageFlags(); Message.Addresses = new ImapAddressCollection(); Match match; if ((match = Regex.Match(response, @"\* (\d*)")).Success) Message.ID = Convert.ToInt32(match.Groups[1].ToString()); if ((match = Regex.Match(response, @"\(FLAGS \(([^\)]*)\)")).Success) Message.Flags.ParseFlags(match.Groups[1].ToString()); if ((match = Regex.Match(response, @"INTERNALDATE ""([^""]+)""")).Success) Message.Received = DateTime.Parse(match.Groups[1].ToString()); if ((match = Regex.Match(response, @"RFC822.SIZE (\d+)")).Success) Message.Size = Convert.ToInt32(match.Groups[1].ToString()); if ((match = Regex.Match(response, @"ENVELOPE")).Success) response = response.Remove(0, match.Index + match.Length); if ((match = Regex.Match(response, @"\(""(?:\w{3}\, )?([^""]+)""")).Success) { Match subMatch; subMatch = Regex.Match(match.Groups[1].ToString(), @"([\-\+]\d{4}.*|NIL.*)"); //(-\d{4}|-\d{4}[^""]+|NIL) DateTime d; DateTime.TryParse(match.Groups[1].ToString().Remove(subMatch.Index), out d); Message.Sent = d; Message.TimeZone = subMatch.Groups[1].ToString(); response = response.Remove(0, match.Index + match.Length); } string TOKEN; int TOKEN_OFFSET = 0; if (response.Contains("((")) TOKEN = "(("; else { TOKEN = "\" NIL"; TOKEN_OFFSET = 2; } Message.Subject = response.Substring(0, response.IndexOf(TOKEN) + TOKEN_OFFSET).Trim(); if (Message.Subject == "NIL") Message.Subject = null; else if ((match = Regex.Match(Message.Subject, "^\"(.*)\"$")).Success) Message.Subject = match.Groups[1].ToString(); Message.Subject = ImapDecode.Decode(Message.Subject); response = response.Remove(0, response.Substring(0, response.IndexOf("((")).Length); //if ((match = Regex.Match(response, @"(""[^""]*"" \(\(|NIL)")).Success) //{ // Message.Subject = match.Groups[1].ToString(); // if (Message.Subject == "NIL") // Message.Subject = null; // else if (Message.Subject.StartsWith("\"")) // Message.Subject = Message.Subject.Substring(1, Message.Subject.Length -5); // response = response.Remove(0, match.Index + match.Length - 3); //} if ((match = Regex.Match(response, @"""<([^>]+)>""\)\)")).Success) { Message.MessageID = match.Groups[1].ToString(); response = response.Remove(match.Index).Trim(); } if (response.EndsWith("NIL")) response = response.Remove(response.Length - 3); else { match = Regex.Match(response, @"""<([^>]+)>"""); Message.Reference = match.Groups[1].ToString(); } try { Message.Addresses = Message.Addresses.ParseAddresses(response); } catch (Exception ex) { Message.Errors = response + ex.ToString(); } Mailbox.Messages.Add(Message); response = string.Empty; do { response += Connection.Read(); } while (!(response.EndsWith("))") || Regex.IsMatch(response, patFetchComplete) || Regex.IsMatch(response, patFetchNotOk))); } while (response.StartsWith("*")); //match = Regex.Match(response, @"\(FLAGS \(([\w\\]+)\) INTERNALDATE ""([^""]+)"" RFC822\.SIZE (\d+) ENVELOPE \(""([^""]+)"" ""([^""]+)"" \(\(NIL NIL ""([^""]+""\)\)"); } }
/// <summary> /// Obtains message from a mailbox. /// </summary> /// <param name="Mailbox">The ImapMailbox object to add the messages to.</param> /// <returns>Returns a ImapMailbox object containing the messages.</returns> public ImapMailbox Fetch(ImapMailbox Mailbox) { if (!(Connection.ConnectionState == ConnectionState.Open)) NoOpenConnection(); Connection.Write(string.Format("FETCH 1:* ALL\r\n")); ParseMessages(ref Mailbox); return Mailbox; }
/// <summary> /// Retreives message headers for a message. /// </summary> /// <param name="message">A integer representing the message id.</param> /// <returns>Returns a ImapMailboxMessage object.</returns> public ImapMailboxMessage FetchHeaders(int message) { ImapMailbox Mailbox = new ImapMailbox(); return Fetch(Mailbox, message, message).Messages[0]; }
/// <summary> /// Obtains messages from a mailbox. /// </summary> /// <param name="Mailbox">The ImapMailbox object to add the messages to.</param> /// <param name="messages">A interger array of message ids.</param> /// <returns>Returns a ImapMailbox object containing the messages.</returns> public ImapMailbox Fetch(ImapMailbox Mailbox, int[] messages) { if (!(Connection.ConnectionState == ConnectionState.Open)) NoOpenConnection(); string messagelist = string.Empty; for (int i = 0; i < messages.Length; i++) messagelist += (i == 0) ? messages[i].ToString() : "," + messages[i]; Connection.Write(string.Format("FETCH {0} ALL\r\n", messagelist)); ParseMessages(ref Mailbox); return Mailbox; }
/// <summary> /// Obtains message from a mailbox. /// </summary> /// <param name="Mailbox">The ImapMailbox object to add the messages to.</param> /// <param name="begin">The first message to retreive.</param> /// <param name="end">The last message to retreive.</param> /// <returns>Returns a ImapMailbox object containing the messages.</returns> public ImapMailbox Fetch(ImapMailbox Mailbox, int begin, int end) { if (!(Connection.ConnectionState == ConnectionState.Open)) NoOpenConnection(); Connection.Write(string.Format("FETCH {0}:{1} ALL\r\n", begin, end)); ParseMessages(ref Mailbox); return Mailbox; }
/// <summary> /// Obtains messages from a mailbox. /// </summary> /// <param name="messages">A interger array of message ids.</param> /// <returns>Returns a ImapMailbox object containing the messages.</returns> public ImapMailbox Fetch(int[] messages) { ImapMailbox Mailbox = new ImapMailbox(); return Fetch(Mailbox, messages); }
/// <summary> /// Obtains message from a mailbox. /// </summary> /// <param name="begin">The first message to retreive.</param> /// <param name="end">The last message to retreive.</param> /// <returns>Returns a ImapMailbox object containing the messages.</returns> public ImapMailbox Fetch(int begin, int end) { ImapMailbox Mailbox = new ImapMailbox(); return Fetch(Mailbox, begin, end); }
void ParseMessages(ref ImapMailbox Mailbox) { List<string> responses = LoadMessages(); if (Mailbox.Messages == null) Mailbox.Messages = new List<ImapMailboxMessage>(); for (int i = 0; i < responses.Count; i++) { try { if (!responses[i].StartsWith("*")) continue; ImapMailboxMessage Message = new ImapMailboxMessage(); Message.Flags = new ImapMessageFlags(); Message.Addresses = new ImapAddressCollection(); Match match; if ((match = Regex.Match(responses[i], @"\* (\d*)")).Success) Message.ID = Convert.ToInt32(match.Groups[1].ToString()); if ((match = Regex.Match(responses[i], @"\(FLAGS \(([^\)]*)\)")).Success) Message.Flags.ParseFlags(match.Groups[1].ToString()); if ((match = Regex.Match(responses[i], @"INTERNALDATE ""([^""]+)""")).Success) Message.Received = DateTime.Parse(match.Groups[1].ToString()); if ((match = Regex.Match(responses[i], @"RFC822.SIZE (\d+)")).Success) Message.Size = Convert.ToInt32(match.Groups[1].ToString()); if ((match = Regex.Match(responses[i], @"ENVELOPE")).Success) responses[i] = responses[i].Remove(0, match.Index + match.Length); if ((match = Regex.Match(responses[i], @"\(""(?:\w{3}\, )?([^""]+)""")).Success) { Match subMatch; subMatch = Regex.Match(match.Groups[1].ToString(), @"([\-\+]\d{4}.*|NIL.*)"); //(-\d{4}|-\d{4}[^""]+|NIL) DateTime d; DateTime.TryParse(match.Groups[1].ToString().Remove(subMatch.Index), out d); Message.Sent = d; Message.TimeZone = subMatch.Groups[1].ToString(); responses[i] = responses[i].Remove(0, match.Index + match.Length); } string TOKEN = "(("; int TOKEN_OFFSET = responses[i].Length; if (responses[i].Contains("((")) TOKEN_OFFSET = 0; else { TOKEN = "\" NIL"; TOKEN_OFFSET = 2; } Message.Subject = responses[i].Substring(0, responses[i].IndexOf(TOKEN) + TOKEN_OFFSET).Trim(); if (Message.Subject == "NIL") Message.Subject = null; else if ((match = Regex.Match(Message.Subject, "^\"(.*)\"$")).Success) Message.Subject = match.Groups[1].ToString(); Message.Subject = ImapDecode.Decode(Message.Subject); if (responses[i].Contains("((")) responses[i] = responses[i].Remove(0, responses[i].Substring(0, responses[i].IndexOf("((")).Length); else { if (Message.Subject == string.Empty) // this is squirrely dont hate me Message.Subject = responses[i]; responses[i] = string.Empty; } if ((match = Regex.Match(responses[i], @"""<([^>]+)>""\)\)")).Success) { Message.MessageID = match.Groups[1].ToString(); responses[i] = responses[i].Remove(match.Index).Trim(); } if (responses[i].EndsWith("NIL")) responses[i] = responses[i].Remove(responses[i].Length - 3); else { match = Regex.Match(responses[i], @"""<([^>]+)>"""); Message.Reference = match.Groups[1].ToString(); } try { Message.Addresses = Message.Addresses.ParseAddresses(responses[i]); } catch (Exception ex) { Message.Errors = responses[i] + ex.ToString(); } Mailbox.Messages.Add(Message); } catch (Exception ex) // for debugging { throw ex; } } }