/// <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) { System.Text.Encoding encodingTR = System.Text.Encoding.GetEncoding("ISO-8859-9"); 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.ID)) { response = response.Substring(response.IndexOf("* " + message.ID), response.Length - response.IndexOf("* " + message.ID)); } response = ImapDecode.Decode(response); //response = Connection.Read(); if (response.StartsWith("*")) { //TODO Bu satýr araþtýrýlacak. message.BodyParts[part].Data = ParseBodyPart(message.BodyParts[part].ContentEncoding, encodingTR); //message.BodyParts[part].Data = message.BodyParts[part].Data.Substring(0, (int) message.BodyParts[part].Size); } 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(); string response = getCommand(); if (!response.StartsWith("* " + message.ID)) { response = response.Substring(response.IndexOf("* " + message.ID), response.Length - response.IndexOf("* " + message.ID)); } response = ImapDecode.Decode(response); response = searchTheConnString(response); 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++) { //ToLower added if (message.BodyParts[i].ContentType.MediaType == "TEXT/HTML") { message.HasHTML = true; message.HTML = i; } //ToLower added else if (message.BodyParts[i].ContentType.MediaType == "TEXT/PLAIN") { message.HasHTML = true; message.Text = i; } } //response = Connection.Read(); return(message); } else { throw new ImapCommandInvalidMessageNumber("No UID found for message number" + message.ID); } }
private string ParseBodyPart(IMAP.BodyPartEncoding encoding, Encoding en) { string response; en = System.Text.Encoding.GetEncoding("ISO-8859-9"); StringBuilder sb = new StringBuilder(""); do { response = Connection.Read(); if (response.IndexOf("FLAGS") != -1) { continue; } if (Regex.IsMatch(response, patFetchComplete)) { break; } if (encoding == IMAP.BodyPartEncoding.BASE64) { sb.Append(response + '\r' + '\n'); } else if (encoding == IMAP.BodyPartEncoding.QUOTEDPRINTABLE) { response = response.Replace("=FC", "ü"); response = response.Replace("=DC", "Ü"); response = response.Replace("=F0", "ð"); response = response.Replace("=D0", "Ð"); response = response.Replace("=F6", "ö"); response = response.Replace("=D6", "Ö"); response = response.Replace("=E7", "ç"); response = response.Replace("=C7", "Ç"); response = response.Replace("=FD", "ý"); response = response.Replace("=DD", "Ý"); response = response.Replace("=FE", "þ"); response = response.Replace("=DE", "Þ"); response = response.Replace("=3D", "="); response = response.Replace("_", " "); response = response.Replace("=5F", "_"); if (response.EndsWith("=") || response.EndsWith(")")) { sb.Append(response.Substring(0, response.Length - 1)); } else { sb.AppendLine(response); } } else { sb.AppendLine(response + '\r' + '\n'); } } while (true); //} while (!(response.EndsWith("==") || response == ")")); if (sb.ToString().Trim().EndsWith(")")) { sb = sb.Remove(sb.ToString().IndexOf(")"), 1); } if (encoding != BodyPartEncoding.BASE64) { return(ImapDecode.Decode(sb.ToString(), en)); } // return sb.ToString().Substring(0,sb.Length-15); //----------------------------------// return(sb.ToString()); //----------------------------------// }
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))); response = ImapDecode.Decode(response); 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, @"\(""([^""]+)""")).Success) { Match subMatch; subMatch = Regex.Match(match.Groups[1].ToString(), @"(-\d+.*|-\d+.*|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); } Message.Subject = response.Substring(0, response.IndexOf("((")).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))); } while (response.StartsWith("*")); //match = Regex.Match(response, @"\(FLAGS \(([\w\\]+)\) INTERNALDATE ""([^""]+)"" RFC822\.SIZE (\d+) ENVELOPE \(""([^""]+)"" ""([^""]+)"" \(\(NIL NIL ""([^""]+""\)\)"); } }
private ImapAddressList CreateCollection(string AddressList) { if (AddressList.ToCharArray()[0] == '(') { int num = 1; string temp = "("; for (int i = 1; i < AddressList.Length; i++) { if (AddressList.ToCharArray()[i] == '(') { num++; } else if (AddressList.ToCharArray()[i] == ')' && num > 1) { num--; } else { temp += AddressList.ToCharArray()[i]; } } AddressList = temp; } AddressList = Regex.Replace(AddressList, "\\\\\"", "'"); AddressList = Regex.Replace(AddressList, @"\.\.", "."); //if (AddressList.Contains("Carleton")) // throw new Exception(); ImapAddressList Addresses = new ImapAddressList(); MatchCollection matches = Regex.Matches(AddressList, @"\(((?>\((?<LEVEL>)|\)(?<-LEVEL>)|(?! \( | \) ).)+(?(LEVEL)(?!)))\)$"); foreach (Match match in matches) { StringBuilder displayName = new StringBuilder(""); StringBuilder email = new StringBuilder(""); string value = match.Groups[1].ToString().Trim(); // Display Name if (!(value.StartsWith("NIL"))) { Match sub; if ((sub = Regex.Match(value, @"^{(\d+)}(.*)")).Success) { value = "\"" + sub.Groups[2].Value.Insert(Convert.ToInt32(sub.Groups[1].Value), "\""); } sub = Regex.Match(value, @"""([^""]+)"""); displayName.Append(sub.Groups[1].ToString()); value = value.Substring(sub.Length).Trim(); } else { value = value.Substring(3).Trim(); } // Display Name Extended if (!(value.StartsWith("NIL"))) { Match sub; if ((sub = Regex.Match(value, @"^{(\d+)}(.*)")).Success) { value = "\"" + sub.Groups[2].Value.Insert(Convert.ToInt32(sub.Groups[1].Value), "\""); } sub = Regex.Match(value, @"""([^""]+)"""); displayName.Append(" " + sub.Groups[1].ToString()); value = value.Substring(sub.Length).Trim(); } else { value = value.Substring(3).Trim(); } // Email Prefix if (!(value.StartsWith("NIL"))) { Match sub; if ((sub = Regex.Match(value, @"^{(\d+)}(.*)")).Success) { value = "\"" + sub.Groups[2].Value.Insert(Convert.ToInt32(sub.Groups[1].Value), "\""); } sub = Regex.Match(value, @"""([^""]+)"""); email.Append(sub.Groups[1].ToString()); value = value.Substring(sub.Length).Trim(); } else { value = value.Substring(3).Trim(); } // Email Suffix if (!(value.StartsWith("NIL"))) { Match sub; if ((sub = Regex.Match(value, @"^{(\d+)}(.*)")).Success) { value = "\"" + sub.Groups[2].Value.Insert(Convert.ToInt32(sub.Groups[1].Value), "\""); } sub = Regex.Match(value, @"""([^""]+)"""); email.Append("@" + sub.Groups[1].ToString()); value = value.Substring(sub.Length).Trim(); } else { value = value.Substring(3).Trim(); } if (email.ToString().IndexOf('@') < 0) { email = new StringBuilder("undisclosed-recipients@localhost"); } ImapAddress address = new ImapAddress(email.ToString(), ImapDecode.Decode(displayName.ToString())); Addresses.Add(address); return(Addresses); } return(Addresses); }