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"]); } } } } }
/// <summary>Displays the data from the specified Table object.</summary> /// <param name="table">The Table containing the contact data to display. /// </param> private void ShowContactReport(Outlook.Table table) { this.SuspendLayout(); // Free memory for any old controls. foreach (Control c in this.reportPanel.Controls) { if (c != null) { c.Dispose(); } } this.reportPanel.Controls.Clear(); if (table != null) { // Add the new control with updated contact information. this.reportPanel.Controls.Add(NewDataGrid(table)); } else { this.reportPanel.Controls.Add(ErrorMessage()); } this.ResumeLayout(); this.Refresh(); }
/// <summary> /// Read all possible contact and return it /// </summary> /// <param name="cacheTimeStamp">For future using</param> /// <returns>Distionry where key is EntryID and Value is Last Modification Time</returns> public Dictionary <string, DateTime> GetTableFilter(DateTime cacheTimeStamp) { //string criteria = string.Format("[LastModificationTime] > '{0}'", cacheTimeStamp.ToString("yyyyMMdd HH:mm:ss")); Outlook.Table table = _ContactFolder.GetTable(Type.Missing, Type.Missing); Dictionary <string, DateTime> ret = new Dictionary <string, DateTime>(); while (!table.EndOfTable) { Outlook.Row nextRow = table.GetNextRow(); ret.Add(nextRow["EntryID"].ToString(), (DateTime)nextRow["LastModificationTime"]); } return(ret); }
/// <summary>Displays the contact data with the default columns and /// does not filter the rows.</summary> private void ShowDefaultReport() { Outlook.Folder opportunities = Globals.ThisAddIn.OpportunitiesFolder; if (opportunities != null) { Outlook.Table contacts = opportunities.GetTable(); ShowContactReport(contacts); } else { ShowContactReport(null); } }
/// <summary>Handles the Click event for the form's Customize Columns button. /// </summary> /// <remarks>Displays the contact data with the addition of the CustomerID /// built-in property and the five Sales Opportunity custom properties. Does /// not filter the rows.</remarks> private void customizeColumnsButton_Click(object sender, EventArgs e) { Outlook.Folder opportunities = Globals.ThisAddIn.OpportunitiesFolder; if (opportunities != null) { Outlook.Table contacts = opportunities.GetTable(); AddCustomColumns(contacts); ShowContactReport(contacts); } else { ShowContactReport(null); } }
/// <summary>Handles the Click event for the form's Filter button.</summary> /// <remarks>Displays the contact data with the default columns and /// filters the rows based on the CompanyName property.</remarks> private void filterButton_Click(object sender, EventArgs e) { Outlook.Folder opportunities = Globals.ThisAddIn.OpportunitiesFolder; if (opportunities != null) { string criteria = "[CompanyName] = 'Adventure Works'"; Outlook.Table contacts = opportunities.GetTable(criteria); ShowContactReport(contacts); } else { ShowContactReport(null); } }
/// <summary>Creates a DataGridView control that displays the contact /// information contained in the specified Table object.</summary> /// <param name="table">The Table containing the contact data to display. /// </param> /// <returns>The new DataGridView.</returns> private DataGridView NewDataGrid(Outlook.Table table) { DataGridView dataGrid = new DataGridView(); // For each column in the table, add a column to the control. Note that the // Table column collection uses 1-based indexing; whereas, the DataGridView // column collection uses 0-based indexing. dataGrid.ColumnCount = table.Columns.Count; for (int i = 1; i <= table.Columns.Count; i++) { Outlook.Column tableColumn = table.Columns[i]; DataGridViewColumn dataColumn = dataGrid.Columns[i - 1]; dataColumn.Name = tableColumn.Name; dataColumn.HeaderText = Constants.GetDisplayName(tableColumn.Name); dataColumn.ValueType = Constants.GetDataType(tableColumn.Name); dataColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; // Format the Purchase Estimate property data as currency. if (dataColumn.HeaderText == Constants.purchaseEstimateDisplayName) { dataColumn.DefaultCellStyle.Format = "C"; } } // For each row in the table, add the contact data to the control. table.MoveToStart(); while (!table.EndOfTable) { Outlook.Row contact = table.GetNextRow(); object[] contactData = contact.GetValues(); // The ordering of the contact property values returned by the // Table's GetValues method matches the ordering of the column // information returned by the Table's Columns property. dataGrid.Rows.Add(contactData); } // Modify the control's display and behavior properties. dataGrid.AutoSize = true; dataGrid.Dock = DockStyle.Fill; dataGrid.BorderStyle = BorderStyle.FixedSingle; dataGrid.ReadOnly = true; return(dataGrid); }
/// <summary>Handles the Click event for the form's Customize Columns button. /// </summary> /// <remarks>Displays the contact data with the addition of the CustomerID /// built-in property and the five Sales Opportunity custom properties. Filters /// the rows based on the custom Sales Rep property.</remarks> private void filterCustomColumnsButton_Click(object sender, EventArgs e) { Outlook.Folder opportunities = Globals.ThisAddIn.OpportunitiesFolder; if (opportunities != null) { string criteria = string.Format( "[{0}] = 'Karen Berg'", Constants.salesRepDisplayName); Outlook.Table contacts = opportunities.GetTable(criteria); AddCustomColumns(contacts); ShowContactReport(contacts); } else { ShowContactReport(null); } }
private void DemoTableColumns() { const string PR_HAS_ATTACH = "http://schemas.microsoft.com/mapi/proptag/0x0E1B000B"; // Obtain Inbox Microsoft.Office.Interop.Outlook.Folder folder = MailNS.GetDefaultFolder(OlDefaultFolders.olFolderInbox) as Folder;//Microsoft.Office.Interop.Outlook.Application.Session.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox)as Microsoft.Office.Interop.Outlook.Folder; // Create filter string filter = "@SQL=" + "\"" + PR_HAS_ATTACH + "\"" + " = 1"; // Must use 'like' comparison for Find/FindNext string filter1 = "@SQL=" + "\"" + "urn:schemas:httpmail:subject" + "\"" + " like '%License Keys - Order%'"; Microsoft.Office.Interop.Outlook.Table table = folder.GetTable(filter, Microsoft.Office.Interop.Outlook.OlTableContents.olUserItems); // Remove default columns table.Columns.RemoveAll(); // Add using built-in name table.Columns.Add("EntryID"); table.Columns.Add("Subject"); table.Columns.Add("ReceivedTime"); table.Sort("ReceivedTime", Microsoft.Office.Interop.Outlook.OlSortOrder.olDescending); // Add using namespace // Date received table.Columns.Add( "urn:schemas:httpmail:datereceived"); while (!table.EndOfTable) { Microsoft.Office.Interop.Outlook.Row nextRow = table.GetNextRow(); StringBuilder sb = new StringBuilder(); sb.AppendLine(nextRow["Subject"].ToString()); // Reference column by name sb.AppendLine("Received (Local): " + nextRow["ReceivedTime"]); // Reference column by index sb.AppendLine("Received (UTC): " + nextRow[4]); sb.AppendLine(); System.Diagnostics.Debug.WriteLine(sb.ToString()); } }
/// <summary>For a Table object, removes the CreationTime and LastModificationTime /// properties and adds the CustomerID built-in property and the five Sales /// Opportunity custom properties.</summary> private void AddCustomColumns(Outlook.Table contacts) { for (int i = contacts.Columns.Count; i > 0; i--) { if (contacts.Columns[i].Name == Constants.creationTimeDisplayName || contacts.Columns[i].Name == Constants.lastModificationTimeDisplayName) { contacts.Columns.Remove(i); } } contacts.Columns.Add(Constants.customerIDProperRef); contacts.Columns.Add(Constants.encounterDatePropTag); contacts.Columns.Add(Constants.purchaseEstimatePropTag); contacts.Columns.Add(Constants.salesRepPropTag); contacts.Columns.Add(Constants.salesValuePropTag); contacts.Columns.Add(Constants.tradeShowPropTag); }
private void searchSentMailUsingTable() { Outlook.NameSpace nameSpace = null; Outlook.Folder sentFolder = null; Outlook.Table sentTable = null; Messages messages = new Messages(System.Security.Principal.WindowsIdentity.GetCurrent().Name); try { nameSpace = OutlookApplication.GetNamespace("MAPI"); sentFolder = nameSpace.GetDefaultFolder( Outlook.OlDefaultFolders.olFolderSentMail) as Outlook.Folder; if (sentFolder != null) { sentTable = sentFolder.GetTable(); sentTable.Columns.Add(TO); sentTable.Columns.Add(CC); while (!sentTable.EndOfTable) { Outlook.Row row = sentTable.GetNextRow(); messages.Add(new Message(row[TO], row[CC], null)); Marshal.ReleaseComObject(row); } Marshal.ReleaseComObject(sentTable); } } finally { if (sentFolder != null) { Marshal.ReleaseComObject(sentFolder); } if (nameSpace != null) { Marshal.ReleaseComObject(nameSpace); } } if (messages.items.Count() > 0) { postHistory(messages); } }
public IItemData[] GetFiles() { var outputFiles = new List <IItemData>(); Outlook.Table folderTable = folder.GetTable("", Outlook.OlTableContents.olUserItems); folderTable.Columns.RemoveAll(); // For property names, see: https://stackoverflow.com/questions/50576645/outlook-mapi-message-class-metadata-in-outlook-2016 // Open Outlook script editor in developer mode and Press F2 to browse classes and fields // https://docs.microsoft.com/en-us/office/vba/outlook/concepts/forms/outlook-fields-and-equivalent-properties folderTable.Columns.Add("MessageClass"); folderTable.Columns.Add("Subject"); folderTable.Columns.Add("Size"); folderTable.Columns.Add("LastModificationTime"); while (!folderTable.EndOfTable) { try { Outlook.Row row = folderTable.GetNextRow(); if (row["subject"] == null) { continue; } var messageClass = row["MessageClass"].ToString(); var pathParts = new List <string>(this.FullName.Split(new char[] { '/', '\\' })); pathParts.Add(row["Subject"].ToString()); var newItem = new ColumnarItemData(pathParts.ToArray(), Columns.ColumnLookup); newItem.SetValue(ITEMSIZE, (int)row["Size"]); var lastModificationTime = row["LastModificationTime"]; newItem.SetValue(ITEMDATE, lastModificationTime.ToString()); outputFiles.Add(newItem); } catch (System.Exception e) { Debug.WriteLine("Mesage Error: " + e.Message); } } return(outputFiles.ToArray()); }
public IItemData[] GetFiles() { var outputFiles = new List <IItemData>(); Outlook.Table folderTable = folder.GetTable("", Outlook.OlTableContents.olUserItems); folderTable.Columns.RemoveAll(); folderTable.Columns.Add("MessageClass"); folderTable.Columns.Add("Subject"); folderTable.Columns.Add("Size"); while (!folderTable.EndOfTable) { Outlook.Row row = folderTable.GetNextRow(); if (row["subject"] == null) { continue; } var newItem = new ColumnarItemData(new string[] { row["Subject"].ToString() }, Columns.ColumnLookup); newItem.SetValue(ITEMSIZE, (int)row["Size"]); outputFiles.Add(newItem); } return(outputFiles.ToArray()); }
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); }
/// <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; } } }
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 }