Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#-连接到Outlook 2010邮箱以获取Xls文件_C#_Ssis_Outlook - Fatal编程技术网

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

C#-连接到Outlook 2010邮箱以获取Xls文件,c#,ssis,outlook,C#,Ssis,Outlook,我有一个c#脚本,可以连接到OFFICE 365邮箱,然后用xls文件接收任何电子邮件,并将其放在共享文件夹中。问题是,OFFICE 365邮箱现在已成为Outlook 2010内部部署邮箱,并且脚本已停止工作 我的问题是我使用什么服务URL和服务凭据,它是相同的语法还是我需要在脚本中使用新的连接方式 旧书 using System; using System.Data; using Microsoft.SqlServer.Dts.Runtime; using System.Windows.Fo

我有一个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;
        }
    }
}
使用系统;
使用系统数据;
使用Microsoft.SqlServer.Dts.Runtime;
使用System.Windows.Forms;
Net系统;
使用System.Net.Security;
使用System.Security.Cryptography.X509证书;
使用Microsoft.Exchange.WebServices.Data;
名称空间ST_0710846949654fbd84606ec3011bd081.csproj
{
[System.AddIn.AddIn(“ScriptMain”,Version=“1.0”,Publisher=”“,Description=”“)]
公共部分类ScriptMain:Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
#区域VSTA生成代码
枚举脚本结果
{
Success=Microsoft.SqlServer.Dts.Runtime.dtsesecresult.Success,
Failure=Microsoft.SqlServer.Dts.Runtime.dtsesecresult.Failure
};
#端区
/*
执行引擎在任务执行时调用此方法。
要访问对象模型,请使用Dts属性。连接、变量、事件、,
和日志功能作为Dts属性的成员提供,如以下示例所示。
要引用变量,请调用Dts.Variables[“MyCaseSensitiveVariableName”].Value;
要发布日志条目,请调用Dts.log(“这是我的日志文本”,999,null);
要触发事件,请调用Dts.Events.firefinformation(99,“测试”,“点击帮助消息”,“0”,true);
要使用connections集合,请使用以下内容:
ConnectionManager cm=Dts.Connections.Add(“OLEDB”);
cm.ConnectionString=“数据源=本地主机;初始目录=AdventureWorks;提供程序=SQLNCLI10;集成安全性=SSPI;自动转换=False;”;
从该方法返回之前,请设置Dts.TaskResult的值以指示成功或失败。
要打开“帮助”,请按F1。
*/
公共图书馆
{
ExchangeService服务=新的ExchangeService();
service.TraceEnabled=true;
service.TraceFlags=TraceFlags.All;
service.Credentials=新的WebCredentials(“xxreturns@xxxxxxxxxxxx.co.uk“,”PasswordXXXXXXXXX“,”mail.xxxxxxxxxxxxx.co.uk”);
service.Url=新的Uri(“https://mail.xxxxxxxxxxx.co.uk/owa");
//可变人口
字符串FileName1=null;
字符串attSaveLocation=Dts.Variables[“User::attSaveLocation”].Value.ToString();
字符串destfold=Dts.Variables[“User::destFolder”].Value.ToString();
字符串emailFrom=Dts.Variables[“User::emailFrom”].Value.ToString();
string filetype=Dts.Variables[“User::filetype”].Value.ToString();
//在电子邮件文件夹中查找项目
FindItemsResultsFoundItems=
service.FindItems(WellKnownFolderName.Inbox,new ItemView(600));//可以限制提取结果的数量
foreach(foundItems中的项目)
{
字符串tmpitemid;
字符串处理=空;
tmpitemid=item.Id.ToString();
如果(项目为EmailMessage)
{
//绑定到现有消息项,请求其Id属性(使用tmpitemid)及其附件集合。
EmailMessage foundEmail=EmailMessage.Bind(服务、新项目ID(tmpitemid)、新属性集(BasePropertySet.IdOnly、ItemSchema.Attachments));
EmailMessage foundEmail2=(EmailMessage)项;
FindFoldersResults findResults=service.FindFolders(WellKnownFolderName.Inbox,新文件夹视图(10));
//获取exchange地址的发件人电子邮件地址
字符串fromaddress=null;
NameResolutionCollection nd=service.ResolveName(foundEmail2.From.Address);
foreach(名称分辨率为nm,单位为nd)
{
如果(nm.Mailbox.RoutingType==“SMTP”)
{
fromaddress=nm.Mailbox.Address.ToLower();
}
其他的
{
fromaddress=foundEmail2.From.Address.ToString().ToLower();
}
}
//其他地址
if(fromaddress==null)
{
fromaddress=foundEmail2.From.Address.ToString().ToLower();
}
//如果电子邮件地址与参数类似
if(fromaddress.Contains(emailFrom))
{
//进程附件
foreach(foundEmail.Attachments中的附件)
{
如果(附件是文件附件)
{
FileAttachment FileAttachment=作为文件附件的附件;
FileName1=attSaveLocation+fileAttachment.Name;
if(fileAttachment.Name.Contains(filetype))
{
fileAttachment.Load(FileName1);
已处理=“Y”;
}
}
}
如果(已处理=“Y”)
{
//获取邮件根文件夹中的所有文件夹。
Folder rootfolder=Folder.Bind(服务,WellKnownFolderName.Inb