Click here to Skip to main content
15,885,953 members

Outlook - Importing body contents and attachments

Kasunmit asked:

Open original thread
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);

            try
            {
                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";
                        Directory.CreateDirectory(sUniqueAttFolderName);

                        foreach (Outlook.Attachment oAtt in oItem.Attachments)
                        {
                            sUniqueFileName = sUniqueAttFolderName + "\" + oAtt.FileName;
                            oAtt.SaveAsFile(sUniqueFileName);
                        }
                    }
                    
                }

            }
            catch (System.Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }

            MessageBox.Show("Processred");

        }


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)


       Try
           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
               'Next


               '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"
                   MkDir(sUniqueAttFolderName)

                   For Each oAtt In oItem.Attachments
                       sUniqueFileName = sUniqueAttFolderName + " " + oAtt.FileName
                       oAtt.SaveAsFile(sUniqueFileName)
                   Next oAtt

               End If

               'End If

           Next oItem

       Catch ex As System.Exception
           MessageBox.Show(ex.ToString)
       End Try

       oNS.Logoff()

       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]
Tags: C# (C# 3.0), Visual Basic, Outlook

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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