Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7514105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:23:44+00:00 2026-05-30T00:23:44+00:00

I am new 2 C# and I have been given a task… I have

  • 0

I am new 2 C# and I have been given a task… I have to write a C# code to download the sent email attachments and subject of email from Outlook 2007 to a local drive or any specified location. How do I do that? I am able to get the attachments which are in inbox. Can any one please help me getting the mails sent through Outlook?

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    this.Application.NewMail += new
      Microsoft.Office.Interop.Outlook.
      ApplicationEvents_11_NewMailEventHandler(ThisApplication_NewMail);
}

private void ThisApplication_NewMail()
{
    Outlook.MAPIFolder SentMail = this.Application.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail);
    Outlook.Items SentMailItems = SentMail.Items;
    Outlook.MailItem newEmail = null;
    //SentMailItems = SentMailItems.Restrict("[Unread] = true");
    try
    {
        foreach (object collectionItem in SentMailItems)
        {
            newEmail = collectionItem as Outlook.MailItem;
            if (newEmail != null)
            {
                if (newEmail.Attachments.Count > 0)
                {
                    for (int i = 1; i <= newEmail.Attachments.Count; i++)
                    {
                        newEmail.Attachments[i].SaveAsFile(@"C:\TestFileSave\" + newEmail.Attachments[i].FileName);
                    }
                }
            }
        }
    }
    catch (Exception ex)
    {
        string errorInfo = (string)ex.Message
            .Substring(0, 11);
        if (errorInfo == "Cannot save")
        {
            MessageBox.Show(@"Create Folder C:\TestFileSave");
        }
    }
}

Thanks in Advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T00:23:45+00:00Added an answer on May 30, 2026 at 12:23 am

    I’ve tested your code, the SaveAsFile() method of Attachment class instance throws:

    {System.IO.DirectoryNotFoundException: Cannot save the attachment.
    Path does not exist. Verify the path is correct. at
    Microsoft.Office.Interop.Outlook.Attachment.SaveAsFile(String Path)
    … }

    So, it is necessary to make sure that the directory for saving attachments exists.

    private void ThisApplication_NewMail()
    {
        const string destinationDirectory = @"C:\TestFileSave";
    
        if (!Directory.Exists(destinationDirectory))
        {
            Directory.CreateDirectory(destinationDirectory);
        }
    
        MAPIFolder sentMail = Application.ActiveExplorer().Session.GetDefaultFolder(OlDefaultFolders.olFolderSentMail);
        Items sentMailItems = sentMail.Items;
        try
        {
            foreach (object collectionItem in sentMailItems)
            {
                MailItem newEmail = collectionItem as MailItem;
                if (newEmail == null) continue;
    
                if (newEmail.Attachments.Count > 0)
                {
                    for (int i = 1; i <= newEmail.Attachments.Count; i++)
                    {
                        string filePath = Path.Combine(destinationDirectory, newEmail.Attachments[i].FileName);
                        newEmail.Attachments[i].SaveAsFile(filePath);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
    }
    

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been given a new task from the client which is basically creating
I am new 2 C# and i have been given a task... I have
In my new task, I have been asked to write specifications for a new
I am new to Qt , I have been given this task where i
I am new to the SharePoint Development world and I have been given the
I have been given the task of rewriting an internal utility in .Net for
I have been given the task to design a list of APIs to be
I have been given the painful task of writing a C# application to sync
I have been given the task of creating a sql database and creating a
I have been given the task of bringing three legacy systems together into one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.