Пример #1
0
        /// <summary>
        /// GetAttachments method - void used for getting the attachments from exchange server
        /// </summary>
        /// <param name="form">Form1 form object passing as a reference</param>
        public void GetAttachments()
        {
            try
            {
                if (!Directory.Exists(Path))
                {
                    Directory.CreateDirectory(Path);
                }
                SetExchangeType();
                serv.Credentials = new WebCredentials(Username, Password);
                serv.AutodiscoverUrl("");

                mailboxCurrentSize = GetMailBoxSize();

                if (mailboxCurrentSize > 450)
                {
                    SendEmail("Obavjest.", "Mailbox je gotovo pun, molimo provjerite i izbrišite stare mail-ove kako bi oslobodili mjesta...!");
                }

                ItemView view = new ItemView(EmailFetch);
                FindItemsResults <Item> result = serv.FindItems(WellKnownFolderName.Inbox, new ItemView(EmailFetch));

                if (result != null && result.Items != null && result.Items.Count > 0)
                {
                    foreach (Item item in result.Items)
                    {
                        EmailMessage msg = EmailMessage.Bind(serv, item.Id, new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments, ItemSchema.HasAttachments, ItemSchema.DateTimeReceived, EmailMessageSchema.From, EmailMessageSchema.Sender));
                        if (msg.Sender.ToString().ToLower().Contains(FilterSender) && msg.From.ToString().ToLower().Contains(FilterSender))
                        {
                            foreach (Attachment att in msg.Attachments)
                            {
                                if (att is FileAttachment)
                                {
                                    FileAttachment file = att as FileAttachment;
                                    if (file.Name.Contains(FilterExtension.ToUpper()) && file.Name.Contains("LOG_FILE"))
                                    {
                                        file.Load(Path + "\\" + file.Name.Replace(FilterExtension.ToUpper(), "_" + msg.DateTimeReceived.ToString("yyyyMMddHHmmss") + FilterExtension.ToUpper()));
                                        if (File.Exists(Path + "\\" + file.Name.Replace(FilterExtension.ToUpper(), "_" + msg.DateTimeReceived.ToString("yyyyMMddHHmmss") + FilterExtension.ToUpper())))
                                        {
                                            logOutput = "[Log - Execution Time] - " + DateTime.Now.ToString() + "\nSender: \"" + FilterSender + "\" || Subject: \"" + item.Subject.ToString() + "\" || Attachment: \"" + file.Name.ToString() + "\" || Path: \"" + Path + "\\" + "\" || Status: \"Success.\"";
                                            LogRotate(logOutput);
                                            item.Delete(DeleteMode.MoveToDeletedItems);
                                        }
                                        else
                                        {
                                            file.Load(Path + "\\" + file.Name.Replace(FilterExtension.ToUpper(), "_" + msg.DateTimeReceived.ToString("yyyyMMddHHmmss") + FilterExtension.ToUpper()));
                                            logOutput = "[Log - Execution Time] - " + DateTime.Now.ToString() + "\nSender: \"" + FilterSender + "\" || Subject: \"" + item.Subject.ToString() + "\" || Attachment: \"" + file.Name.ToString() + "\" || Path: \"" + Path + "\\" + "\" || Status: \"Success.\"";
                                            LogRotate(logOutput);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                logOutput = "Execution Time: \"" + DateTime.Now.ToString() + "\" || Sender: \"" + FilterSender + "\" || Subject: \"" + Subject + "\" || Attachment: \"" + FilterSender + "\" || Content:" + AttachementName + " || Path: \"" + Path + "\\" + "\" || Status: \"Failed.\" " + " || Error message: \"" + e.Message + "\"\r\n";
                LogRotate(logOutput);
                SendEmail("Extracting attachment failed...!", logOutput);
            }
        }
        /// <summary>
        /// GetAttachments method - void used for getting the attachments from exchange server
        /// </summary>
        /// <param name="form">Form1 form object passing as a reference</param>
        public void GetAttachments(Form1 form)
        {
            try
            {
                if (!Directory.Exists(Path))
                {
                    Directory.CreateDirectory(Path);
                }
                SetExchangeType();
                serv.Credentials = new WebCredentials(Username, Password);
                serv.AutodiscoverUrl("");

                mailboxCurrentSize = GetMailBoxSize();

                if (mailboxCurrentSize > MailBoxSizeLimit)
                {
                    SendEmail("Obavjest", "Mailbox je gotovo pun, molimo provjerite i izbrišite stare mail-ove kako bi oslobodili mjesta...!");
                }

                // Automatizacija - brisanje item-a sa servera.

                /*
                 *  if (mailboxCurrentSize > 500)
                 *  {
                 *      ItemView search = new ItemView(10000);
                 *      search.PropertySet = new PropertySet(BasePropertySet.IdOnly, ItemSchema.DateTimeCreated);
                 *      search.OrderBy.Add(ItemSchema.DateTimeCreated, SortDirection.Ascending);
                 *      search.Traversal = ItemTraversal.Shallow;
                 *      FindItemsResults<Item> results = serv.FindItems(WellKnownFolderName.MsgFolderRoot, search);
                 *
                 *      foreach (Item item in results.Items)
                 *      {
                 *
                 *          if (mailboxCurrentSize > 50)
                 *          {
                 *              mailboxCurrentSize = GetMailBoxSize();
                 *              item.Delete(DeleteMode.HardDelete);
                 *          }
                 *      }
                 *  }
                 */
                ItemView view = new ItemView(EmailFetch);
                FindItemsResults <Item> result = serv.FindItems(WellKnownFolderName.Inbox, new ItemView(EmailFetch));

                if (result != null && result.Items != null && result.Items.Count > 0)
                {
                    foreach (Item item in result.Items)
                    {
                        EmailMessage msg = EmailMessage.Bind(serv, item.Id, new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments, ItemSchema.HasAttachments, ItemSchema.DateTimeReceived, EmailMessageSchema.From, EmailMessageSchema.Sender));

                        if (msg.Sender.ToString().ToLower().Contains(FilterSender) && msg.From.ToString().ToLower().Contains(FilterSender))
                        {
                            foreach (Attachment att in msg.Attachments)
                            {
                                if (att is FileAttachment)
                                {
                                    FileAttachment file = att as FileAttachment;
                                    if (file.Name.Contains(FilterExtension.ToUpper()) && file.Name.Contains("LOG_FILE"))
                                    {
                                        //received = Convert.ToDateTime(msg.DateTimeReceived.ToString();
                                        file.Load(Path + "\\" + file.Name.Replace(FilterExtension.ToUpper(), "_" + msg.DateTimeReceived.ToString("yyyyMMddHHmmss") + FilterExtension.ToUpper()));
                                        if (File.Exists(Path + "\\" + file.Name.Replace(FilterExtension.ToUpper(), "_" + msg.DateTimeReceived.ToString("yyyyMMddHHmmss") + FilterExtension.ToUpper())))
                                        {
                                            form.attachment = file.Name.ToString();
                                            form.subject    = item.Subject.ToString();
                                            form.date       = DateTime.Now.ToString();
                                            form.sender     = FilterSender.ToString();
                                            logOutput       = "[Log - Execution Time] - " + DateTime.Now.ToString() + "\nSender: \"" + FilterSender + "\" || Subject: \"" + item.Subject.ToString() + "\" || Attachment: \"" + file.Name.ToString() + "\" || Path: \"" + Path + "\\" + "\" || Status: \"Success.\" Received: " + msg.DateTimeReceived.ToString() + "";
                                            LogRotate(logOutput);
                                            form.backgroundWorker1.ReportProgress(0);
                                            //item.Delete(DeleteMode.HardDelete);
                                            item.Delete(DeleteMode.MoveToDeletedItems);
                                        }
                                        else
                                        {
                                            file.Load(Path + "\\" + file.Name.Replace(FilterExtension.ToUpper(), "_" + msg.DateTimeReceived.ToString("yyyyMMddHHmmss") + FilterExtension.ToUpper()));
                                            form.attachment = file.Name.ToString();
                                            form.subject    = item.Subject.ToString();
                                            form.date       = DateTime.Now.ToString();
                                            form.sender     = FilterSender.ToString();
                                            logOutput       = "[Log - Execution Time] - " + DateTime.Now.ToString() + "\nSender: \"" + FilterSender + "\" || Subject: \"" + item.Subject.ToString() + "\" || Attachment: \"" + file.Name.ToString() + "\" || Path: \"" + Path + "\\" + "\" || Status: \"Success.\" Received: " + msg.DateTimeReceived.ToString() + "";
                                            LogRotate(logOutput);
                                            form.backgroundWorker1.ReportProgress(0);
                                            //item.Delete(DeleteMode.HardDelete);
                                        }
                                    }
                                }
                            }
                        }
                        //
                    }
                }
            }
            catch (Exception e)
            {
                logOutput = "Execution Time: \"" + DateTime.Now.ToString() + "\" || Sender: \"" + FilterSender + "\" || Subject: \"" + Subject + "\" || Attachment: \"" + FilterSender + "\" || Path: \"" + Path + "\\" + "\" || Status: \"Failed.\" " + " || Error message: \"" + e.Message + "\"\r\n";
                LogRotate(logOutput);
                SendEmail("Extracting attachment failed...!", logOutput);
            }
        }