Click here to Skip to main content
11,926,429 members (59,463 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#3.0 C# VB VB.NET Outlook
Hi. I need to import Outlook body contents and attachments for classification.
To import the body contents and attachments I use the following method:

private void SaveAllMAPIFolderItems()
            Outlook.Application objOutlook = default(Outlook.Application);
            Outlook.NameSpace objNameSpace = default(Outlook.NameSpace);
            Outlook.MAPIFolder objMAPIFolder = default(Outlook.MAPIFolder);
            Outlook.NameSpace oNS = default(Outlook.NameSpace);
            Outlook.MAPIFolder oInbox = default(Outlook.MAPIFolder);
            Outlook.MailItem oItem = null;
            //Outlook.Attachment oAtt = default(Outlook.Attachment);

            string sSubject = null;
            string sType = null;
            string sSent = null;
            string sUniqueFileName = null;
            string sUniqueAttFolderName = null;
            objOutlook = new Outlook.Application();
            oNS = objOutlook.GetNamespace("MAPI");
            objMAPIFolder = (Outlook.MAPIFolder)objOutlook.Session.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
                foreach (Outlook.MailItem OItem_loopVariable in objMAPIFolder.Items)
                    oItem = OItem_loopVariable;
                    sSubject = oItem.Subject;
                    sUniqueAttFolderName = conBasePath + "_bak_" + System.Guid.NewGuid().ToString();
                    oItem.SaveAs(sUniqueFileName + ".txt", Outlook.OlSaveAsType.olTXT);
                    if (oItem.Attachments.Count > 0)
                        sUniqueAttFolderName = sUniqueFileName + "_Folder";
                        foreach (Outlook.Attachment oAtt in oItem.Attachments)
                            sUniqueFileName = sUniqueAttFolderName + "\" + oAtt.FileName;
            catch (System.Exception ex)

which I converted from the following VB.NET code:

Dim objOutlook As Outlook.Application
       Dim objNamespace As Outlook.NameSpace
       Dim objMAPIFolder As Outlook.MAPIFolder
       Dim oNS As Outlook.NameSpace
       Dim oInbox As Outlook.MAPIFolder
       Dim oItem As Object   ' as Outlook.Mailitem
       Dim oAtt As Outlook.Attachment
       Dim sSubject As String
       Dim sType As String
       Dim sSent As String
       Dim sUniqueFileName As String
       Dim sUniqueAttFolderName As String
       objOutlook = New Outlook.Application
       oNS = objOutlook.GetNamespace("MAPI")
       oNS.Logon(ShowDialog:=True, NewSession:=True)
       objMAPIFolder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
       'objMAPIFolder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail)

           For Each oItem In objMAPIFolder.Items
               sSubject = oItem.Subject
               'Dim someString As String = sSubject
               'Dim index As Integer = someString.IndexOf(":")
               'Dim replaced As String
               'Dim character As Char
               'For Each character In someString
               '    If character = ":" Then
               '        replaced = someString.Replace(":", ")
               '    End If

               'sUniqueFileName = conBasePath + replaced + "_bak_" + System.Guid.NewGuid().ToString
               sUniqueFileName = conBasePath + "bak_" + System.Guid.NewGuid().ToString
               oItem.SaveAs(sUniqueFileName + ".txt", Outlook.OlSaveAsType.olTXT)
               If (oItem.Attachments.Count > 0) Then
                   sUniqueAttFolderName = sUniqueFileName + "_Folder"
                   For Each oAtt In oItem.Attachments
                       sUniqueFileName = sUniqueAttFolderName + " " + oAtt.FileName
                   Next oAtt
               End If
               'End If
           Next oItem
       Catch ex As System.Exception
       End Try
       MsgBox("Processed", MsgBoxStyle.Information)

but I can't get the same answer as with VB.Net. The target folder is empty and I can't import the contents. Please help.

[Modified: removed the code tags within the pre tags and moved the first bit of code down to get the full width of the screen]
Posted 26-Jun-10 8:34am
Edited 29-Jun-10 5:59am
William Winner 29-Jun-10 10:54am
Maybe you didn't read what I wrote when I modified your OP. Don't use both pre and code tags at the same time! (ie <pre><code>). Code tags are only for formatting code when in the middle of other text!
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

So I just tested out your code, and here's your problem:

oItem.SaveAs(sUniqueFileName + ".txt", Microsoft.Office.Interop.Outlook.OlSaveAsType.olTXT);

sUniqueFileName is never set to anything besides null. Not only that, but you didn't specify which folder to save it to.

Also, you don't need to use the OItem_loopVariable.

You could have just written

foreach (oItem in objMAIPFolder.Items)
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Your are playing an empty object within the foreach loop. The loop will be the following:

foreach (Outlook.MailItem objMail 
        in objMAPIFolder.Items)
         sSubject = objMail.Subject;
         // Do some more         

You can also try the following link:
(i) Outlook 2007 Add-in Using Microsoft Visual C#.NET[^]
William Winner 29-Jun-10 12:02pm
I'm just kind of curious...what was your point?

What he was doing was the same thing, except with an extra step.

Besides that, it doesn't solve the problem.
Md. Marufuzzaman 29-Jun-10 12:08pm
It will give some ideas to resolve this issues.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web04 | 2.8.151126.1 | Last Updated 29 Jun 2010
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100