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;
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]