Mr Harno
Mr Harno

Reputation: 318

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

                }

Upvotes: 8

Views: 1181

Answers (3)

AnGG
AnGG

Reputation: 821

Its seems that Load(string) returns void so it cannot be awaited

Try this :

if (getFile)
{
    fileAttachment.Load();
    string filePath = Path.Combine(outputDirectory, fileAttachment.Name);
    try
    {
        File.WriteAllBytes(filePath, fileAttachment.Content);
        filepaths.Add(filePath);
    }
    catch (Exception e)
    {
        // write somewhere
    }
}

Upvotes: 0

Abi Chhetri
Abi Chhetri

Reputation: 1437

Please Check the Detail From MicrosoftDoc here

foreach (Attachment attachment in message.Attachments)
{

   if (attachment is FileAttachment)
   {
         FileAttachment fileAttachment = attachment as FileAttachment;
         DirectoryInfo directory = Directory.CreateDirectory("absolute path");
         String name = fileAttachment.Name; 
         fileAttachment.Load("absolute path" + name);
         
                            
  }
}

Upvotes: 0

Rich
Rich

Reputation: 3720

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

Upvotes: 4

Related Questions