0

So I have my public folders returning a count of the items within a specific public folder which works for me. These are saved as .msg which contain pure xml data.

I want to be able to read the xml data from the outlook.msg and store it in either temp table or memory so I can insert to sql server?

Here is what I have so far:

 olApp = new Outlook.Application();
            olNs = (Outlook._NameSpace) olApp.GetNamespace("MAPI");
            oFolder = olNs.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
            oExp = oFolder.GetExplorer(false);
            olNs.Logon(Missing.Value, Missing.Value, false, true);

            //pick a MAPI folder
            Outlook.MAPIFolder publicFolders = (Outlook.MAPIFolder) olNs.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olPublicFoldersAllPublicFolders).Folders["Food"];

            try
            {
                foreach (Outlook.MAPIFolder f in publicFolders.Folders)
                {
                    if (f.Name == "Burgers")
                        textBlockCount = f.Items.Count.ToString();
                }
                return;
            }
            catch (System.Exception e)
            {
                Log.Logging(e, "Mailbox exception");
            }

This is a redacted xml file replacing any customer details with dummy data:

<?xml version="1.0"?>
<XMLCONTAINER version="1.0" incCount="3">
  <APPLICATION id="APPLICATION_NODE_1" title="" name="55038170812101005FOODITEMS Foodpayment" application="Excel" appfile="%TURTLE%\xmlsForms\RDBxmls.xla" command="openRDBeDesigns">
    <MAINDATA id="CONTAINER_NODE_2" title=""/>
    <TURTLE.FORM id="CONTAINER_NODE_3" title="" table="tblMain" database="\\c\g\FDR1\FOODS\Data\Link\FOODpayments.mdb">
      <FIELD fieldName="EID">1234567</FIELD>
      <FIELD fieldName="DateTime">21/02/2019 14:50:17</FIELD>
      <FIELD fieldName="DateOfLetter">21/02/19</FIELD>
      <FIELD fieldName="Name">Mr Joe Blogs</FIELD>
      <FIELD fieldName="CUSTID">RR123456R</FIELD>
      <FIELD fieldName="paymentAmount">1144.80</FIELD>
      <FIELD fieldName="AmountForPreviousYears">1144.80</FIELD>
      <FIELD fieldName="NoPurple">i&amp;e not comp'd - so he can't pay</FIELD>
      <FIELD childID="CONTAINER_NODE_3_field59option1" fieldName="ComeBackReason">Sourcing2</FIELD>
      <FIELD fieldName="Telephone">012345678</FIELD>
      <FIELD fieldName="AcceptPhoneNumber">TRUE</FIELD>
      <FIELD childID="CONTAINER_NODE_3_field7option4" fieldName="CustomerType">4</FIELD>
      <FIELD fieldName="Target4">FALSE</FIELD>
      <FIELD fieldName="Card">FALSE</FIELD>
      <FIELD fieldName="ContractSettlement">FALSE</FIELD>
      <FIELD fieldName="UPYear">512</FIELD>
      <FIELD fieldName="AllocatedTo">1234567</FIELD>
      <FIELD fieldName="LateCase">FALSE</FIELD>
    </TURTLE.FORM>
  </APPLICATION>
</XMLCONTAINER>

So I have been updating this and found out that PostItem works and MailItem doesn't. I am now able to show the message body, so how do I extract those FIELDS to a list or something? I put in the string mystring but does'nt look like it saves to it:

for (int i = 1; i < publicFolder.Items.Count; i++)
                {
                    item = (PostItem)publicFolder.Items[i];
                    if (item != null)
                    {
                        MessageBox.Show(item.Subject, "Subject");
                        MessageBox.Show(item.BodyFormat.ToString(), "Item: {0}");
                        MessageBox.Show(item.Body, "Body");

                        string mystring = item.Body;



                    }
                    else
                    {
                        MessageBox.Show("No mail items found");
                    }
                }
ChrisR
  • 81
  • 9
  • Usually attachments to mail message are HTML not xml. You need to parse using an HTML reader. – jdweng Feb 21 '19 at 14:23
  • @jdweng Sorry, you are correct yes its not an attachment. It just displays the xml data in the email body. Is it possible to parse from this to a .csv file or would I need to just use .html. All I'm really looking for is the output in a file that I can upload to SQL Server? – ChrisR Feb 21 '19 at 15:48
  • So what are you having problem with? Reading a standalone MSG file? Or accessing messages in a particular Outlook folder? – Dmitry Streblechenko Feb 21 '19 at 17:32
  • If it is in the body, then the body tag is HTML. The innertext of the body cold be XML and can be converted to csv. Need to see sample of the xml. – jdweng Feb 21 '19 at 17:56
  • @jdweng sure I have uploaded screenshot (redacted with dummy info) to imgur here:https://imgur.com/a/epCLd22 – ChrisR Feb 21 '19 at 19:14
  • @DmitryStreblechenko reading a standalone msg file is my first issue. From the dummy xml attachment https://imgur.com/a/epCLd22 which is what it looks like the split is using for each of the values, I just need to read that and store that, I'll see how to upload it to SQL Server tblMain later. – ChrisR Feb 21 '19 at 19:18
  • Possible duplicate of [How do I read and parse an XML file in C#?](https://stackoverflow.com/questions/642293/how-do-i-read-and-parse-an-xml-file-in-c) – Ken White Feb 22 '19 at 02:07

2 Answers2

0

Use olNs.OpenSharedItem to open a standalone MSG file.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
0

I parse some of the xml. Did not know what you needed. I used the Net Library XML Linq.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            string xml = File.ReadAllText(FILENAME);
            XDocument doc = XDocument.Parse(xml);

            var results = doc.Descendants("FIELD").Select(x => new {
                fieldName = (string)x.Attribute("fieldName"),
                childID = (string)x.Attribute("childID"),
                value = (string)x
            }).ToList();
        }
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20