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....
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With