Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Downloading attachments using sample code #40

Open
webbexpert opened this issue Sep 14, 2019 · 5 comments
Open

Downloading attachments using sample code #40

webbexpert opened this issue Sep 14, 2019 · 5 comments

Comments

@webbexpert
Copy link

Hi! Not sure if theres a bug with the Load method; but the following code appears to write a 0 byte file to disk

                // Read 100 mails
                foreach (var email in service.FindItems(WellKnownFolderName.Inbox, new ItemView(100)).Result)
                {
                    if (email.HasAttachments)
                    {
                        var attachmentMessage = EmailMessage.Bind(service, email.Id, new PropertySet(ItemSchema.Attachments)).Result;

                        foreach (var attachment in attachmentMessage.Attachments)
                        {
                            string name = null;

                            if (attachment is FileAttachment)
                            {
                                FileAttachment fileAttachment = attachment as FileAttachment;
                                // Load the attachment into a file.
                                // This call results in a GetAttachment call to EWS.
                                fileAttachment.Load();

                                name = fileAttachment.Name;
                                fileAttachment.Load("C:\\Temp\\" + name);

                                Debug.WriteLine("File attachment name: " + name);
                            }
                            else // Attachment is an item attachment.
                            {
                                ItemAttachment itemAttachment = attachment as ItemAttachment;
                                // Load attachment into memory and write out the subject.
                                // This does not save the file like it does with a file attachment.
                                // This call results in a GetAttachment call to EWS.
                                itemAttachment.Load();

                                Debug.WriteLine("Item attachment name: " + itemAttachment.Name);
                            }
                        }
                    }

                    Debug.WriteLine("Subject: " + email.Subject);
                }

The project I've used for reference is located here:
https://docs.microsoft.com/en-us/previous-versions/office/developer/exchange-server-2010/dd633665(v%3Dexchg.80)

Thanks for your time!

@scara1701
Copy link

.Load() is a task, I think your code resumes before this is completed.

Try:
await fileAttachment.Load();

Greetings

@webbexpert
Copy link
Author

webbexpert commented Sep 26, 2019

Hi Scara! Thanks for your reply

I didn't spot the async task, as the example was passing a string param to the synchronous version Load(string)...

Switching to a task and calling Load() fixed the missing content. Thank you!

@LubosVoska
Copy link

This works for me

public static void GetAttachmentsFromEmail(ExchangeService service, Item item)
{
	// Bind to an existing message item and retrieve the attachments collection.
	// This method results in an GetItem call to EWS.
	EmailMessage message = EmailMessage.Bind(service, item.Id, new PropertySet(ItemSchema.Attachments));
	// Iterate through the attachments collection and load each attachment.
	foreach (Attachment attachment in message.Attachments)
	{
		if (attachment is FileAttachment)
		{
			FileAttachment fileAttachment = attachment as FileAttachment;
			// Load the attachment into a file.
			// This call results in a GetAttachment call to EWS.
			DirectoryInfo directory = Directory.CreateDirectory("C:\\temp\\");
			fileAttachment.Load("C:\\temp\\" + fileAttachment.Name);			

		}
	}
}

@777seyran
Copy link

please add some working code here.I've the same problem with filesize.

@ChrisH-GDG
Copy link

ChrisH-GDG commented Jun 17, 2020

Just as an FYI for anyone else who stumbles across this issue... the FileAttachment.Load("outputdir") isn't Async which i think is the issue. I got round it with the below I'd already done the fileattachment validation prior to this stage etc.

 var f1 =  await fileAttachment.Load();
 if (f1 != null)
{
  FileAttachment loaded = f1.First().Attachment as FileAttachment;
  File.WriteAllBytes(outputDirectory + fileAttachment.Name, loaded.Content);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants