title: Importing EML (MIME) files in Outlook programmatically
body: <p>I have a directory full of email files I want to import into Outlook.</p>
  
  <p>These files <em>should be</em>, AFAIK, in RFC822 format. Opening them with Notepad++ shows the following and more plaintext</p>
  
  <pre><code>Return-Path: XXX
  Received: XXX
  Authentication-Results: XXX
  X-Message-Status: XXX
  X-SID-PRA: XXX
  X-SID-Result: XXX
  X-AUTH-Result: XXX
  X-Message-Delivery: XXX
  X-Message-Info: XXXX
  Received: from XXX
  Received: from XXX
  DomainKey-Signature: XXX
  </code></pre>
  
  <p>So I guess they are in plain old format. Now I would like to import them into a PST file in order to index and make them searcheable. I have very poor knowledge of Outlook Interop.</p>
  
  <p>Renaming the untitled files to <code>.eml</code> works, I can open them with Outlook 2010. Dragging them into Outlook windows half works: emails are not previewed but I can double-click them. Not all emails get imported (I'm talking about 3000+ messages), and Outlook doesn't ingore damaged files, so either all or none.</p>
  
  <p>I was exploring Outlook interop.</p>
  
  <p>The basic idea is the following: for each text file, import that as email into a given folder.</p>
  
  <p>I have tried to write, but not yet to test, the following code</p>
  
  <pre><code>Application outlook = GetApplicationObject();
  
  outlook.Session.AddStore(pstPath);
  
  Store theStore = outlook.Session.Stores.Cast&lt;Store&gt;().FirstOrDefault(store =&gt; store.FilePath == pstPath);
  
  if (theStore == null) throw new Exception();
  
  try
  {
      string[] files = Directory.GetFiles(dataDirectory, "*", SearchOption.AllDirectories);
  
      Folder folder = (Folder)theStore.GetRootFolder();
  
      foreach (string file in files.Where(file =&gt; file.EndsWith(".eml")))
      {
          //What?
  
  
      }
  }
  finally
  {
      //Dispose??
  }
  </code></pre>
  
  <p>Where <code>GetApplicationObject</code> is copied from another SO answer.</p>
  
  <p>Question: is it possible to create a <code>MailItem</code> based on the contents of a file? Or do I have to use an email reader and then create the MailItem field by field? Like the following I was writing...</p>
  
  <pre><code>IMail mail = builder.CreateFromEmlFile(file);
  
  MailItem outlookMail = outlook.CreateItem(OlItemType.olMailItem);
  outlookMail.Body = mail.GetBodyAsText();
  outlookMail.CC = string.Join("; ", mail.Cc.Select(x =&gt; x.Render()));
  outlookMail.HTMLBody = mail.GetBodyAsHtml();
  
  if (MimeImportance.High.Equals(mail.Importance))
      outlookMail.Importance = OlImportance.olImportanceHigh;
  else if (MimeImportance.Low.Equals(mail.Importance))
      outlookMail.Importance = OlImportance.olImportanceLow;
  else
      outlookMail.Importance = OlImportance.olImportanceNormal;
  
  outlookMail.Sender=outlook.CreateItem()
  </code></pre>
  
  <p>Library I'm trying is from <a href="http://www.limilabs.com/mail" rel="nofollow">http://www.limilabs.com/mail</a></p>