title: Outlook HTML Mail - changing linked items to embedded
body: <p>I'm attempting to send HTML formatted emails using C# 3 via <code>Outlook.MailItem</code></p>
  
  <pre><code>Outlook.MailItem objMail = (Outlook.MailItem)olkApp.CreateItem(Outlook.OlItemType.olMailItem);
  objMail.To = to;
  objMail.Subject = subject;
  objMail.HTMLBody = htmlBody;
  </code></pre>
  
  <p>The email is generated externally by saving from an RTF control (TX Text Control), which yields HTML with links to images stored in a <code>&lt;&lt;FileName&gt;&gt;</code>_files subdirectory. Example: </p>
  
  <p><code>&lt;img border="0" src="file:///C:/Documents%20and%20Settings/ItsMe/Local%20Settings/Temp/2/zbt4dmvs_Images/zbt4dmvs_1.png" width="94" height="94" alt="[image]"&gt;</code></p>
  
  <p>Sending the email this way generates a mail with broken links.  </p>
  
  <p>Using Outlook 2007 as the email client with Word as the email editor, switching to RTF (Options tab, Format tab group) preserves the layout <em>and</em> inlines the images.</p>
  
  <p>Programmatically doing this via:</p>
  
  <pre><code>var oldFormat = objMail.BodyFormat;
  objMail.BodyFormat = Outlook.OlBodyFormat.olFormatRichText;
  objMail.BodyFormat = oldFormat;
  </code></pre>
  
  <p>loses the formatting and mangles the images (the image becomes a <code>[image]</code> link marker on screen which is clickable but no longer shows the image).  This isn't a surprise given that the documentation for <a href="http://msdn.microsoft.com/en-us/library/bb207134.aspx" rel="nofollow noreferrer">MailItem.BodyFormat Property</a> says "<em>All text formatting will be lost when the <code>BodyFormat</code> property is switched from RTF to HTML and vice-versa</em>".</p>
  
  <p>Sadly there doesnt seem to be an easy way to change the <code>Type</code> of each <code>Attachment</code> in the <code>MailItem.Attachements</code> to <code>OlAttachmentType.olByValue</code>, as it's a read-only property that's set when you create the <code>Attachment</code>.</p>
  
  <p>An approach that comes to mind is to walk the HTML, replacing the <code><code>&lt;img&gt;</code></code> tags with markers and programatically walking the <code>MailItem</code> text, inserting an <code>Outlook.Attachment</code> of <code>Type</code> <code>OlAttachmentType.olByValue</code>.</p>
  
  <p>Another option is to convert the <code><code>&lt;img&gt;</code></code> links to use <code><code>src="cid:uniqueIdN"</code></code> and add the images as attachments with the referenced identities.</p>
  
  <p>So, to the question... Is there a way to get the linked images converted to embedded images, ideally without getting into third party tools like Redemption?  Converting to RTF happens to yield the outcome, but doing it that way is by no means a pre-requisite, and obviously may lose fidelity - I Just Want It to Just Work :D  Neither of my existing ideas sound Clean to me.</p>