2017-02-14 57 views
0

我有一個c#腳本連接到OFFICE 365郵箱,然後將任何帶有xls文件的電子郵件放到共享文件夾中。問題是,現在OFFICE 365郵箱已成爲駐留郵箱上的Outlook 2010,並且該腳本已停止工作。C# - 連接到Outlook 2010郵箱取Xls文件

我的問題是我使用的服務URL和服務憑證,它是相同的語法,還是我需要一種新的連接方式?

舊腳本

using System; 
using System.Data; 
using Microsoft.SqlServer.Dts.Runtime; 
using System.Windows.Forms; 
using System.Net; 
using System.Net.Security; 
using System.Security.Cryptography.X509Certificates; 
using Microsoft.Exchange.WebServices.Data; 

namespace ST_0710846949654fbd84606ec3011bd081.csproj 
{ 
    [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")] 
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase 
    { 

     #region VSTA generated code 
     enum ScriptResults 
     { 
      Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success, 
      Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure 
     }; 
     #endregion 

    /* 
     The execution engine calls this method when the task executes. 
     To access the object model, use the Dts property. Connections, variables, events, 
     and logging features are available as members of the Dts property as shown in the following examples. 

     To reference a variable, call Dts.Variables["MyCaseSensitiveVariableName"].Value; 
     To post a log entry, call Dts.Log("This is my log text", 999, null); 
     To fire an event, call Dts.Events.FireInformation(99, "test", "hit the help message", "", 0, true); 

     To use the connections collection use something like the following: 
     ConnectionManager cm = Dts.Connections.Add("OLEDB"); 
     cm.ConnectionString = "Data Source=localhost;Initial Catalog=AdventureWorks;Provider=SQLNCLI10;Integrated Security=SSPI;Auto Translate=False;"; 

     Before returning from this method, set the value of Dts.TaskResult to indicate success or failure. 

     To open Help, press F1. 
    */ 


     public void Main() 
     { 
      ExchangeService service = new ExchangeService(); 
      service.TraceEnabled = true; 
      service.TraceFlags = TraceFlags.All; 



      service.Credentials = new WebCredentials("[email protected]", "PasswordXXXXXXXXX", "mail.xxxxxxxxxxxxxxx.co.uk"); 
      service.Url = new Uri("https://mail.xxxxxxxxxxx.co.uk/owa"); 

      // Variable population 
      string FileName1 = null; 
      string attSaveLocation = Dts.Variables["User::attSaveLocation"].Value.ToString(); 
      string destfold = Dts.Variables["User::destFolder"].Value.ToString(); 
      string emailFrom = Dts.Variables["User::emailFrom"].Value.ToString(); 
      string filetype = Dts.Variables["User::filetype"].Value.ToString(); 

      //find items in the email folder 
      FindItemsResults<Item> foundItems = 
      service.FindItems(WellKnownFolderName.Inbox, new ItemView(600)); //can limit how many results are pulled 

      foreach (Item item in foundItems) 
      { 
       string tmpitemid; 
       string processed = null; 
       tmpitemid = item.Id.ToString(); 
       if (item is EmailMessage) 
       { 
        // Bind to an existing message item, requesting its Id property (using the tmpitemid) plus its attachments collection. 
        EmailMessage foundEmail = EmailMessage.Bind(service, new ItemId(tmpitemid), new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments)); 
        EmailMessage foundEmail2 = (EmailMessage)item; 
        FindFoldersResults findResults = service.FindFolders(WellKnownFolderName.Inbox, new FolderView(10)); 

        //get the from e-mail address for exchange addresses 
        string fromaddress = null; 
        NameResolutionCollection nd = service.ResolveName(foundEmail2.From.Address); 
        foreach (NameResolution nm in nd) 
        { 
         if (nm.Mailbox.RoutingType == "SMTP") 
         { 
          fromaddress = nm.Mailbox.Address.ToLower(); 
         } 
         else 
         { 
          fromaddress = foundEmail2.From.Address.ToString().ToLower(); 
         } 
        } 
        //for other addresses 
        if (fromaddress == null) 
        { 
         fromaddress = foundEmail2.From.Address.ToString().ToLower(); 
        } 
        //if the email address is like the parameter 
        if (fromaddress.Contains(emailFrom)) 
        { 
         //process attachments 
         foreach (Attachment attachment in foundEmail.Attachments) 
         { 
          if (attachment is FileAttachment) 
          { 
           FileAttachment fileAttachment = attachment as FileAttachment; 
           FileName1 = attSaveLocation + fileAttachment.Name; 
           if (fileAttachment.Name.Contains(filetype)) 
           { 
            fileAttachment.Load(FileName1); 
            processed = "Y"; 
           } 
          } 
         } 
         if (processed == "Y") 
         { 
          // Get all the folders in the message's root folder. 
          Folder rootfolder = Folder.Bind(service, WellKnownFolderName.Inbox); 
          rootfolder.Load(); 
          foreach (Folder folder in rootfolder.FindFolders(new FolderView(100))) 
          { 
           if (folder.DisplayName == destfold) 
           { 
            foundEmail2.Move(folder.Id); 
           } 
          } 
         } 
        } 
       } 
      } 
      Dts.TaskResult = (int)ScriptResults.Success; 
     } 
    } 
} 
+0

的Office 365是主要基於網絡應用,辦公2010沒有。 OWA是交換而不是outlook .. OWA不一定需要以前設置。 – BugFinder

回答