Reputation: 162995
I am using EWS in C# to query emails in my mailbox. I am able to get the email body message. I want to know how can I get the images embedded int the email.
Here is an example of email body. I want to download the image "cid:[email protected]", how can I do that?
<body lang="EN-US" link="#0563C1" vlink="#954F72">
<div class="WordSection1">
<p class="MsoNormal">Solid wood dining table with 6 chairs (2 captain chairs, 4 armless). Great condition.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><img width="469" height="287" id="Picture_x0020_1" src="cid:[email protected]">
<o:p></o:p></p>
<p class="MsoNormal"> <img width="212" height="313" id="Picture_x0020_2" src="cid:[email protected]"> <img width="281" height="469" id="Picture_x0020_3" src="cid:[email protected]"><o:p>
</o:p></p>
</div>
</body>
Upvotes: 0
Views: 3741
Reputation: 691
If you call item.Load(), that will also do it which is something like this
foreach (var item in findResults.Items)
{
item.Load(); // this will load everything
}
Upvotes: 0
Reputation: 166
The HasAttachments property is false for inline attachments, even if the Attachments collection is populated. This was confusing.
Upvotes: 0
Reputation: 22032
The images should be in the Attachments collection so you should be able to just enumerate through the attachments collection find the matching Cid and download it. The Attachment collection won't be returned with FindItems operation so you need to make sure you use a GetItem operation on the Message to get those details eg
ItemView view = new ItemView(100);
view.PropertySet = new PropertySet(PropertySet.IdOnly);
PropertySet PropSet = new PropertySet();
PropSet.Add(ItemSchema.HasAttachments);
PropSet.Add(ItemSchema.Body);
PropSet.Add(ItemSchema.DisplayTo);
PropSet.Add(ItemSchema.IsDraft);
PropSet.Add(ItemSchema.DateTimeCreated);
PropSet.Add(ItemSchema.DateTimeReceived);
PropSet.Add(ItemSchema.Attachments);
FindItemsResults<Item> findResults;
do
{
findResults = service.FindItems(WellKnownFolderName.Inbox, view);
if (findResults.Items.Count > 0)
{
service.LoadPropertiesForItems(findResults.Items, PropSet);
foreach (var item in findResults.Items)
{
foreach (Attachment Attach in item.Attachments) {
if (Attach.IsInline) {
Console.WriteLine(Attach.ContentId);
if(Attach.ContentId == "[email protected]"){
((FileAttachment)Attach).Load(@"c:\temp\downloadto.png");
}
}
}
}
}
view.Offset += findResults.Items.Count;
} while (findResults.MoreAvailable);
Cheers Glen
Upvotes: 4