Microsoft Outlook 11.0 Object Library

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 


Answer this question

Microsoft Outlook 11.0 Object Library

  • Mike in Tucson

    I didn't explicitly mention it in the last post, but you do need to be looping through the attachments collection and saving each one, so your code should use "i" instead of "1".  "1" was just the correct value for the original posted code.

    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

    That is probably because you still aren't specifing a file name in the SaveAsFile method...

    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

    thanks rkimble
  • TS1985

    I'm not sure why you're getting that exception, but again, if you use an Enumerator rather than a For-Next loop, you won't be able to go out of bounds.

    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

    No problem.  Good luck!
  • Mr Sharpie

    Why am i getting exception "Array index out of bounds" when i try to mark emails as read. code is as follows

                ' 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

    Hi rkimble

    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 can't get around that warning... the user has to agree (or revert to an older edition of Outlook - I think outlook 2000 w/o any SP or HF doesn't ask this).  If this is to be a fully automated app (no user), you'll need a POP3 control rather than Outlook.

    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 it worked. Is there anyway to prevent the file from being replaced. SaveAsFile method always overwrites the attached. Is there anyway to prompt user before overwriting.

    Thanks

  • Microsoft Outlook 11.0 Object Library