我想从不是我发送的特定文件夹中下载附件。
我需要从该文件夹中下载今天日期的最新未读邮件。
那么我该怎么做呢?
这是我的代码:
Dim app As Microsoft.Office.Interop.Outlook.Application = Nothing
Dim ns As Microsoft.Office.Interop.Outlook._NameSpace = Nothing
Dim inboxFolder As Microsoft.Office.Interop.Outlook.MAPIFolder = Nothing
Dim subFolder As Microsoft.Office.Interop.Outlook.MAPIFolder = Nothing
Dim destinationDirectory As String = "C:\UnreadMails"
If Not Directory.Exists(destinationDirectory) Then
Directory.CreateDirectory(destinationDirectory)
End If
Try
app = New Microsoft.Office.Interop.Outlook.Application()
ns = app.GetNamespace("MAPI")
ns.Logon(Nothing, Nothing, False, False)
inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox)
subFolder = inboxFolder.Folders("UnreadMails") 'folder.Folders[1]; also works
Console.WriteLine("Folder Name: {0}, EntryId: {1}", subFolder.Name, subFolder.EntryID)
Console.WriteLine("Num Items: {0}", subFolder.Items.Count.ToString())
For i As Integer = 1 To subFolder.Items.Count
Dim item As Microsoft.Office.Interop.Outlook.MailItem = CType(subFolder.Items(i), Microsoft.Office.Interop.Outlook.MailItem)
Dim filePath As String = Path.Combine(destinationDirectory, item.Attachments(i).FileName)
item.Attachments(i).SaveAsFile(filePath)
Next i
Catch ex As System.Runtime.InteropServices.COMException
Console.WriteLine(ex.ToString())
Finally
ns = Nothing
app = Nothing
inboxFolder = Nothing
End Try