Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6870755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:43:19+00:00 2026-05-27T03:43:19+00:00

Working in C# with the EWS Managed API, we’re having trouble efficiently retrieving the

  • 0

Working in C# with the EWS Managed API, we’re having trouble efficiently retrieving the images stored as inline attachments.

The endpoint is to show an email with inline images as a fully formed html page in a panel. The code we currently us:

     string sHTMLCOntent = item.Body;

      FileAttachment[] attachments = null;

      if (item.Attachments.Count != 0)
      {
        attachments = new FileAttachment[item.Attachments.Count];
        for (int i = 0; i < item.Attachments.Count; i++)
        {
          string sType = item.Attachments[i].ContentType.ToLower();
          if (sType.Contains("image"))
          {
            attachments[i] = (FileAttachment)item.Attachments[i];
            string sID = attachments[i].ContentId;
            sType = sType.Replace("image/", "");
            string sFilename = sID + "." + sType;
            string sPathPlusFilename = Directory.GetCurrentDirectory() + "\\" + sFilename;
            attachments[i].Load(sFilename);
            string oldString = "cid:" + sID;
            sHTMLCOntent = sHTMLCOntent.Replace(oldString, sPathPlusFilename);
          }
        }
      }

(sourced: http://social.technet.microsoft.com/Forums/en-US/exchangesvrdevelopment/thread/ad10283a-ea04-4b15-b20a-40cbd9c95b57)

.. this is not very efficient though and is slowing down the responsiveness of our web app. Does anyone have a better solution for this problem? We are using Exchange 2007 SP1, so the IsInline property wont work as its Exchange 2010 only.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:43:20+00:00Added an answer on May 27, 2026 at 3:43 am

    I build an index of your “cid:”s first:

    private const string CidPattern = "cid:";
    
    private static HashSet<int> BuildCidIndex(string html)
    {
        var index = new HashSet<int>();
        var pos = html.IndexOf(CidPattern, 0);
        while (pos > 0)
        {
            var start = pos + CidPattern.Length;
            index.Add(start);
            pos = html.IndexOf(CidPattern, start);
        }
        return index;
    }       
    

    Then you need a replace function that replaces the cids based on your index

    private static void AdjustIndex(HashSet<int> index, int oldPos, int byHowMuch)
    {
        var oldIndex = new List<int>(index);
        index.Clear();
        foreach (var pos in oldIndex)
        {
            if (pos < oldPos)
                index.Add(pos);
            else
                index.Add(pos + byHowMuch);
        }           
    }
    
    private static bool ReplaceCid(HashSet<int> index, ref string html, string cid, string path)
    {
        var posToRemove = -1;
        foreach (var pos in index)
        {
            if (pos + cid.Length < html.Length && html.Substring(pos, cid.Length) == cid)
            {
                var sb = new StringBuilder();
                sb.Append(html.Substring(0, pos-CidPattern.Length));
                sb.Append(path);
                sb.Append(html.Substring(pos + cid.Length));
                html = sb.ToString();
    
                posToRemove = pos;
                break;
            }
        }
    
        if (posToRemove < 0)
            return false;
    
        index.Remove(posToRemove);
        AdjustIndex(index, posToRemove, path.Length - (CidPattern.Length + cid.Length));
    
        return true;
    }
    

    so now, you can check your attachments

    FileAttachment[] attachments = null;
    var index = BuildCidIndex(sHTMLCOntent);
    if (index.Count > 0 && item.Attachments.Count > 0)
    {
        var basePath = Directory.GetCurrentDirectory();
    
        attachments = new FileAttachment[item.Attachments.Count];
        for (var i = 0; i < item.Attachments.Count; ++i)
        {
          var type = item.Attachments[i].ContentType.ToLower();
          if (!type.StartsWith("image/")) continue;                    
          type = type.Replace("image/", "");
    
          var attachment = (FileAttachment)item.Attachments[i];
          var cid = attachment.ContentId;
          var filename = cid + "." + type;
          var path = Path.Combine(basePath, filename);
          if(ReplaceCid(index, ref sHTMLCOntent, cid, path))
          {
             // only load images when they have been found          
             attachment.Load(path);
             attachments[i] = attachment;
          }
       }
    }
    

    Additional to that: instead of calling attachment.Load right away, and pass the path to the image directly, you could link to another script, where you pass the cid as a parameter and then check back with the exchange for that image; then the process of loading the image from exchange does not block the html cid replacement and could lead to loading the page faster, since the html can send to the browser sooner.
    PS: Code is not tested, just so you get the idea!

    EDIT

    Added the missing AdjustIndex function.

    EDIT 2

    Fixed small bug in AdjustIndex

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Working with dates in ruby and rails on windows, I'm having problems with pre-epoch
Working demo CLick $(document).ready(function(e) { $('span#pijlr, span#pijll').show(); $('#gallery').css('overflow','hidden'); $('span#pijlr').click(function(e) { if (!$('#gallcont').is(':animated')) { var
We are currently working on some kind of mailprocessing via EWS Exchange. Now if
Summary: I am having trouble connecting to an exchange 2007 mailbox that is running
Working with an API that can handle multiple connections (i.e. sessions), each of these
I'm trying to access my email using EWS. It is working fine from a
Working with an API where I need to send a value over in an
Working with a SqlCommand in C# I've created a query that contains a IN
Working on a project at the moment and we have to implement soft deletion
Working on a somewhat complex page for configuring customers at work. The setup is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.