title: C# Outlook relating Inbox and sent items
body: <p>I am working on a software where i am able to retrieve the inbox and sent items from outlook. What i want to do is to relate the inbox emails with replies (if someone has sent a reply to that email). The list should be displayed in this order</p>
  
  <p>Sender@abc.com    Incoming    Subject         Received Time
  sender@abc.com    Reply       Subject         Sent time</p>
  
  <p>What i am planning to do is to retrieve inbox items in one datatable and the sent items in another datatable. It reads the emails one by one on the basis of sender email and the subject, then searches that sender and email in the sent items and if it matches, merge that to a third datatable. </p>
  
  <p>is there any other better way to do so?</p>
  
  <p>Here is the code:</p>
  
  <pre><code> private DataTable GetInboxItems()
  
      {
          DataTable inboxTable;
          //try
          //{
          filter = "[ReceivedTime] &gt;= '" + dtpStartDate.Value.ToString("dd/MM/yyyy 12:00 AM") + "' and [ReceivedTime] &lt;= '" + dtpEndDate.Value.ToString("dd/MM/yyyy  11:59 PM") + "'";
          Outlook.Application outlookApp = GetApplicationObject();
          Outlook.Folder root = outlookApp.Session.DefaultStore.GetRootFolder() as Outlook.Folder;
          EnumerateFolders(root);
          //string filter = "[ReceivedTime] &gt; '" + dtpStartDate.Value.ToString("dd/MM/yyyy") + "'";
  
          //inbox
          Outlook.MAPIFolder inboxFolder = outlookApp.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
          inboxTable = CreateTable();
          int count = 0;
  
  
          if (inboxFolder.Items.Count &gt; 0)
          {
  
              var restrictedItems = inboxFolder.Items.Restrict(filter);
              restrictedItems.Sort("[ReceivedTime]", true); //descending
              //foreach (var item in inboxFolder.Items)
              foreach (var item in restrictedItems)
              {
                  var mail = item as Outlook.MailItem;
                  if (mail != null)
                  {
                      //try
                      //{
                      DataRow row = inboxTable.NewRow();
                      //row["sn"] = (++count).ToString();
                      row["sn"] = mail.EntryID + " " + mail.ReceivedByEntryID;
                      row["MailType"] = "Inbox";
                      row["SenderName"] = mail.SenderName;
                      row["SenderEmail"] = mail.SenderEmailAddress;
                      row["ReceivedDate"] = mail.ReceivedTime;
                      row["Subject"] = mail.Subject;
                      row["Body"] = mail.Body != null ? (mail.Body.Length &gt; 25 ? mail.Body.Substring(0, 25) : mail.Body) : null;
                      //row["Body"] = mail.Body != null ? mail.Body : "";
                      row["MailSize"] = mail.Size.ToString();
                      string attachments = null;
                      if (mail.Attachments.Count &gt; 0)
                      {
                          foreach (var attachment in mail.Attachments)
                          {
                              if (((Outlook.Attachment)attachment) != null)
                                  //attachments = ((Outlook.Attachment)attachment).FileName + " " + ((Outlook.Attachment)attachment).Size.ToString() + ", ";
                                  attachments += (((Outlook.Attachment)attachment).Size / 1024).ToString() + " KB, ";
                          }
                      }
  
  
                      row["AttachmentCount"] = mail.Attachments.Count;
                      if (attachments != null)
                          row["AttachmentSize"] = attachments.Substring(0, attachments.Length - 2);
  
                      inboxTable.Rows.Add(row);
                  }
                  //catch (Exception ex)
                  //{
  
                  //    return null;
                  //}
  
              }
          }
  
          return inboxTable;
      }
  </code></pre>