EWS Managed Api - Attachment URL

539 views Asked by At

I am using EWS Managed Api 2.2 and Exchange Server 2010_SP2. I am developing something to get attachment of email. I am wondering if I can get the url of any attachment in email to access from anywhere.

Thanks

1

There are 1 answers

0
Chris Barnes On

You can, but it might not be that pretty. In summary, you can use WebClientReadFormQueryString as a basis for constructing the link to an attachment. The attachments, in OWA, have the same id as the parent message. Below is what I did to construct the link.

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Diagnostics;
using System.IO;
using Microsoft.Exchange.WebServices;
using Microsoft.Exchange.WebServices.Data;
using Microsoft.Exchange.WebServices.Auth;


/******************************************************/
    //pass WebClientReadFormQueryString and the attachment index value
    private static string makeAttachmentLinkForOWA(string queryString, int itemNum = 0)
    {
        //extract the id. The url encoding is lost
        NameValueCollection queryCollection = HttpUtility.ParseQueryString(queryString);

        //apply url encoding to the extracted id
        string msgId = HttpUtility.UrlEncode(queryCollection.Get("id"));

        //this is to increment attid0
        char attachmentLetter = Convert.ToChar(itemNum + 65);

        //build the query prefix
        string a = "https://webmail.myorg.org/owa/attachment.ashx?attach=1&id=";

        //build the attachment postfix. attid0 works with documents (docx, xls, etc.) but failed on .wav
        string b = String.Format("&attid0=BAA{0}AAAA&attcnt=1", attachmentLetter.ToString());

        //its broken if there isn't a messageId
        if (msgId == null)
        {
            return null;
        }
        else
        {
            //build the link
            string link = String.Format("{0}{1}{2}", a, msgId, b);

            return link;
        }
    }