2011-09-16 32 views
1

我試圖訪問並閱讀Outlook郵件。我嘗試了下面的代碼,但它給了我一個安全警告彈出窗口,說「一個程序試圖訪問存儲在Outlook Express中的電子郵件地址信息」。當我嘗試在foreach中訪問Microsoft.Office.Interop.Outlook.MailItem。如何允許通過C#永久訪問Outlook前景

const string OUTLOOK_PROCESSNAME = "OUTLOOK"; 
    const string OUTLOOK_APPLICATIONNAME = "Outlook.Application"; 

    private static Microsoft.Office.Interop.Outlook.Application StartOutlookApplication() 
    { 
     return StartApplication(OUTLOOK_PROCESSNAME, OUTLOOK_APPLICATIONNAME) as Microsoft.Office.Interop.Outlook.Application; 
    } 

    private static object StartApplication(string processName, string applicationName) 
    { 
     // Application object 
     object app = null; 
     try 
     { 

      // is there an existing application object ? 
      if (Process.GetProcessesByName(processName).Length > 0) 
      { 

       // use the GetActiveObject method to attach an existing application object 
       app = Marshal.GetActiveObject(applicationName); 
      } 
      if (app == null) 
      { 
       // create a new instance 
       Type t = Type.GetTypeFromProgID(applicationName); 
       app = Activator.CreateInstance(t); 
      } 


     } 
     catch (System.Exception ex) 
     { 
      // Some Logging 
      Trace.WriteLine(string.Format("Error while starting the Application: {0}", applicationName)); 
     } 
     return app; 

    } 


Microsoft.Office.Interop.Outlook.MAPIFolder subFolder = null; 
      Microsoft.Office.Interop.Outlook.Application app = StartOutlookApplication(); 
      Microsoft.Office.Interop.Outlook.NameSpace NS = app.GetNamespace("MAPI"); 
      Microsoft.Office.Interop.Outlook.MAPIFolder inboxFld = NS.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox); 
      lastupdateddate = getmostrecentupdatetime(); 
      DateTime lastupdated=Convert.ToDateTime(lastupdateddate); 
      subFolder = inboxFld.Folders[Inboxpath]; 

      foreach (Microsoft.Office.Interop.Outlook.MailItem t in subFolder.Items) 
      { 
       if (t.SenderEmailAddress.Contains(senderemail)) 
       { 

請有人幫助我。我需要運行我的程序,而不顯示此警告消息。

回答

相關問題