Click here to Skip to main content
15,878,430 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a requirement to get all email messages from a certain folder on our exchange server, save all attachments and the message itself to the file system.
I can save the attachments, but I'm having an issue with the message itself.

Our exchange server is 2010 SP2, so I am using EWS to connect, get the messages and save the attachments. These files are going into a document repository system. This system can display .msg files, so I need the emails to save as .msg.

From what I've found so far, I can only save the messages as .eml files.

I thought I could work with this by converting the .eml to .msg, but not finding a way to do this yet.

Then I thought I could get the messages from a console application using Microsoft.Office.Interop.Outlook after getting some kind of Id from the EWS application. I can't find id's in both that are the same.

Does anyone have any suggestions? I have to get the message files as .msg for our document repository system.

thank you in advance,


Exchange Web Services .NET offers feature to save messages and other items as Outlook .msg files. It works with ANSI and Unicode .msg files.
Share this answer
Tisha Shaffer 21-Aug-13 9:06am    
My company will not allow me to use third party software. That is why I'm stuck. I'm sorry that I didn't mention that in my original question.
I have decided not to use EWS, instead I am using Microsoft.Office.Outlook.Interop.

I have the following code working well on my development PC:

log = New cLog()
        Dim AppKeys As IDictionary = ConfigurationManager.GetSection("AppKeys")
        Dim app As New Microsoft.Office.Interop.Outlook.Application()
        Dim box As String
        log.Write("Starting to retrieve mail.")
            box = AppKeys("mailbox")
            tempLoc = Environment.CurrentDirectory & "\" & ConfigurationManager.AppSettings("TempLoc")
            Dim ns As Microsoft.Office.Interop.Outlook.NameSpace = app.Session
            log.Write("Namespace is: " & ns.CurrentProfileName)
            Dim recipient As Recipient = ns.CreateRecipient(box)
            log.Write("recipient is: " & recipient.Name)
            log.Write("recipient resolved")
            Dim inbox As Folder = ns.GetSharedDefaultFolder(recipient, OlDefaultFolders.olFolderInbox)
            log.Write("Got inbox")
            Dim subject As String
            Dim mailCount As Int32 = 0
            log.Write("There are " + inbox.Items.Count.ToString() + " Emails in " + box)
            For Each mm As MailItem In inbox.Items
                If Not subject Is Nothing Then
                    For Each attach As Attachment In mm.Attachments
                        attach.SaveAsFile(tempLoc & filename & attach.FileName.Substring(attach.FileName.Length - 4, 4))
                        ''Save email message as msg and create idx file for it
                        filename = filename.Substring(0, filename.Length - 2) & "_" & count + 1
                        mm.SaveAs(tempLoc & filename & ".msg", OlSaveAsType.olMSG)
                End If
        Catch ex As System.Exception
            log.Write("Excpetion getting mail: " & ex.Message)
        End Try

However, when I move the compiled code to a production PC (with Office 2010 installed, same as my development PC) I now get the following error on this line, "ns.GetSharedDefaultFolder":

The operation failed because of a registry or installation problem. Restart Outlook and try again. If the problem persists, reinstall.

I have a seperate application running on the same production PC that uses Microsoft.Office.Outlook.Interop to convert .MSG files to .PDF and that runs without issue.

The only difference that I can think of is that my development PC is Windows XP and the production PC is Windows 7.

Any thoughts would be greatly appreciated!

Share this answer
Kenneth Haugland 14-Oct-13 12:43pm    
You should mark your question as solved then.
Tisha Shaffer 14-Oct-13 13:46pm    
I will post my current issue under a new question then.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900