Click here to Skip to main content
15,884,838 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
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]
Posted
Updated 29-Jun-10 4:59am
v4
Comments
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!

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

C#
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

C#
foreach (oItem in objMAIPFolder.Items)
 
Share this answer
 
v3
Your are playing an empty object within the foreach loop. The loop will be the following:

C#
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[^]
(ii) http://www.codeproject.com/Tips/54556/Get-Outlook-07-email-attachments-entry-ID-using-Cs.aspx[^]
 
Share this answer
 
Comments
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.
[no name] 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


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