Save the ContentBytes or append /$value to the request URL to get raw content.
: Use an IConfidentialClientApplication to obtain an access token from Azure. Locate Message : List messages in a folder (e.g., "Inbox"). Download Attachment : Retrieve the attachment list for a specific message ID. Check for FileAttachment types. c# download email attachment from exchange server
// Retrieve specific message's attachments var attachments = await graphClient.Users[userId] .Messages[messageId] .Attachments .GetAsync(); foreach (var attachment in attachments.Value) { if (attachment is FileAttachment fileAttachment) { // Save the content bytes to a local file await File.WriteAllBytesAsync($"C:\\temp\\{fileAttachment.Name}", fileAttachment.ContentBytes); } } Use code with caution. 2. Legacy Approach: EWS Managed API (On-Premises) Save the ContentBytes or append /$value to the
The Exchange Web Services (EWS) Managed API is the standard for on-premises servers. Note that Microsoft has deprecated EWS for Exchange Online. Get attachments by using EWS in Exchange - Microsoft Learn Download Attachment : Retrieve the attachment list for
For Microsoft 365/Exchange Online, Microsoft recommends using the Microsoft Graph API via the Microsoft.Graph SDK.