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