5

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....

chaliasos
  • 9,659
  • 7
  • 50
  • 87
zytham
  • 613
  • 1
  • 12
  • 27
  • string SchemaPR_ATTACH_CONTENT_ID = @"http://schemas.microsoft.com/mapi/proptag/0x3712001E"; – zytham May 04 '12 at 09:07

3 Answers3

2

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.

stritch000
  • 355
  • 4
  • 10
  • This wont work if the Mail has a signature with some sort of picture in it. Outlook will not include under the tag. – Steinfeld Nov 04 '15 at 14:48
  • Yes, this does not work when the embedded image is referenced from a css property. Also, I noticed accessing HtmlBody and RtfBody set the mail dirty (ask for save on close) as a side effect :/ – Melvyn Dec 07 '17 at 15:37
1

This is what worked for me - check if the attachment name is present in HTMLBody of email

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/

Mukesh Modhvadiya
  • 2,178
  • 2
  • 27
  • 32
RadsM
  • 21
  • 1
-1

I know this is an old question, but the answer may help someone.

    using Attachment = MsgReader.Outlook.Storage.Attachment;
    
    foreach (Attachment attachment in mailItem.Attachments.Where(a => ((Attachment)a).Hidden == false)) {
      // do whatever you want with the 'real' attachments.
    }
m b k
  • 145
  • 1
  • 4