/// <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++) { if (i == 0) { messagelist = messages[i].ToString(); } else { messagelist += (i % 100 == 1) ? messages[i].ToString() : "," + messages[i]; } if ((i % 100) == 0 || i == messages.Length - 1) { Connection.Write(string.Format("FETCH {0} ALL\r\n", messagelist)); ParseMessages(ref Mailbox, messages); messagelist = string.Empty; } } return(Mailbox); }
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); }
/// <summary> /// Obtains unseen messages from a mailbox /// </summary> /// <param name="mailBox">The ImapMailbox object to add the messages to</param> /// <returns></returns> public void FindUnseenMessageID(ImapMailbox mailBox) { if (!(Connection.ConnectionState == ConnectionState.Open)) { NoOpenConnection(); } Connection.Write(string.Format("SEARCH UNSEEN\r\n")); ParseSearchCommand(ref mailBox); }
/// <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> /// 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> /// Selects a mailbox to perform commands on. /// </summary> /// <param name="mailbox">The name of the mailbox to select.</param> /// <returns>Returns a mailbox object containing the properties of the mailbox.</returns> public ImapMailbox Select(string mailbox) { ImapMailbox Mailbox = null; if (!(Connection.ConnectionState == ConnectionState.Open)) { NoOpenConnection(); } Connection.Write("SELECT \"" + mailbox + "\"\r\n"); Mailbox = ParseMailbox(mailbox); return(Mailbox); }
/// <summary> /// Examines a mailbox. /// </summary> /// <param name="mailbox">The name of the mailbox to examine.</param> /// <returns>Returns a mailbox object containing the properties of the mailbox.</returns> public ImapMailbox Examine(string mailbox) { ImapMailbox Mailbox = null; if (!(Connection.ConnectionState == ConnectionState.Open)) { NoOpenConnection(); } Connection.Write("EXAMINE \"" + mailbox + "\"\r\n"); Mailbox = ParseMailbox(mailbox); return(Mailbox); }
private void ParseSearchCommand(ref ImapMailbox mailbox) { string response = string.Empty; int[] messagelist = null; if (mailbox.Messages == null) { mailbox.Messages = new List <ImapMailboxMessage>(); } do { response = Connection.Read(); if (response.EndsWith("completed.")) { break; } if (response.StartsWith("* SEARCH ")) { string msgList = response.Substring(9, response.Length - 9); char[] delimiterChars = { ' ' }; string[] ids = msgList.Split(delimiterChars); messagelist = new int [ids.GetLength(0)]; int i = 0; foreach (string s in ids) { messagelist[i] = System.Convert.ToInt32(s); i++; } } } while (!(response.EndsWith("completed.") || Regex.IsMatch(response, patFetchComplete))); if (messagelist != null) { mailbox = Fetch(mailbox, messagelist); } }
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 ""([^""]+""\)\)"); } }
/// <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="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)); }
/// <summary> /// Saves the new mail attachements to the given path /// </summary> public void SaveAttachements() { Thread.CurrentThread.CurrentUICulture = new CultureInfo("tr-TR", false); Thread.CurrentThread.CurrentCulture = new CultureInfo("tr-TR", false); if (m_MailMessageFilters.Length == 0) { throw new Exception("Okunacak mail filtreleri sisteme kaydedilmemiştir."); } try { m_Connection = new ImapConnect(); m_Connection.Hostname = m_Host; m_Connection.Port = Convert.ToInt32(m_Port); m_Connection.Open(); } catch (Exception ex) { throw new Exception("IMAP Server bağlantısı gerçekleştirilemedi!", ex); } try { ImapAuthenticate imapAuthanticate = new ImapAuthenticate(m_Connection, m_UserName, new Crypto().Decrypt(m_Password)); try { imapAuthanticate.Login(); } catch (Exception exd) { throw new Exception("Kullanıcı login sırasında hata oluştu. Lütfen kullanıcı adı ve parolayı tekrar gözden geçiriniz.", exd); } m_ImapMailbox = new ImapMailbox("inbox"); m_ImapCommand = new ImapCommand(m_Connection); m_ImapCommand.Select(m_ImapMailbox.Mailbox); ImapMailboxMessage imapMailboxMessage = new ImapMailboxMessage(); m_ImapCommand.FindUnseenMessageID(m_ImapMailbox); foreach (ImapMailboxMessage msg in m_ImapMailbox.Messages) { //nevzat: bazen msg.from null geliyor, şimdilik bu mailleri atlayalım if (msg == null || msg.From == null) { /*string d = msg.From + " - " + msg.Received + " - " + msg.HTML + " - " + msg.Errors; * throw new Exception(d);*/ continue; } for (int i = 0; i < m_MailMessageFilters.Length; i++) { if ((m_MailMessageFilters[i].From.Equals("") || msg.From.Address.Contains(m_MailMessageFilters[i].From)) && (m_MailMessageFilters[i].Subject.Equals("") || msg.Subject.Contains(m_MailMessageFilters[i].Subject)) ) { SavedMails sm = new SavedMails(); sm.From = msg.From.Address; m_SavedMails.Add(sm); m_ImapCommand.FetchBodyStructure(msg); int ind = 0; foreach (ImapMessageBodyPart imapMessageBodyPart in msg.BodyParts) { m_ImapCommand.FetchBodyPart(msg, ind); if (imapMessageBodyPart.Attachment && (m_MailMessageFilters[i].FileName.Equals("") || imapMessageBodyPart.FileName.Contains(m_MailMessageFilters[i].FileName))) { createFile(imapMessageBodyPart, m_MailMessageFilters[i], sm); } ind++; } } } } } catch (Exception ex) { m_Connection = null; throw ex; } finally { if (m_Connection != null && m_Connection.ConnectionState == ConnectionState.Open) { m_Connection.Close(); } } }