Advertisement
parabola949

Outlook mail searching

May 7th, 2013
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.75 KB | None | 0 0
  1. using System;
  2. using System.Windows.Forms;
  3. using Outlook = Microsoft.Office.Interop.Outlook;
  4.  
  5. namespace OutlookDelete
  6. {
  7.     public partial class Form1 : Form
  8.     {
  9.         public Form1()
  10.         {
  11.             InitializeComponent();
  12.         }
  13.  
  14.         public void DeleteMail()
  15.         {
  16.             Outlook._Application myOlApp = null;
  17.             Outlook.NameSpace newNS = null;
  18.             myOlApp = new Outlook.Application();
  19.             newNS = myOlApp.GetNamespace("MAPI");
  20.             Outlook.MAPIFolder outbox = newNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderOutbox);
  21.             Outlook.MAPIFolder inbox = newNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
  22.             Outlook.MAPIFolder sent = newNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail);
  23.             //Outlook.MAPIFolder delete = newNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderDeletedItems);
  24.             string strSearchString = "AppV";
  25.             string filter = "@SQL=\"urn:schemas:httpmail:subject\" like '%" + strSearchString + "%'";
  26.             Outlook.Table outboxTable = outbox.GetTable(filter, Outlook.OlTableContents.olUserItems);
  27.             Outlook.Table inboxTable = inbox.GetTable(filter, Outlook.OlTableContents.olUserItems);
  28.             Outlook.Table sentTable = sent.GetTable(filter, Outlook.OlTableContents.olUserItems);
  29.             Outlook.Table deleteTable = sent.GetTable(filter, Outlook.OlTableContents.olUserItems);
  30.             while (!outboxTable.EndOfTable)
  31.             {
  32.                 // get the row item
  33.                 Outlook.Row myrow = outboxTable.GetNextRow();
  34.                 string subject = (string) myrow["Subject"];
  35.                 string EntryID = "";
  36.                 EntryID = Convert.ToString(myrow["EntryID"]);
  37.                 string StoreID = "";
  38.                 StoreID = Convert.ToString(outbox.StoreID);
  39.                 Outlook.MailItem oMail = (Outlook.MailItem) myOlApp.Session.GetItemFromID(EntryID, StoreID);
  40.  
  41.                 oMail.Delete();
  42.  
  43.                 // release objects
  44.                 myrow = null;
  45.             }
  46.             while (!inboxTable.EndOfTable)
  47.             {
  48.                 // get the row item
  49.                 Outlook.Row myrow = inboxTable.GetNextRow();
  50.                 string subject = (string) myrow["Subject"];
  51.                 string EntryID = "";
  52.                 EntryID = Convert.ToString(myrow["EntryID"]);
  53.                 string StoreID = "";
  54.                 StoreID = Convert.ToString(inbox.StoreID);
  55.  
  56.  
  57.                 Outlook.MailItem oMail = (Outlook.MailItem) myOlApp.Session.GetItemFromID(EntryID, StoreID);
  58.                 //MessageBox.Show(oMail.Subject);
  59.                 oMail.Delete();
  60.  
  61.  
  62.                 myrow = null;
  63.             }
  64.  
  65.             while (!sentTable.EndOfTable)
  66.             {
  67.                 // get the row item
  68.                 Outlook.Row myrow = sentTable.GetNextRow();
  69.                 string subject = (string)myrow["Subject"];
  70.                 string EntryID = "";
  71.                 EntryID = Convert.ToString(myrow["EntryID"]);
  72.                 string StoreID = "";
  73.                 StoreID = Convert.ToString(sent.StoreID);
  74.                
  75.                     Outlook.MailItem oMail = (Outlook.MailItem)myOlApp.Session.GetItemFromID(EntryID, StoreID);
  76.                     oMail.Delete();
  77.                
  78.                 // release objects
  79.                 myrow = null;
  80.             }
  81.  
  82.             deleteBin();
  83.             //while (!deleteTable.EndOfTable)
  84.             //{
  85.             //    // get the row item
  86.             //    Outlook.Row myrow = deleteTable.GetNextRow();
  87.             //    string subject = (string)myrow["Subject"];
  88.             //    string EntryID = "";
  89.             //    EntryID = Convert.ToString(myrow["EntryID"]);
  90.             //    string StoreID = "";
  91.             //    StoreID = Convert.ToString(delete.StoreID);
  92.             //    //delete.Items.Remove(1);
  93.             //    Outlook.MailItem oMail = (Outlook.MailItem)myOlApp.Session.GetItemFromID(EntryID, StoreID);
  94.             //    oMail.Delete();
  95.                
  96.             //    // release objects
  97.             //    myrow = null;
  98.  
  99.             //}
  100.            
  101.         }
  102.  
  103.         private void btnGo_Click(object sender, EventArgs e)
  104.         {
  105.             DeleteMail();
  106.         }
  107.  
  108.         public void deleteBin()
  109.         {
  110.            
  111.  
  112.             Outlook._Application outlookApp =
  113.                 new Outlook.Application();
  114.  
  115.             Outlook.MAPIFolder binF = (Outlook.MAPIFolder)outlookApp.Session.GetDefaultFolder
  116.                 (Outlook.OlDefaultFolders.olFolderDeletedItems);
  117.             while (binF.Items.Count != 0)
  118.             {
  119.                 binF.Items.Remove(1);
  120.             }  
  121.            
  122.         }
  123.  
  124.        
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement