C# - 连接到 Outlook 2010 邮箱以获取 Xls 文件



我有一个 c# 脚本,它连接到 OFFICE 365 邮箱,然后使用 xls 文件接收任何电子邮件并将其放在共享文件夹中。问题是现在 OFFICE 365 邮箱已成为 Outlook 2010 本地邮箱,并且脚本已停止工作。

我的问题是我使用什么服务 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("xxreturns@xxxxxxxxxxxx.co.uk", "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;
        }
    }
}

不确定这是否适用于Outlook 2010,但也许此链接可以帮助您:https://msdn.microsoft.com/en-us/office/office365/api/mail-rest-operations

最新更新