.Net Core 3.1 迁移后 C# Exchange Web 服务附件保存不起作用

标签 c# exchangewebservices asp.net-core-3.1

希望是一个非常简单的。我将应用程序迁移到 .net-core 3.1 来自 .net使用 EWS 的框架 4.7 .除了进行大量交互异步之外,所有似乎都可以正常工作而无需任何更改。除了从电子邮件下载附件。在调试和使用对象检查器时,FileAttachment对象存在并且对象属性/大小看起来正是我所期望的。 FileAttachment . Load (string outputPath) 创建一个文件,但是,该文件的内容大小为 0KB。所以 shell 已经创建,但没有数据流入其中。可能我错过了一些明显的东西,但明显的东西正在逃避我。尝试使用流和输出路径方法,结果相同。该功能在 .net 中仍然正常工作框架版本。任何想法非常感谢?

                foreach (EmailMessage email in orderedList)
                {

                    EmailMessage message = await EmailMessage.Bind(_service, email.Id, new PropertySet(BasePropertySet.FirstClassProperties, EmailMessageSchema.Attachments));

                    if (email.HasAttachments)
                    {
                        foreach (Attachment attachment in message.Attachments)
                        {
                            bool getFile = false;

                            if (attachment is FileAttachment)
                            {
                                FileAttachment fileAttachment = attachment as FileAttachment;

                                if (!string.IsNullOrEmpty(filename) && fileAttachment.Name.StartsWith(filename))
                                {
                                    getFile = true;
                                }
                                else if (string.IsNullOrEmpty(filename))
                                {
                                    getFile = true;
                                }

                                if (getFile)
                                {

                                    //var response = await fileAttachment.Load();

                                    //FileStream theStream = new FileStream(outputDirectory + fileAttachment.Name, FileMode.Create, FileAccess.ReadWrite);
                                    //fileAttachment.Load(theStream);
                                    //theStream.Close();
                                    //theStream.Dispose();

                                    fileAttachment.Load(outputDirectory + fileAttachment.Name);
                                    message.IsRead = true;
                                    await  message.Update(ConflictResolutionMode.AlwaysOverwrite, true);
                                    filepaths.Add(outputDirectory + fileAttachment.Name);
                                }
                            }
                        }
                    }

                }

最佳答案

https://github.com/sherlock1982/ews-managed-api/issues/40

var f1 =  await fileAttachment.Load();
 if (f1 != null)
{
  FileAttachment loaded = f1.First().Attachment as FileAttachment;
  File.WriteAllBytes(outputDirectory + fileAttachment.Name, loaded.Content);
}
这个 github 评论为我修复了它

关于.Net Core 3.1 迁移后 C# Exchange Web 服务附件保存不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61630559/

相关文章:

ember.js - .NET Core 3.1 CORS 不适用于 Ember (3.12) Web UI

c# - Moq 与 PrivateObject 不兼容吗?

c# - 使用 LINQ、C# 解析 XML 并将元素转换为类型化集合

php - Office365 API - 访问其他用户/房间的日历

Exchange 2013 上的 C# EWS Api : Get an attachment into a Stream

c# - 自动将 JWT 加载到 User.Identity (ASP.NET Core 3.1)

c# - ConnectionMultiplexer 如何处理断开连接?

c# - 从 Controller 传递一些数据到 View

c# - 如何使用 EWS 托管 API 保存项目附件