如何使用兑换

时间:2017-10-03 13:10:39

标签: c# .net multithreading visual-studio outlook-redemption

我想使用兑换连接到Office 365并从收件箱和其他文件夹下载所有电子邮件,我的开发机器

  • Windows 10 X64
  • Visual Studio 2015 Pro
  • 我的开发计算机上未安装Outlook
  • 从此link
  • 安装 Microsoft Exchange Server MAPI客户端和协作数据对象1.2.1
  • 使用此命令在form1.cs文件regsvr32.exe <fullpath>\redemption.dll
  • 中注册redemption.dll
  • 我的项目中的Refrenced Interop.Redemption.dll

这是我尝试连接到Office365邮箱的代码

  string codeBase = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;
            UriBuilder uri = new UriBuilder(codeBase);
            string path = Uri.UnescapeDataString(uri.Path);
            path = Path.GetDirectoryName(path);

            RedemptionLoader.DllLocation32Bit = string.Concat(path, @"\Redemption.dll");
            RedemptionLoader.DllLocation64Bit = string.Concat(path, @"\Redemption64.dll");

            RDOSession session = RedemptionLoader.new_RDOSession();

            session.LogonHostedExchangeMailbox("smtp.office365.com", "abc@domain.com","xxxx");// getting error at this line               

            if (session.LoggedOn)
            {
                RDOFolder contactsFolder;    
                contactsFolder = session.GetDefaultFolder(rdoDefaultFolders.olFolderContacts);
                var i = contactsFolder.Folders.Count;

                foreach (var item in contactsFolder.Folders)
                {
                    var obj = (RDOFolder2)item;
                    var name = obj.AddressBookName;    
                }
            }

这是我收到的错误

  

System.Runtime.InteropServices.COMException未处理
  ErrorCode = -2147023541 HResult = -2147023541 Message =指定的   域名要么不存在要么无法联系
  Source = Redemption.RDOSession StackTrace:          at Redemption.IRDOSession.LogonHostedExchangeMailbox(String SMTPAddress,String UserName,String Password)          在Office 365Backup.Form1.CreateSession()在D:\ Data \ Projects \ DotNet \ Office365Backup \ Source \ Office365Backup \ Form1.cs:line   40          在Office 365Backup.Form1..ctor()中的D:\ Data \ Projects \ DotNet \ Office365Backup \ Source \ Office365Backup \ Form1.cs:line   19          在Office365Backup.Program.Main()中的D:\ Data \ Projects \ DotNet \ Office365Backup \ Source \ Office365Backup \ Program.cs:line   25          在System.AppDomain._nExecuteAssembly(RuntimeAssembly程序集,String [] args)          在System.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args)          在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()          在System.Threading.ThreadHelper.ThreadStart_Context(对象状态)          在System.Threading.ExecutionContext.RunInternal(ExecutionContext   executionContext,ContextCallback回调,对象状态,布尔值   preserveSyncCtx)          at System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean   preserveSyncCtx)          在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态)          在System.Threading.ThreadHelper.ThreadStart()InnerException:

成功完成此任务的任何建议。

由于

1 个答案:

答案 0 :(得分:1)

要连接到Office 365邮箱(或Exchange 2013或2016上的任何邮箱),您将需要Outlook版本的MAPI(Outlook 2010 SP2,Outlook 2013 SP1或Outlook 2016) - 独立版本的MAPI无法连接因为Exchange会拒绝连接。

另请注意,LogonHostedExchangeMailbox中的第一个参数是用户的SMTP地址,而不是服务器名称。