/// <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 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 ""([^""]+""\)\)");
            }
        }
Exemple #4
0
        /// <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();
                }
            }
        }