Facebook
From ola, 2 Months ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 183
  1.    public void DownloadFiles(string donePath, string workPath)
  2.    {
  3.        using (var client = new ImapClient(new ProtocolLogger("imap.log")))
  4.        {
  5.            try
  6.            {
  7.                string password = Properties.Settings.Default.PocztaPassword.Value;
  8.                string username = Properties.Settings.Default.PocztaEmail.Value;
  9.                int days = Convert.ToInt32(Properties.Settings.Default.LastDays.Value) * (-1);
  10.  
  11.                //int days = 200;
  12.  
  13.  
  14.                PreparePocztaRoutingDirectories(donePath, workPath);
  15.  
  16.                client.Connect("bappress.home.pl", 993, SecureSocketOptions.SslOnConnect);
  17.  
  18.                client.Authenticate(username, password);
  19.  
  20.                client.Inbox.Open(FolderAccess.ReadWrite);
  21.  
  22.                IList<UniqueId> uids = client.Inbox.Search(SearchQuery.MessageContains("@poczta-polska.pl").And(SearchQuery.SentSince(DateTime.Now.AddDays(days))));
  23.                SendMessage(MessageType.Info, "Pobieranie danych z Poczta", false);
  24.                foreach (UniqueId uid in uids)
  25.                {
  26.                    MimeMessage message = client.Inbox.GetMessage(uid);
  27.  
  28.                    foreach (MimeEntity attachment in message.Attachments)
  29.                    {
  30.                        var fileName = attachment.ContentDisposition?.FileName ?? attachment.ContentType.Name;
  31.  
  32.                        if (!string.IsNullOrEmpty(fileName))
  33.                        {
  34.                            if (fileName.Contains("WsrWgPna"))
  35.                            {
  36.                                using (Stream stream = File.Create($@"{workPath}\{fileName}"))
  37.                                {
  38.                                    stream.Flush();
  39.                                    stream.Seek(0, SeekOrigin.Begin);
  40.  
  41.                                    if (attachment is MessagePart rfc822)
  42.                                    {
  43.                                        rfc822.Message.WriteTo(stream);
  44.  
  45.                                    }
  46.                                    else
  47.                                    {
  48.                                        var part = (MimePart)attachment;
  49.  
  50.                                        part.Content.DecodeTo(stream);
  51.  
  52.  
  53.                                    }
  54.  
  55.                                }
  56.  
  57.                                Filename = fileName;
  58.                            }
  59.                            if (fileName.Contains("OdbiorWPunkcie"))
  60.                            {
  61.                                using (Stream stream = File.Create($@"{workPath}\{fileName}"))
  62.                                {
  63.                                    stream.Flush();
  64.                                    stream.Seek(0, SeekOrigin.Begin);
  65.  
  66.                                    if (attachment is MessagePart)
  67.                                    {
  68.                                        var rfc822 = (MessagePart)attachment;
  69.  
  70.                                        rfc822.Message.WriteTo(stream);
  71.                                    }
  72.                                    else
  73.                                    {
  74.                                        var part = (MimePart)attachment;
  75.  
  76.                                        part.Content.DecodeTo(stream);
  77.  
  78.                                    }
  79.                                }
  80.                                Filename2 = fileName;
  81.  
  82.  
  83.                            }
  84.  
  85.                        }
  86.                    }
  87.  
  88.                }
  89.              
  90.            
  91.            }
  92.            catch (Exception ex)
  93.            {
  94.                SendMessage(MessageType.Error, ex.Message, false);
  95.            }
  96.            finally
  97.            {
  98.                client.Disconnect(true);
  99.            }
  100.        }  
  101.    }
  102.    
  103.    
  104.    
  105.         public void XLStoCSVConverter(string filename, string workPath)
  106.         {
  107.             try
  108.             {
  109.                 Spire.Xls.Workbook workbook = new Spire.Xls.Workbook();
  110.                 workbook.LoadFromFile&#40;$@"{workPath}\{filename}"&#41;;
  111.                 Spire.Xls.Worksheet sheet = workbook.Worksheets[0];
  112.                 sheet.SaveToFile&#40;$@"{workPath}\{filename.Replace(".xls", ".csv"&#41;}", ",", Encoding.UTF8);
  113.             }
  114.             catch (Exception ex)
  115.             {
  116.                 SendMessage(MessageType.Error, ex.Message, false);
  117.             }
  118.            
  119.  
  120.         }