How to download attachments from outlook
There are a few solid ways to download email attachments from Microsoft Outlook using C#. The right approach depends on whether you’re working with the desktop app, a mailbox on Microsoft 365, or just IMAP access.
1) Using Outlook Interop (desktop Outlook installed)
This approach automates the locally installed Outlook client via Microsoft Office Interop Outlook.
When to use
• You have Outlook installed on the machine
• You’re building an internal tool
• Not ideal for servers (COM + UI dependency)
Outlook Interop Example
using Outlook = Microsoft.Office.Interop.Outlook;
using System.IO;
class Program
{
static void Main()
{
var outlookApp = new Outlook.Application();
var ns = outlookApp.GetNamespace("MAPI");
var inbox = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
foreach (Outlook.MailItem mail in inbox.Items)
{
if (mail.Attachments.Count > 0)
{
foreach (Outlook.Attachment att in mail.Attachments)
{
string path = Path.Combine(@"C:\Attachments\", att.FileName);
att.SaveAsFile(path);
}
}
}
}
}
Pros
• Easy if Outlook is already used
• Full access to mailbox features
Cons
• Requires Outlook installed
• Not suitable for background/server apps
2) Using Microsoft Graph API (recommended)
Use Microsoft Graph API to access Outlook/Office 365 mailboxes.
When to use Microsoft Graph API
• Cloud apps
• Web apps / services
• Production systems
Steps
• Register app in Microsoft Azure Portal
• Grant Mail permissions (Mail.Read)
• Authenticate (OAuth2)
• Fetch messages and attachments
Microsoft Graph API Example (simplified)
var messages = await graphClient.Me.Messages
.Request()
.GetAsync();
foreach (var msg in messages)
{
var attachments = await graphClient.Me.Messages[msg.Id].Attachments
.Request()
.GetAsync();
foreach (var att in attachments)
{
if (att is FileAttachment fileAttachment)
{
File.WriteAllBytes(
Path.Combine(@"C:\Attachments\", fileAttachment.Name),
fileAttachment.ContentBytes
);
}
}
}
Pros
• Modern, secure (OAuth)
• Works without Outlook installed
• Scalable
Cons
• Requires setup (Azure app registration)
• Slightly more complex
3) Using IMAP (generic email access)
You can use libraries like MailKit.
When to use IMAP?
Any email provider (including Outlook)
Lightweight apps
No Microsoft-specific dependency
Example
using MailKit.Net.Imap;
using MailKit;
using MimeKit;
using System.IO;
using (var client = new ImapClient())
{
client.Connect("outlook.office365.com", 993, true);
client.Authenticate("your@email.com", "password");
var inbox = client.Inbox;
inbox.Open(FolderAccess.ReadOnly);
for (int i = 0; i < inbox.Count; i++)
{
var message = inbox.GetMessage(i);
foreach (var attachment in message.Attachments)
{
if (attachment is MimePart part)
{
var filePath = Path.Combine(@"C:\Attachments\", part.FileName);
using (var stream = File.Create(filePath))
{
part.Content.DecodeTo(stream);
}
}
}
}
client.Disconnect(true);
}
Pros
• Works everywhere
• No Outlook dependency
• Simple
Cons
• Less feature-rich than Graph
• Requires handling auth (may need app passwords)