Here’s a sample code in C# using the .NET framework to download attachments from Outlook
using System; using System.IO; using System.Runtime.InteropServices; using Outlook = Microsoft.Office.Interop.Outlook; namespace OutlookAttachmentDownloaderExample { class Program { static void Main(string[] args) { // Define the Outlook application Outlook.Application outlookApp = new Outlook.Application(); // Define the Outlook mail folder and the mail item Outlook.MAPIFolder inbox = outlookApp.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox); Outlook.MailItem mailItem = inbox.Items[1] as Outlook.MailItem; // Define the attachment download path string downloadPath = @"C:\Users\UserName\Documents\Attachments\"; // Loop through each attachment in the mail item foreach (Outlook.Attachment attachment in mailItem.Attachments) { // Define the attachment file name and download path string fileName = attachment.FileName; string filePath = Path.Combine(downloadPath, fileName); // Download the attachment attachment.SaveAsFile(filePath); Console.WriteLine("Attachment {0} has been downloaded to {1}.", fileName, filePath); } Console.WriteLine("Attachments have been downloaded from the Outlook mail item."); Console.ReadLine(); } } }
In this example, the Microsoft Office Interop Outlook library is used to download attachments from Outlook. The Application
class is used to create a new instance of the Outlook application. The MAPIFolder
class is used to define the Outlook mail folder, and the MailItem
class is used to define the mail item from which the attachments will be downloaded. The Attachment
class is used to loop through each attachment in the mail item. The SaveAsFile
method of the Attachment
object is used to download the attachment to the specified download path. Finally, the downloaded attachments are printed to the console.