Read message body with InLine Attachment’s and render it

In order to use this code you will need to get the GetBinding() method and understand how to get a message item UniqueId

I am catching the Inline attachments and converting to Base64, should be fine for you

using Microsoft.Exchange.WebServices.Autodiscover;
using Microsoft.Exchange.WebServices.Data;
using BodyType = Microsoft.Exchange.WebServices.Data.BodyType;/// <summary>

/// Les the menssagem por id.
/// </summary>
/// <param name="messId">The mess id.</param>
/// <returns></returns>
public static string LeMenssagemPorId(string messId)
{
PropertySet psPropSet = new PropertySet(BasePropertySet.FirstClassProperties)
{
    RequestedBodyType = BodyType.HTML
};
Item mess = Item.Bind(GetBinding(), messId, psPropSet);
mess.Body.BodyType = BodyType.HTML;

string sHTMLCOntent = mess.Body.Text;

FileAttachment[] attachments = null;

if (mess.Attachments.Count != 0)
{
    attachments = new FileAttachment[mess.Attachments.Count];
    for (int i = 0; i < mess.Attachments.Count; i++)
    {
        try
        {
            if (mess.Attachments[i].IsInline)
            {
                string sType = mess.Attachments[i].ContentType.ToLower();
                if (sType.Contains("image"))
                {
                    attachments[i] = (FileAttachment)mess.Attachments[i];
                    attachments[i].Load();
                    string sId = attachments[i].ContentId;
                    sType = sType.Replace("image/", "");
                    string oldString = "cid:" + sId;
                    string imagem = 
                        Convert.ToBase64String(attachments[i].Content);
                    sHTMLCOntent = sHTMLCOntent.Replace(oldString, 
                        "data:image/" + sType + ";base64," + imagem);
                }
            }
        }
        catch (Exception ex)
        {
            HttpContext.Current.Response.Write("Erro: " + ex.Message);
        }
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s