How to distinguish between embedded image and attachment in Outlook 2010 [C #]

Hi, I need to read the attachment and inline image separately in a local directory with Outlook 2010 using C #. For this, I used the concept of identifying properties and content. I am using the following code for this, but now it works.

if (mailItem.Attachments.Count > 0)
{
    /*for (int i = 1; i <= mailItem.Attachments.Count; i++)
    {
    string filePath = Path.Combine(destinationDirectory, mailItem.Attachments[i].FileName);
    mailItem.Attachments[i].SaveAsFile(filePath);
    AttachmentDetails.Add(filePath);
    }*/

    foreach (Outlook.Attachment atmt in mailItem.Attachments)
    {
        MessageBox.Show("inside for each loop" );
        prop = atmt.PropertyAccessor;
        string contentID = (string)prop.GetProperty(SchemaPR_ATTACH_CONTENT_ID);
        MessageBox.Show("content if is " +contentID);

        if (contentID != "")
        {
            MessageBox.Show("inside if loop");
            string filePath = Path.Combine(destinationDirectory, atmt.FileName);
            MessageBox.Show(filePath);
            atmt.SaveAsFile(filePath);
            AttachmentDetails.Add(filePath);
        }
        else
        {
            MessageBox.Show("inside else loop");
            string filePath = Path.Combine(destinationDirectoryT, atmt.FileName);
            atmt.SaveAsFile(filePath);
            AttachmentDetails.Add(filePath);
        }
    }
}

please help in the work.

+5
source share
2 answers

I came here to look for a solution, but I did not like the idea of ​​finding "cid:" in all HTMLBody. Firstly, it slowly does this for each file name, and secondly, if "cid:" was present in the main text, I would get a false result. Also, doing ToLower () in HTMLBody is not a good idea.

HTMLBody, <img> . , "cid:" ( ).

     Regex reg = new Regex(@"<img .+?>", RegexOptions.Singleline | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
     MatchCollection matches = reg.Matches(mailItem.HTMLBody);

     foreach (string fileName in attachments.Select(a => a.FileName)
     {
        bool isMatch = matches
           .OfType<Match>()
           .Select(m => m.Value)
           .Where(s => s.IndexOf("cid:" + fileName, StringComparison.InvariantCultureIgnoreCase) >= 0)
           .Any();

        Console.WriteLine(fileName + ": " + (isMatch ? "Inline" : "Attached"));
     }

, , , , . , Regex, .

+2

, - , HTMLBody

VB.NET

<i>
For Each oAttachment In m_olMailItem.Attachments 
If m_olMailItem.HTMLBody.ToLower.Contains("cid:" & oAttachment.FileName) = True Then 
   Msgbox("Embedded")

Else 
   Msgbox("Not Embedded ! ")

End if 

http://www.outlookforums.com/threads/44155-detecting-if-attachement-is-embedded-or-not/

-1

All Articles