Пример #1
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();
                }
            }
        }
Пример #2
0
        /// <summary>
        ///
        /// </summary>
        private void createFile(ImapMessageBodyPart imapMessageBodyPart, MailMessageFilter mailMessageFilter, SavedMails sm)
        {
            string message = "";

            try
            {
                if (imapMessageBodyPart.FileName.Equals(""))
                {
                    return;
                }

                System.Text.Encoding      encodingTR = System.Text.Encoding.GetEncoding("ISO-8859-9");
                System.Text.ASCIIEncoding encoding   = new System.Text.ASCIIEncoding();



                if (imapMessageBodyPart.FileName.Contains("?"))
                {
                    string[] dm = imapMessageBodyPart.FileName.Split('?');
                    message = imapMessageBodyPart.FileName;
                    dm[3]   = dm[3].Replace("=FC", "ü");
                    dm[3]   = dm[3].Replace("=DC", "Ü");
                    dm[3]   = dm[3].Replace("=F0", "ğ");
                    dm[3]   = dm[3].Replace("=D0", "Ğ");
                    dm[3]   = dm[3].Replace("=F6", "ö");
                    dm[3]   = dm[3].Replace("=D6", "Ö");
                    dm[3]   = dm[3].Replace("=E7", "ç");
                    dm[3]   = dm[3].Replace("=C7", "Ç");
                    dm[3]   = dm[3].Replace("=FD", "ı");
                    dm[3]   = dm[3].Replace("=DD", "İ");
                    dm[3]   = dm[3].Replace("=FE", "ş");
                    dm[3]   = dm[3].Replace("=DE", "Ş");
                    dm[3]   = dm[3].Replace("_", " ");
                    dm[3]   = dm[3].Replace("=5F", "_");

                    imapMessageBodyPart.FileName = dm[3];
                }
                string temp = "";
                try
                {
                    temp  = imapMessageBodyPart.FileName.Substring(0, imapMessageBodyPart.FileName.LastIndexOf('.'));
                    temp  = temp + "_" + DateTime.Today.Year + DateTime.Today.Month.ToString().PadLeft(2, '0') + DateTime.Today.Day.ToString().PadLeft(2, '0') + "_" + DateTime.Now.Hour.ToString().PadLeft(2, '0') + DateTime.Now.Minute.ToString().PadLeft(2, '0') + DateTime.Now.Second.ToString().PadLeft(2, '0');
                    temp += imapMessageBodyPart.FileName.Substring(imapMessageBodyPart.FileName.LastIndexOf('.'), imapMessageBodyPart.FileName.Length - imapMessageBodyPart.FileName.LastIndexOf('.'));
                }
                catch
                {
                    sm.ExceptionType = 1;
                    sm.SavedFiles.Add(new SavedFiles());
                    ((SavedFiles)sm.SavedFiles[sm.SavedFiles.Count - 1]).FileName = "";
                    ((SavedFiles)sm.SavedFiles[sm.SavedFiles.Count - 1]).Size     = 0;
                    return;
                }
                //nevzat:path.combine şeklinde değiştirdim.
                if (!Directory.Exists(mailMessageFilter.DestinationPath))
                {
                    Directory.CreateDirectory(mailMessageFilter.DestinationPath);
                }

                string fullPath = Path.Combine(mailMessageFilter.DestinationPath, temp);//mailMessageFilter.DestinationPath.EndsWith("\\") ? mailMessageFilter.DestinationPath : (mailMessageFilter.DestinationPath + "\\") + temp;
                using (BinaryWriter binWriter = new BinaryWriter(File.Open(fullPath, FileMode.Append), encodingTR))
                {
                    byte[] b;
                    string result = "";
                    try
                    {
                        if (imapMessageBodyPart.Data.Length != 0)
                        {
                            b = System.Convert.FromBase64String(imapMessageBodyPart.Data.Substring(0, imapMessageBodyPart.Data.Length - 1));
                        }
                        else
                        {
                            b = System.Convert.FromBase64String("");
                        }
                        result = encodingTR.GetString(b);
                    }
                    catch
                    {
                        result = imapMessageBodyPart.Data;
                    }

                    binWriter.Write(encodingTR.GetBytes(result));
                    binWriter.Close();
                    //nevzat: maildeki isim değil, kaydedilen dosya adı şeklinde değiştirdim
                    m_CreatedFileList.AppendLine(fullPath /*imapMessageBodyPart.FileName*/);
                    sm.SavedFiles.Add(new SavedFiles());
                    ((SavedFiles)sm.SavedFiles[sm.SavedFiles.Count - 1]).FileName = temp;
                    ((SavedFiles)sm.SavedFiles[sm.SavedFiles.Count - 1]).Size     = result.Length;
                }
            }
            catch (Exception ex)
            {
                sm.ExceptionType = 1;
                sm.SavedFiles.Add(new SavedFiles());
                ((SavedFiles)sm.SavedFiles[sm.SavedFiles.Count - 1]).FileName = "";
                ((SavedFiles)sm.SavedFiles[sm.SavedFiles.Count - 1]).Size     = 0;
                return;
            }
        }