- public void DownloadFiles(string donePath, string workPath)
- {
- using (var client = new ImapClient(new ProtocolLogger("imap.log")))
- {
- try
- {
- string password = Properties.Settings.Default.PocztaPassword.Value;
- string username = Properties.Settings.Default.PocztaEmail.Value;
- int days = Convert.ToInt32(Properties.Settings.Default.LastDays.Value) * (-1);
- //int days = 200;
- PreparePocztaRoutingDirectories(donePath, workPath);
- client.Connect("bappress.home.pl", 993, SecureSocketOptions.SslOnConnect);
- client.Authenticate(username, password);
- client.Inbox.Open(FolderAccess.ReadWrite);
- IList<UniqueId> uids = client.Inbox.Search(SearchQuery.MessageContains("@poczta-polska.pl").And(SearchQuery.SentSince(DateTime.Now.AddDays(days))));
- SendMessage(MessageType.Info, "Pobieranie danych z Poczta", false);
- foreach (UniqueId uid in uids)
- {
- MimeMessage message = client.Inbox.GetMessage(uid);
- foreach (MimeEntity attachment in message.Attachments)
- {
- var fileName = attachment.ContentDisposition?.FileName ?? attachment.ContentType.Name;
- if (!string.IsNullOrEmpty(fileName))
- {
- if (fileName.Contains("WsrWgPna"))
- {
- using (Stream stream = File.Create($@"{workPath}\{fileName}"))
- {
- stream.Flush();
- stream.Seek(0, SeekOrigin.Begin);
- if (attachment is MessagePart rfc822)
- {
- rfc822.Message.WriteTo(stream);
- }
- else
- {
- var part = (MimePart)attachment;
- part.Content.DecodeTo(stream);
- }
- }
- Filename = fileName;
- }
- if (fileName.Contains("OdbiorWPunkcie"))
- {
- using (Stream stream = File.Create($@"{workPath}\{fileName}"))
- {
- stream.Flush();
- stream.Seek(0, SeekOrigin.Begin);
- if (attachment is MessagePart)
- {
- var rfc822 = (MessagePart)attachment;
- rfc822.Message.WriteTo(stream);
- }
- else
- {
- var part = (MimePart)attachment;
- part.Content.DecodeTo(stream);
- }
- }
- Filename2 = fileName;
- }
- }
- }
- }
- }
- catch (Exception ex)
- {
- SendMessage(MessageType.Error, ex.Message, false);
- }
- finally
- {
- client.Disconnect(true);
- }
- }
- }
- public void XLStoCSVConverter(string filename, string workPath)
- {
- try
- {
- Spire.Xls.Workbook workbook = new Spire.Xls.Workbook();
- workbook.LoadFromFile($@"{workPath}\{filename}");
- Spire.Xls.Worksheet sheet = workbook.Worksheets[0];
- sheet.SaveToFile($@"{workPath}\{filename.Replace(".xls", ".csv")}", ",", Encoding.UTF8);
- }
- catch (Exception ex)
- {
- SendMessage(MessageType.Error, ex.Message, false);
- }
- }