Exemple #1
0
        /// <summary>
        /// Converts the Mail file to HTML
        /// </summary>
        /// <param name="filePath">Path to the mail file</param>
        /// <param name="destinationPath">Directory where the HTML file will be saved</param>
        /// <returns>Name of the converted file</returns>
        public string Convert(string filePath, string destinationPath)
        {
            Process     process    = new Process();
            Application appOutlook = null;
            MailItem    mailItem   = null;

            try
            {
                if (Path.GetExtension(filePath) == "." + SupportedExtensions.eml.ToString())
                {
                    foreach (var oldProcess in Process.GetProcessesByName("OUTLOOK"))
                    {
                        oldProcess.Kill();
                    }

                    process.StartInfo.FileName = filePath;
                    process.Start();

                    bool outlookOpen = false;
                    while (!outlookOpen)
                    {
                        try
                        {
                            appOutlook  = (Application)Marshal.GetActiveObject("Outlook.Application");
                            outlookOpen = true;
                        }
                        catch
                        {
                            Thread.Sleep(100);
                        }
                    }
                    mailItem = (MailItem)appOutlook.ActiveInspector().CurrentItem;
                }
                else
                {
                    appOutlook = new Application();
                    mailItem   = (MailItem)appOutlook.Session.OpenSharedItem(filePath);
                }
                string fileName = Path.GetFileNameWithoutExtension(filePath) + DateTime.Now.Ticks + ".html";

                if (!Directory.Exists(destinationPath))
                {
                    Directory.CreateDirectory(destinationPath);
                }
                mailItem.SaveAs(Path.Combine(destinationPath, fileName), OlSaveAsType.olHTML);

                return(fileName);
            }
            catch
            {
                return(null);
            }
            finally
            {
                mailItem?.Close(OlInspectorClose.olDiscard);
                appOutlook?.Quit();
                process.Dispose();
            }
        }
Exemple #2
0
        /// <summary>
        /// Abre o email e salva todos os anexos na pasta de notas fiscais
        /// </summary>
        public void ExtrairAnexosDeEmail(string nomeArquivo, Application Outlook)
        {
            MailItem email = (MailItem)Outlook.Session.OpenSharedItem(nomeArquivo);

            try
            {
                int i = 0;
                foreach (Attachment anexo in email.Attachments)
                {
                    string nomeXML = PastasXml.Default.PastaNFs + "\\" + anexo.FileName;
                    while (File.Exists(nomeXML))
                    {
                        nomeXML = nomeXML.Insert(nomeXML.Length - 4, i.ToString());
                    }
                    anexo.SaveAsFile(nomeXML);
                    i++;
                }
                email.Close(OlInspectorClose.olDiscard);
            }
            catch
            {
                email.Close(OlInspectorClose.olDiscard);
            }
        }
Exemple #3
0
        public void PerformAction(string speechText)
        {
            speechText = Utility.TruncateActionString(speechText, "Reply");

            var item = Utility.GetNThItem(speechText);

            if (item is MailItem)
            {
                MailItem mailItem  = (MailItem)item;
                MailItem replyItem = mailItem.Reply();

                Globals.ThisAddIn.Application.ActiveExplorer().ClearSelection();
                replyItem.Display(false);

                VoiceSearch.speakItOut(" The Subject of the mail is : " + replyItem.Subject + ", You are Replying to " + replyItem.To).GetAwaiter().GetResult();
                //Task.Delay(1000).Wait();
                VoiceSearch.speakItOut("Tell me what you want to reply.").GetAwaiter().GetResult();
                //Task.Delay(2000).Wait();
                string body = VoiceSearch.RecognizeSpeechAsync().GetAwaiter().GetResult();
                replyItem.Body = body;

                VoiceSearch.speakItOut("Your Reply is ready. Do you want to Send or Discard?").GetAwaiter().GetResult();
                //Task.Delay(2000).Wait();

                string action = VoiceSearch.RecognizeSpeechAsync().GetAwaiter().GetResult();
                if (action.ToLower().Contains("send"))
                {
                    replyItem.Send();
                    VoiceSearch.speakItOut("Mail Sent.").GetAwaiter().GetResult();
                }
                else
                {
                    replyItem.Close(OlInspectorClose.olDiscard);
                    VoiceSearch.speakItOut("Mail Discarded.").GetAwaiter().GetResult();
                }
            }
            else
            {
                Console.WriteLine(item.GetType());
            }
        }
 public void DiscardMail()
 {
     mailItem.Close(OlInspectorClose.olDiscard);
 }
