Read and save Attachment from Outlook using C# and VB.Net in ASP.Net

Last Reply 11 days ago By dharmendr

Posted 12 days ago

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

You are viewing reply posted by: dharmendr 11 days ago.
Posted 11 days ago

Hi rakibxl,

Refer below code.

Namespaces

C#

using Outlook = Microsoft.Office.Interop.Outlook;

VB.Net 

Imports Outlook = Microsoft.Office.Interop.Outlook

Code

C#

public void EnumerateFolders(Outlook.Folder folder)
{
    Outlook.Folders childFolders = folder.Folders;
    if (childFolders.Count > 0)
    {
        foreach (Outlook.Folder childFolder in childFolders)
        {
            if (childFolder.FolderPath.Contains("Inbox"))
            {
                IterateMessages(childFolder, Server.MapPath("~/Attachments/"));
                EnumerateFolders(childFolder);
            }
        }
    }
}

public void IterateMessages(Outlook.Folder folder, string pathToSaveFile)
{
    var fi = folder.Items;
    if (fi != null)
    {
        foreach (Object item in fi)
        {
            Outlook.MailItem mi = (Outlook.MailItem)item;
            var attachments = mi.Attachments;
            if (attachments.Count != 0)
            {
                for (int i = 1; i <= mi.Attachments.Count; i++)
                {
                    string[] extensionsArray = { ".txt" };
                    if (extensionsArray.Any(mi.Attachments[i].FileName.Contains))
                    {
                        mi.Attachments[i].SaveAsFile(pathToSaveFile + mi.Attachments[i].FileName);
                    }
                }
            }
        }
    }
}

VB.Net

Public Sub EnumerateFolders(ByVal folder As Outlook.Folder)
    Dim childFolders As Outlook.Folders = folder.Folders
    If childFolders.Count > 0 Then
        For Each childFolder As Outlook.Folder In childFolders
            If childFolder.FolderPath.Contains("Inbox") Then
                IterateMessages(childFolder, Server.MapPath("~/Attachments/"))
                EnumerateFolders(childFolder)
            End If
        Next
    End If
End Sub

Public Sub IterateMessages(ByVal folder As Outlook.Folder, ByVal pathToSaveFile As String)
    Dim fi = folder.Items
    If fi IsNot Nothing Then
        For Each item As Object In fi
            Dim mi As Outlook.MailItem = CType(item, Outlook.MailItem)
            Dim attachments = mi.Attachments
            If attachments.Count <> 0 Then
                For i As Integer = 1 To mi.Attachments.Count
                    Dim extensionsArray As String() = {".txt"}
                    If System.IO.Path.GetExtension(mi.Attachments(i).FileName).ToLower() = ".txt" Then
                        mi.Attachments(i).SaveAsFile(pathToSaveFile & mi.Attachments(i).FileName)
                    End If
                Next
            End If
        Next
    End If
End Sub