Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to differentiate between Inline image and attachment in outlook 2010 [C#]

Hi I have to read attachment and inline image separately in local directory from outlook 2010 using C#. I have used property and content ID concept for this. I am using following code for doing that but it is now working.

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 work in progress....

like image 352
zytham Avatar asked Nov 04 '22 00:11

zytham


1 Answers

I came here looking for a solution but didn't like the idea of searching for "cid:" in the whole HTMLBody. Firstly, it's slow to do that for every file name and secondly, if "cid:" was present in the body text I would get a false positive. Also, performing ToLower() on HTMLBody is not a good idea.

Instead I ended up using a regular expression once on the HTMLBody to look for any instance of the <img> tag. Thus there is no way to falsely match "cid:" in the body text (however unlikely that may be).

     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"));
     }

I'm quite sure that I could have written a regular expression to return just the file names and that it would probably be more efficient. But I'd rather the extra expense for the sake of readability for those who aren't Regex gurus that have to maintain the code.

like image 189
stritch000 Avatar answered Nov 12 '22 13:11

stritch000