locked
Read email Attachment from specific folder in outlook RRS feed

  • Question

  • User972064672 posted

    Dear Programmers, Is anybody worked about extracting email attachment from outlook email and read that attachment. i mean my goal is to first to read email attachment from outlook email and then read attachment file like txt file and save that txt file information. if anybody worked with this kindly give me direction. I found this below link https://www.matthewproctor.com/extracting-attachments-from-outlook-using-c-sharp

    Thursday, June 25, 2020 3:08 PM

Answers

  • User288213138 posted

    Hi Rakib Ahmed,

    i mean my goal is to first to read email attachment from outlook email and then read attachment file like txt file and save that txt file information.

    Here a demo about how to save e-mail attachments to a specified folder when the mail is received in the inbox.

    This example works only if you add a folder named TestFileSave at the root of the C directory.

    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 inBox = this.Application.ActiveExplorer()
            .Session.GetDefaultFolder(Outlook
            .OlDefaultFolders.olFolderInbox);
        Outlook.Items inBoxItems = inBox.Items;
        Outlook.MailItem newEmail = null;
        inBoxItems = inBoxItems.Restrict("[Unread] = true");
        try
        {
            foreach (object collectionItem in inBoxItems)
            {
                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");
            }
        }
    }

    More information about how to save attachments from Outlook email items you can refer to this link:

    https://docs.microsoft.com/en-us/visualstudio/vsto/how-to-programmatically-save-attachments-from-outlook-e-mail-items?view=vs-2019

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 26, 2020 2:37 AM

All replies

  • User-943250815 posted

    I think your best choice is ti use Mailkit/Mimekit to work with mails, you can get it on Nuget.
    For how to check and extract attachments http://www.mimekit.net/docs/html/Frequently-Asked-Questions.htm

    Thursday, June 25, 2020 3:38 PM
  • User288213138 posted

    Hi Rakib Ahmed,

    i mean my goal is to first to read email attachment from outlook email and then read attachment file like txt file and save that txt file information.

    Here a demo about how to save e-mail attachments to a specified folder when the mail is received in the inbox.

    This example works only if you add a folder named TestFileSave at the root of the C directory.

    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 inBox = this.Application.ActiveExplorer()
            .Session.GetDefaultFolder(Outlook
            .OlDefaultFolders.olFolderInbox);
        Outlook.Items inBoxItems = inBox.Items;
        Outlook.MailItem newEmail = null;
        inBoxItems = inBoxItems.Restrict("[Unread] = true");
        try
        {
            foreach (object collectionItem in inBoxItems)
            {
                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");
            }
        }
    }

    More information about how to save attachments from Outlook email items you can refer to this link:

    https://docs.microsoft.com/en-us/visualstudio/vsto/how-to-programmatically-save-attachments-from-outlook-e-mail-items?view=vs-2019

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 26, 2020 2:37 AM