public void CheckBodyStructure() { ImapConnect connection = new ImapConnect(_host, _port, _ssl); ImapCommand command = new ImapCommand(connection); ImapAuthenticate authenticate = new ImapAuthenticate(connection, _user, _pass); connection.Open(); authenticate.Login(); ImapMailbox mailbox = command.Select("INBOX"); ImapMailboxMessage message = new ImapMailboxMessage(); message.ID = 1; message = command.FetchBodyStructure(message); authenticate.Logout(); connection.Close(); }
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> /// Retreives the content of a particular body part. /// </summary> /// <param name="message">A ImapMailboxMessage object.</param> /// <param name="part">A numeric value representing the body part in the message.</param> /// <returns>Returns an ImapMailboxMessage object.</returns> public ImapMailboxMessage FetchBodyPart(ImapMailboxMessage message, int part) { if (!(Connection.ConnectionState == ConnectionState.Open)) NoOpenConnection(); Connection.Write(string.Format("FETCH {0} BODY[{1}]\r\n", message.ID, message.BodyParts[part].BodyPart)); string response = Connection.Read(); if (response.StartsWith("*")) message.BodyParts[part].Data = ParseBodyPart(message.BodyParts[part].ContentEncoding, message.BodyParts[part].Encoding); return message; }
/// <summary> /// Retreives the bodystructure of a message. /// </summary> /// <param name="message">A ImapMailboxMessage object.</param> /// <returns>Returns an ImapMailboxMessage object.</returns> public ImapMailboxMessage FetchBodyStructure(ImapMailboxMessage message) { if (!(Connection.ConnectionState == ConnectionState.Open)) NoOpenConnection(); Connection.Write(string.Format("FETCH {0} BODYSTRUCTURE\r\n", message.ID)); string response = Connection.Read(); if (response.StartsWith("*")) { response = response.Substring(response.IndexOf(" (", response.IndexOf("BODYSTRUCTURE"))); message.Errors = response; message.BodyParts = BodyPartSplit(response.Trim().Substring(0, response.Trim().Length - 1)); response = Connection.Read(); for (int i = 0; i < message.BodyParts.Count && i < 2; i++) { if (message.BodyParts[i].ContentType.MediaType.ToLower() == "text/html") { message.HasHTML = true; message.HTML = i; } else if (message.BodyParts[i].ContentType.MediaType.ToLower() == "text/plain") { message.HasHTML = true; message.Text = i; } } return message; } else throw new ImapCommandInvalidMessageNumber("No UID found for message number" + message.ID); }
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; } } }