I am retrieving message from the Inbox as follows: (MS Article ID 310258: How to use the Microsoft Outlook Object Library to retrieve a message from the Inbox by using Visual C# .NET)
Try
Dim oApp As Outlook.Application = New Outlook.Application
Dim oNS As Outlook.NameSpace = oApp.GetNamespace("mapi")
oNS.Logon(Missing.Value, Missing.Value, False, True)
Dim oInbox As Outlook.MAPIFolder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
Dim oItems As Outlook.Items = oInbox.Items
Dim oMsg As Outlook.MailItem = CType(oItems.GetFirst, Outlook.MailItem)
Console.WriteLine(oMsg.Subject)
Console.WriteLine(oMsg.SenderName)
Console.WriteLine(oMsg.ReceivedTime)
Console.WriteLine(oMsg.Body)
Dim AttachCnt As Integer = oMsg.Attachments.Count
oMsg.Attachments.Item(1).SaveAsFile("C:\Temp\Attach")
Console.WriteLine("Attachments: " + AttachCnt.ToString)
If AttachCnt > 0 Then
Dim i As Integer = 1
While i <= AttachCnt
Console.WriteLine(i.ToString + "-" + oMsg.Attachments(i).DisplayName)
System.Math.Min(System.Threading.Interlocked.Increment(i), i - 1)
End While
End If
oMsg.Display(True)
oNS.Logoff()
oMsg = Nothing
oItems = Nothing
oInbox = Nothing
oNS = Nothing
oApp = Nothing
Catch e As Exception
Console.WriteLine("{0} Exception caught: ", e)
End Try
When I run the sample I get a message box saying "A program is trying to access e-mail addresses you have stored in Outlook. Do you want to allow this If this unexpected, it may b a virus and you should choose "No"." How can I turn off this message box
Secondly, how can I save the attached rather than opening the email
Is there any document that describes methods and properties exposed by Microsoft Outlook Object Library I could not find any link. Thanks

Microsoft Outlook 11.0 Object Library
Mike in Tucson
I assume you're doing something like:
For i as Integer = 1 to oMsg.Attachments.Count
oMsg.Attachments.Item(i).SaveAsFile("C:\Temp\Attach\" & oMsg.Attachments.Item(i).FileName)
Next
Note however that:
Dim enm As IEnumerator = oMsg.Attachments.GetEnumerator
While enm.MoveNext
Dim a As Outlook.Attachment = enm.Current
a.SaveAsFile("c:\temp\attach\" & a.FileName)
End While
Is much better than a For-Next loop. And you could even trim that down to simply:
enm.Current.SaveAsFile("c:\temp\attach\" & enm.Current.FileName)
or, if you want the intellisense:
cType(enm.Current, Oulook.Attachment).SaveAsFile("c:\temp\attach\" & cType(enm.Current, Outlook.Attachment).FileName)
As long as your using the FileName of the current attachment you are saving, you should not have an overwrite issue unless you've previously saved an attachment with the same name. If you find you are repeatedly saving attachments (from different emails) with the same filename, you should then first create a new subdirectory in c:\temp\attach. You could use info parsed from the email to name the subdirectory, or simply count the number of directories in c:\temp\attach and add one named whatever your folder count was.
eric_1234
Modify your code as follows:
oMsg.Attachments.Item(1).SaveAsFile("C:\Temp\Attach\" & oMsg.Attachments.Item(1).FileName)
This will include a file name to save the attachment as. Make sure the path "C:\Temp\Attach" exists.
TRexian
TS1985
Ex:
' Loop each unread message.
Dim enm As IEnumerator = oItems.GetEnumerator
While enm.MoveNext()
'Test to make sure item is a mail item
'and not a meeting request.
If enm.Current.MessageClass = sClassComp Then
oMsg = enm.Current
Console.WriteLine("Subject: " + oMsg.Subject)
'Mark the email as read.
oMsg.UnRead = False
End If
End While
Hope that helps!
Larry Charlton
Mr Sharpie
' Loop each unread message.
Dim i As Integer
For i = 1 To oItems.Count
'Test to make sure item is a mail item
'and not a meeting request.
If oItems.Item(i).MessageClass = sClassComp Then
oMsg = oItems.Item(i)
Console.WriteLine("Subject: " + oMsg.Subject)
'Mark the email as read.
oMsg.UnRead = False
End If
Next
Federico Silberberg
It came up with an error saying "Cannot save the attachment. You don't have appropriate permission to perform this operation." Following are the details:
System.Runtime.InteropServices.COMException (0xD4970005): Cannot save the attach
ment. You don't have appropriate permission to perform this operation.
at Outlook.Attachment.SaveAsFile(String Path)
at RetrieveMessageFromInbox.Module1.Main() in C:\Documents and Settings\pbang
a\My Documents\Visual Studio Projects\RetrieveMessageFromInbox\Module1.vb:line 4
7 Exception caught:
Press any key to continue
sbales
You've saved the first attachment with oMsg.Attachments.Item(1).SaveAsFile. You should put that line in your attachment loop and use .Item(i). Also specify file names...
Just omit the oMsg.Display(True) to keep from opening the email.
Curve
Thanks