Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Exchange Web Services Attachment Saving not working after .Net Core 3.1 Migration

Hopefully a very simple one. I migrated an application to .net-core 3.1 from .net framework 4.7 that uses EWS. Other than making numerous interactions Async all seems to be working fine without any change. Apart from downloading attachments from the email. Whilst debugging and using the object examiner the FileAttachment object exists and the object properties/size look exactly what I expect. The FileAttachment.Load(string outputPath) creates a file, however, the file has 0KB content size. So the shell has been created but no data has been streamed into it. Probably I'm missing something obvious but the obvious is escaping me. Tried using the stream and output path methods, the same result. The function works fine still in the .net framework version. Any ideas very greatly appreciated?

                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);
                                }
                            }
                        }
                    }

                }
like image 772
Mr Harno Avatar asked May 06 '20 08:05

Mr Harno


1 Answers

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);
}

This github comment fixed it for me

like image 189
Rich Avatar answered Sep 28 '22 05:09

Rich