예제 #1
0
        public void FindLastInConversation(Outlook.MailItem mailItem)
        {
            if (mailItem is Outlook.MailItem)
            {
                // Determine the store of the mail item.
                Outlook.Folder folder = mailItem.Parent as Outlook.Folder;
                Outlook.Store  store  = folder.Store;
                if (store.IsConversationEnabled)
                {
                    // Obtain a Conversation object.
                    Outlook.Conversation conv = mailItem.GetConversation();

                    // Check for null Conversation.
                    if (conv != null)
                    {
                        // Obtain Table that contains rows
                        Outlook.Table table = conv.GetTable();
                        int           count = table.GetRowCount();
                        Logger.Log("Conversation Items Count: " + count.ToString());

                        table.MoveToStart();

                        if (!table.EndOfTable)
                        {
                            // lastRow conatins the last item from the conversation
                            Outlook.Row lastRow = table.GetNextRow();

                            //Logger.Log(lastRow["Subject"] + " Modified: " + lastRow["LastModificationTime"]);
                        }
                    }
                }
            }
        }
예제 #2
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);
        }
예제 #3
0
        /// <summary>
        /// Function searches for email duplicates from a specific email
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        private IEnumerable <Outlook.MailItem> search(Outlook.MailItem filter)
        {
            if (filter.Parent == deletedMailsFolder)
            {
                yield break;
            }

            Dictionary <string, Outlook.MailItem> mailsFound = new Dictionary <string, Outlook.MailItem>()
            {
                { filter.EntryID, filter }
            };

            // Obtain a Conversation object.
            Outlook.Conversation conv = filter.GetConversation();

            // Obtain Table that contains rows
            // for each item in Conversation.
            Outlook.Table table = conv.GetTable();

            //break if there just one
            if (table.GetRowCount() == 1)
            {
                yield break;
            }

            Debug.WriteLine("Conversation Items Count: " + table.GetRowCount().ToString());

            // Obtain root items and enumerate Conversation.
            Outlook.SimpleItems simpleItems = conv.GetRootItems();
            foreach (object item in simpleItems)
            {
                // enumerate only MailItem type.
                if (item is Outlook.MailItem)
                {
                    Outlook.MailItem m        = item as Outlook.MailItem;
                    Outlook.Folder   inFolder = m.Parent as Outlook.Folder;
                    string           msg      = m.Subject + " in folder " + inFolder.Name;

                    Debug.WriteLine(msg);

                    if (!mailsFound.ContainsKey(m.EntryID) && MailItemEquals(m, filter) && inFolder != deletedMailsFolder)
                    {
                        mailsFound.Add(m.EntryID, m);
                    }
                }
                // Call EnumerateConversation
                // to access child nodes of root items.
                EnumerateConversation(item, conv, filter, ref mailsFound);
            }

            if (mailsFound.Count > 1)
            {
                //delete duplicates //O(((n-1)n)/2)
                var mf = mailsFound.ToArray();
                for (int i = 0; i < mf.Length - 1; i++)
                {
                    for (int j = i + 1; j < mf.Length; j++)
                    {
                        if (app.Session.CompareEntryIDs(mf[i].Key, mf[j].Key))
                        {
                            mailsFound.Remove(mf[j].Key);
                        }
                    }
                }

                List <Outlook.MailItem> mGelesen      = new List <Outlook.MailItem>(); //List for readed emails
                List <Outlook.MailItem> mNichtGelesen = new List <Outlook.MailItem>(); //List for unreaded
                foreach (Outlook.MailItem m in mailsFound.Values)
                {
                    if (m.UnRead)
                    {
                        mNichtGelesen.Add(m);
                    }
                    else
                    {
                        mGelesen.Add(m);
                    }
                }
                if (mGelesen.Count > 0) // if there are readed emails, move the new unreaded to trash
                {
                    foreach (Outlook.MailItem m in mNichtGelesen)
                    {
                        m.Move(deletedMailsFolder); //move to deleted folder
                        yield return(m);
                    }
                }
                else
                {                                                 //no email were already readed
                    bool first = false;
                    foreach (Outlook.MailItem m in mNichtGelesen) //delete just n-1
                    {
                        if (first)
                        {
                            m.Move(deletedMailsFolder); //move to deleted folder
                        }
                        yield return(m);

                        first = true;
                    }
                }
                //clear readed mails too
                bool first2 = false;
                foreach (Outlook.MailItem m in mGelesen) //delete just n-1
                {
                    if (first2)
                    {
                        m.Move(deletedMailsFolder); //move to deleted folder
                    }
                    yield return(m);

                    first2 = true;
                }
            }
        }
