How to Retrieve inline images of EWS + Exchange 2013 (C#)

How to Retrieve inline images of EWS + Exchange 2013
In the HTML Body was has image cid
such as <img src=cid:part1.0000000.0000000@xxxx.com" alt=“”>
i want Example Source Code for get this image.


Thanks




Hi Nattapong,

Thank you for using Aspose.Email API.

In order to get the inline image from the message body, you need to first download the message and then extract the image which is part of the body in the form of linked resource. Please have a look at the following code sample and share your feedback with us.

Code:

string strCid = "part1.0000000.0000000@xxxx.com";

IEWSClient client = GetAsposeEWSClient1();
ExchangeMessageInfoCollection msgsColl = client.ListMessages(client.MailboxInfo.InboxUri);

foreach (ExchangeMessageInfo msgInfo in msgsColl)
{
    MailMessage msg = client.FetchMessage(msgInfo.UniqueUri);

    foreach(LinkedResource lr in msg.LinkedResources)
    {
        if (lr.ContentId.Equals(strCid))
        {
            lr.Save(lr.ContentType.Name);
        }
    }
}

Hi Muhammad.Waqas,

Thank you for your reply, but your code don’t work for me.
This My Code :

// Call ListMessages method to list messages info from Inbox
ExchangeMessageInfoCollection msgCollection = client.ListMessages(client.MailboxInfo.InboxUri);

// Loop through the collection to display the basic information
foreach (ExchangeMessageInfo msgInfo in msgCollection)
{
if (!msgInfo.IsRead)
{
Console.WriteLine("Subject: " + msgInfo.Subject);
Console.WriteLine("From: " + msgInfo.From.ToString());
Console.WriteLine("To: " + msgInfo.To.ToString());
Console.WriteLine(“Message ID: " + msgInfo.MessageId);
Console.WriteLine(“Unique URI: " + msgInfo.UniqueUri);
Console.WriteLine(”==================================”);

rs += "Subject: " + msgInfo.Subject;
rs += "From: " + msgInfo.From.ToString();
rs += "To: " + msgInfo.To.ToString();
rs += "Message ID: " + msgInfo.MessageId;
rs += "Unique URI: " + msgInfo.UniqueUri;
rs += “==================================”;
rs += Environment.NewLine;

string strMessageURI = msgInfo.UniqueUri;

// Now get the message details using FetchMessage()
MailMessage msg = client.FetchMessage(strMessageURI);
string strcid = “part1.02060305.05070904@xxxxx.com”;

rs += "msg.LinkedResources: " + msg.LinkedResources.Count.ToString();
rs += Environment.NewLine;
rs += “==================================”;
foreach (LinkedResource lr in msg.LinkedResources)
{
if (lr.ContentId.Equals(strcid))
{
rs += "lr.ContentId.Equals(strcid): " + lr.ContentId.Equals(strcid).ToString();
rs += Environment.NewLine;
rs += “==================================”;
lr.Save(lr.ContentType.Name);
rs += "lr.ContentType.Name: " + lr.ContentType.Name.ToString();
rs += Environment.NewLine;
rs += “==================================”;
}
}
}

from this statement i write to text file , i will check in line "rs += “msg.LinkedResources: " + msg.LinkedResources.Count.ToString();” msg.LinkedResources.Count is equal 0

This Below is Part of Email Body String:

Dear All

ทดสอบ mail to ticket

Best Regards,

Kitikorn Iawsipo | Business Analyst

E | [Kitikorn.i@xxxxxx.co.th](mailto:Kitikorn.i@xxxxx.co.th)
| [http://www.zzzzz.com](http://www.xxxxx.com/)


please look this my code and give me for solution

Thank

Nut

Hi Nattapong,

Please try avoiding the check on the content id of the linked resource and save directly as shown in the following code sample. It will save the linked resource in the mail to the disc. Please share your feedback with us if you have any further concern about this.

Code:

IEWSClient client = GetAsposeEWSClient1();
ExchangeMessageInfoCollection msgs = client.ListMessages(client.MailboxInfo.InboxUri);
MailMessage msg = client.FetchMessage(msgs[0].UniqueUri);

foreach (LinkedResource lr in msg.LinkedResources)
{
    Console.WriteLine(lr.ContentId);
    Console.WriteLine(lr.ContentType.MediaType);
    Console.WriteLine(lr.ContentType.Name);
    if (lr.ContentType.MediaType.Contains("jpeg"))
        lr.Save(lr.ContentType.Name + ".jpg");
    else if (lr.ContentType.MediaType.Contains("bmp"))
        lr.Save(lr.ContentType.Name + ".bmp");
}

Hi Muhammad.Waqas,

Thank you for your reply,
I Following by this code with Write to Log File, In Log File Not Has ContentID quantity But in Email Message Has CID (*CID sample is in File Attachment)
This My Code :

// Call ListMessages method to list messages info from Inbox
ExchangeMessageInfoCollection msgCollection = client.ListMessages(client.MailboxInfo.InboxUri);

// Loop through the collection to display the basic information
foreach (ExchangeMessageInfo msgInfo in msgCollection)
{
if (!msgInfo.IsRead)
{
string strMessageID = msgInfo.MessageId;
string strMessageURI = msgInfo.UniqueUri;

Console.WriteLine("Message ID: " + msgInfo.MessageId);
Console.WriteLine("Unique URI: " + msgInfo.UniqueUri);

rs += "Message ID: " + strMessageID;
rs += Environment.NewLine;
rs += “==================================”;
rs += "Unique URI: " + strMessageURI;
rs += Environment.NewLine;
rs += “==================================”;

MailMessage msg = client.FetchMessage(strMessageURI);

**rs += "msg.LinkedResources: " + msg.LinkedResources.Count.ToString();**
rs += Environment.NewLine;
rs += “==================================”;
rs += Environment.NewLine;
rs += "HTML Body: " + msg.HtmlBody.ToString();
rs += “==================================”;
rs += Environment.NewLine;

foreach (LinkedResource lr in msg.LinkedResources)
{
Console.WriteLine("lr.ContentId: " + lr.ContentId);
Console.WriteLine("lr.ContentType.MediaType: " + lr.ContentType.MediaType);
Console.WriteLine("lr.ContentType.Name: " + lr.ContentType.Name);

if (lr.ContentType.MediaType.Contains(“jpeg”))
{
rs += "lr.ContentType.MediaType.Contains(“jpeg”): " + lr.ContentType.MediaType;
rs += Environment.NewLine;
rs += “==================================”;
lr.Save(lr.ContentType.Name + “.jpg”);
}
else if (lr.ContentType.MediaType.Contains(“bmp”))
{
rs += "lr.ContentType.MediaType.Contains(“bmp”): " + lr.ContentType.MediaType;
rs += Environment.NewLine;
rs += “==================================”;
lr.Save(lr.ContentType.Name + “.bmp”);
}
}
}
}

Result :
0 : Message ID: [55FA7B3A.7000007@xxxxx.com](mailto:55FA7B3A.7000007@xxxxx.com)

Unique URI: AAMkAGI2MDBmMTY3LTllNDYtNDg2Ni05N2MzLWRjYjFmZGQ0OTkzNwBGAAAAAAATQ9rdm1xmSalqcho2UQRKBwDEUcDxQ8UGT4WxgDM1bF+nAAAAAAAOAADEUcDxQ8UGT4WxgDM1bF+nAAGgR4npAAA=

msg.LinkedResources: 0

please look this my code and give me for solution

Thank

Nut

Hi Nattapong,


Can you please share your sample email message file with us? The log you have provided us shows that the linked resources are not read from the message file and we need to investigate this issue at our end. We shall look into it and share our feedback with you.

Hi Muhammad.Waqas,

In File Attachment is mail message for test retrieve inline image , This Message is send by OWA Exchange 2013 and in Mail Body Has 1 Image , I used ASPOSE Mail Save to drive with eml file extension.


Thanks & Best Regards,
Nut

Hi Nattapong,

Thank you for sharing the sample message file.

We tested this issue by appending your sample message to Office 365 Exchange Server 2013 test account and fetched it using the following code sample. It retains the linked resource image in the fetched message and saves it successfully to the disc. The code sample provides original credentials for the test account. Please try it at your end and share your feedback with us. Please don’t forget to test it using the latest version of the API i.e. Aspose.Email for .NET 5.7.0.

Code:

IEWSClient _client = EWSClient.GetEWSClient(“https://outlook.office365.com/ews/exchange.asmx”, "UserOne@ase1986.onmicrosoft.com", “Aspose1234”); 
//Declare variable for getting specified custom folder uri ExchangeFolderInfo subfolderInfo = new ExchangeFolderInfo(); 
//Check if specified custom folder exisits _client.FolderExists(_client.MailboxInfo.InboxUri, "SF1", out subfolderInfo); 
if (subfolderInfo != null) { ExchangeMessageInfoCollection msgsColl = _client.ListMessages(subfolderInfo.Uri); 
MailMessage msg = _client.FetchMessage(msgsColl[0].UniqueUri); 
foreach (LinkedResource lr in msg.LinkedResources) { 
Console.WriteLine(lr.ContentId); 
Console.WriteLine(lr.ContentType.MediaType); 
Console.WriteLine(lr.ContentType.Name); 
if (lr.ContentType.MediaType.Contains("jpeg"))
 lr.Save(lr.ContentType.Name + ".jpg"); 2
else if (lr.ContentType.MediaType.Contains("bmp")) 
lr.Save(lr.ContentType.Name + ".bmp"); 
}
 }

PS: you can get a 30-day temporary license free of cost to evaluate the API functionality without any limitations.