Exemple #5
0
        private void GetAttachmentsFromConversation(MailItem mailItem)
        {
            if (mailItem == null)
            {
                return;
            }

            if (mailItem.Attachments.CountNonEmbeddedAttachments() > 0)
            {
                return;
            }

            System.Collections.Generic.Stack <MailItem> st = new System.Collections.Generic.Stack <MailItem>();

            // Determine the store of the mail item.
            Outlook.Folder folder = mailItem.Parent as Outlook.Folder;
            Outlook.Store  store  = folder.Store;
            if (store.IsConversationEnabled == true)
            {
                // Obtain a Conversation object.
                Outlook.Conversation conv = mailItem.GetConversation();
                // Check for null Conversation.
                if (conv != null)
                {
                    // Obtain Table that contains rows
                    // for each item in the conversation.
                    Outlook.Table table = conv.GetTable();
                    _logger.Debug("Conversation Items Count: " + table.GetRowCount().ToString());
                    _logger.Debug("Conversation Items from Root:");

                    // Obtain root items and enumerate the conversation.
                    Outlook.SimpleItems simpleItems = conv.GetRootItems();
                    foreach (object item in simpleItems)
                    {
                        // In this example, enumerate only MailItem type.
                        // Other types such as PostItem or MeetingItem
                        // can appear in the conversation.
                        if (item is Outlook.MailItem)
                        {
                            Outlook.MailItem mail     = item as Outlook.MailItem;
                            Outlook.Folder   inFolder = mail.Parent as Outlook.Folder;
                            string           msg      = mail.Subject + " in folder [" + inFolder.Name + "] EntryId [" + (mail.EntryID.ToString() ?? "NONE") + "]";
                            _logger.Debug(msg);
                            _logger.Debug(mail.Sender);
                            _logger.Debug(mail.ReceivedByEntryID);

                            if (mail.EntryID != null && (mail.Sender != null || mail.ReceivedByEntryID != null))
                            {
                                st.Push(mail);
                            }
                        }
                        // Call EnumerateConversation
                        // to access child nodes of root items.
                        EnumerateConversation(st, item, conv);
                    }
                }
            }

            while (st.Count > 0)
            {
                MailItem it = st.Pop();

                if (it.Attachments.CountNonEmbeddedAttachments() > 0)
                {
                    //_logger.Debug(it.Attachments.CountNonEmbeddedAttachments());

                    try
                    {
                        if (mailItem.IsMailItemSignedOrEncrypted())
                        {
                            if (MessageBox.Show(null, "Es handelt sich um eine signierte Nachricht. Soll diese für die Anhänge ohne Zertifikat dupliziert werden?", "Nachricht duplizieren?", MessageBoxButtons.YesNo) == DialogResult.Yes)
                            {
                                mailItem.Close(OlInspectorClose.olDiscard);
                                mailItem = mailItem.Copy();
                                mailItem.Unsign();
                                mailItem.Save();
                                mailItem.Close(OlInspectorClose.olDiscard);
                                mailItem.Save();
                            }
                            else
                            {
                                st.Clear();
                                break;
                            }
                        }

                        mailItem.CopyAttachmentsFrom(it);
                        mailItem.Save();
                    }
                    catch (System.Exception ex)
                    {
                        //mailItem.Close(OlInspectorClose.olDiscard);
                        MessageBox.Show(ex.Message);
                    }

                    st.Clear();
                }
            }
            st.Clear();

            Marshal.ReleaseComObject(mailItem);
        }
        private async Task CopyMove(object item, Folder backupFolder, ExportOptions exportOptions, DirectoryInfo root)
        {
            await Task.Yield();

            if (item is MailItem)
            {
                try
                {
                    MailItem mailItem = (MailItem)item;
                    if (
                        (exportOptions.mailItemFlag == ExportFlag.All) ||
                        (exportOptions.mailItemFlag != ExportFlag.Exclude) ||
                        (exportOptions.mailItemFlag == ExportFlag.Filter && Between(mailItem.CreationTime, exportOptions.exportStart, exportOptions.exportEnd))
                        )
                    {
                        ////copy
                        //MailItem copiedMailItem = mailItem.Copy();
                        //copiedMailItem.Move(backupFolder);
                        //
                        ////audit
                        //Console.WriteLine(backupFolder.FolderPath + "\t MailItem \t" + mailItem.Subject + "\t" + mailItem.ReceivedTime);
                        //
                        //
                        ////close items
                        //mailItem.Close(OlInspectorClose.olDiscard);
                        //copiedMailItem.Close(OlInspectorClose.olDiscard);

                        mailItem.SaveAs(root.FullName + "\\" + mailItem.EntryID + ".msg", OlSaveAsType.olMSGUnicode);
                        mailItem.Close(OlInspectorClose.olDiscard);

                        Marshal.ReleaseComObject(mailItem);  //cannot set to null before this call
                        Marshal.FinalReleaseComObject(mailItem);
                    }
                }
                catch (System.Exception e)
                {
                    Console.WriteLine(e);
                }
            }
            //else if (item is AppointmentItem)
            //{
            //    try
            //    {
            //        AppointmentItem appointmentItem = (AppointmentItem)item;
            //
            //        if ((exportOptions.appointmentItemFlag != ExportFlag.Exclude && Between(appointmentItem.CreationTime, exportOptions.exportStart, exportOptions.exportEnd)) || exportOptions.appointmentItemFlag == ExportFlag.All)
            //        {
            //            //copy
            //            appointmentItem.CopyTo((MAPIFolder)backupFolder, OlAppointmentCopyOptions.olCreateAppointment);
            //
            //            //audit
            //            Console.WriteLine(backupFolder.FolderPath + "\t AppointmentItem \t" + appointmentItem.Subject + "\t" + appointmentItem.StartUTC);
            //
            //            //closeitems
            //            appointmentItem.Close(OlInspectorClose.olDiscard);
            //        }
            //    }
            //    catch (System.Exception e)
            //    {
            //        Console.WriteLine(e);
            //    }
            //}
            //else if (item is ContactItem)
            //{
            //    try
            //    {
            //        ContactItem contactItem = (ContactItem)item;
            //
            //        if ((exportOptions.contactItemFlag != ExportFlag.Exclude && Between(contactItem.CreationTime, exportOptions.exportStart, exportOptions.exportEnd)) || exportOptions.contactItemFlag == ExportFlag.All)
            //        {
            //            //copy
            //            ContactItem copiedContactItem = contactItem.Copy();
            //            copiedContactItem.Move(backupFolder);
            //
            //            //audit
            //            Console.WriteLine("\t ContactItem \t" + copiedContactItem.LastName + ", " + copiedContactItem.FirstName);
            //
            //            //closeitems
            //            contactItem.Close(OlInspectorClose.olDiscard);
            //            copiedContactItem.Close(OlInspectorClose.olDiscard);
            //        }
            //    }
            //    catch (System.Exception e)
            //    {
            //        Console.WriteLine(e);
            //    }
            //}
            //else if (item is MeetingItem)
            //{
            //    try
            //    {
            //        MeetingItem meetingItem = (MeetingItem)item;
            //
            //        if ((exportOptions.meetingItemFlag != ExportFlag.Exclude && Between(meetingItem.CreationTime, exportOptions.exportStart, exportOptions.exportEnd)) || exportOptions.meetingItemFlag == ExportFlag.All)
            //        {
            //            //copy
            //            MeetingItem copiedMeetingItem = meetingItem.Copy();
            //            copiedMeetingItem.Move(backupFolder);
            //
            //            //audit
            //            Console.WriteLine(backupFolder.FolderPath + "\t MeetingItem \t" + copiedMeetingItem.Subject + "\t" + copiedMeetingItem.ReceivedTime);
            //
            //            //closeitems
            //            meetingItem.Close(OlInspectorClose.olDiscard);
            //            copiedMeetingItem.Close(OlInspectorClose.olDiscard);
            //        }
            //    }
            //    catch (System.Exception e)
            //    {
            //        Console.WriteLine(e);
            //    }
            //}
            //else if (item is TaskItem)
            //{
            //    try
            //    {
            //        TaskItem taskItem = (TaskItem)item;
            //
            //        if ((exportOptions.taskItemFlag != ExportFlag.Exclude && Between(taskItem.CreationTime, exportOptions.exportStart, exportOptions.exportEnd)) || exportOptions.taskItemFlag == ExportFlag.All)
            //        {
            //
            //            //copy
            //            TaskItem copiedtaskItem = taskItem.Copy();
            //            copiedtaskItem.Move(backupFolder);
            //
            //            //audit
            //            Console.WriteLine(backupFolder.FolderPath + "\t TaskItem \t" + copiedtaskItem.Subject + "\t" + copiedtaskItem.CreationTime);
            //
            //            //closeitems
            //            taskItem.Close(OlInspectorClose.olDiscard);
            //            copiedtaskItem.Close(OlInspectorClose.olDiscard);
            //        }
            //    }
            //    catch (System.Exception e)
            //    {
            //        Console.WriteLine(e);
            //    }
            //}
            //else if (item is JournalItem)
            //{
            //    try
            //    {
            //        JournalItem journalItem = (JournalItem)item;
            //
            //        if ((exportOptions.journalItemFlag != ExportFlag.Exclude && Between(journalItem.CreationTime, exportOptions.exportStart, exportOptions.exportEnd)) || exportOptions.journalItemFlag == ExportFlag.All)
            //        {
            //            //copy
            //            JournalItem copiedjournalItem = journalItem.Copy();
            //            copiedjournalItem.Move(backupFolder);
            //
            //            //audit
            //            Console.WriteLine(backupFolder.FolderPath + "\t JournalItem \t" + copiedjournalItem.Subject + "\t" + copiedjournalItem.CreationTime);
            //
            //            //closeitems
            //            journalItem.Close(OlInspectorClose.olDiscard);
            //            copiedjournalItem.Close(OlInspectorClose.olDiscard);
            //        }
            //    }
            //    catch (System.Exception e)
            //    {
            //        Console.WriteLine(e);
            //    }
            //}
            //else
            //{
            //    try
            //    {
            //        if (exportOptions.otherItemFlag == ExportFlag.All)
            //        {
            //            Console.WriteLine("Couldn't Identify mail item at " + backupFolder.FolderPath);
            //        }
            //    }
            //    catch (System.Exception e)
            //    {
            //        Console.WriteLine(e);
            //    }
            //}
        }
Exemple #7
0
 private void BtnFechar_Click(object sender, EventArgs e)
 {
     this.Close(); EmailRecebido.Close(OlInspectorClose.olDiscard);
 }