예제 #4
0
        static List <Mail1> checkMailsInFolderForAnswer(string folder)
        {
            Outlook.Folder defFold  = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) as Outlook.Folder;
            Outlook.Folder tempFold = null;
            foreach (Outlook.Folder subfolder in defFold.Folders)
            {
                if (subfolder.Name == folder)
                {
                    tempFold = subfolder;
                    break;
                }
            }

            string filter = DateTime.Now.AddHours(-DateTime.Now.Hour).AddMinutes(-DateTime.Now.Minute).AddHours(-3).ToString("dd/MM/yyyy HH:mm").Replace(".", "/");

            Outlook.Items items = tempFold.Items.Restrict($"[CreationTime]>'{filter}'");


            List <Mail1> mails1TempList = new List <Mail1>();

            foreach (Outlook.MailItem mail in items)
            {
                Mail1 mailTemp = new Mail1();
                mailTemp.sender         = mail.SenderName;
                mailTemp.Topic          = mail.ConversationTopic;
                mailTemp.senderEmailAdr = getSenderEmailAddress(mail);
                mailTemp.sendDate       = mail.CreationTime;

                Outlook.Conversation tc     = mail.GetConversation();
                Outlook.Table        table1 = tc.GetTable();
                table1.Columns.Add("http://schemas.microsoft.com/mapi/proptag/0x5D0A001F"); //email от кого письмо
                table1.Columns.Add("http://schemas.microsoft.com/mapi/proptag/0x3FF8001F"); //От кого письмо
                //Console.WriteLine(table1.GetRowCount());
                if (table1.GetRowCount() > 1)
                {
                    List <Mail1> listOfMembers = new List <Mail1>();
                    while (!table1.EndOfTable)
                    {
                        Mail1       t   = new Mail1();
                        Outlook.Row row = table1.GetNextRow();
                        t.sendDate       = row["CreationTime"];
                        t.sender         = row["http://schemas.microsoft.com/mapi/proptag/0x3FF8001F"];
                        t.senderEmailAdr = row["http://schemas.microsoft.com/mapi/proptag/0x5D0A001F"];
                        t.sender         = row["http://schemas.microsoft.com/mapi/proptag/0x3FF8001F"];

                        if (needToBesnaweredByThisList.Contains(t.senderEmailAdr))
                        {
                            if (t.sender != mailTemp.sender)
                            {
                                mailTemp.answeredByList.Add(t);
                            }
                        }
                    }

                    mails1TempList.Add(mailTemp);
                }
                else
                {
                    mails1TempList.Add(mailTemp);
                }
            }

            return(mails1TempList);

            #region commented
            // For this example, you will work only with
            //MailItem. Other item types such as
            //MeetingItem and PostItem can participate
            //in Conversation.
            //if (selectedItem is Outlook.MailItem)
            //{
            //    // Cast selectedItem to MailItem.
            //    Outlook.MailItem mailItem =
            //        selectedItem as Outlook.MailItem; ;
            //    // Determine store of mailItem.
            //    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 Conversation.
            //            Outlook.Table table = conv.GetTable();
            //            Debug.WriteLine("Conversation Items Count: " +
            //                table.GetRowCount().ToString());
            //            Debug.WriteLine("Conversation Items from Table:");
            //            while (!table.EndOfTable)
            //            {
            //                Outlook.Row nextRow = table.GetNextRow();
            //                Console.WriteLine(nextRow["Subject"]
            //                    + " Modified: "
            //                    + nextRow["LastModificationTime"]);
            //            }
            //            Debug.WriteLine("Conversation Items from Root:");
            //            // Obtain root items and enumerate 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 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;
            //                    Debug.WriteLine(msg);
            //                }
            //                // Call EnumerateConversation
            //                // to access child nodes of root items.
            //                EnumerateConversation(item, conv);
            //            }
            //        }
            //    }
            //}
            #endregion
        }