Link to home
Start Free TrialLog in
Avatar of sudhakar_koundinya
sudhakar_koundinya

asked on

HTTPS : Download Attachments from MS Exchange Server

Hi All,

I just started a small project where i need to communicate with MS Exchange Server . For this I  choose Apache Slide Project and working fine  .

But when i try to download the attachments from the Exchange server which is configured to HTTPS protocol, I am unable  to do it so because webDavResourceFile.getMethodData() is returning any empty input stream i.e. webDavResourceFile.getMethodData().available() is returning always a zero for me. So obviously i am not able to download
them

The Same thing is working if the server is configured to HTTP Protocol.

So please help me to get the attachments when it is configured to HTTPS protocol

thanks
sudhakar
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Can't you point us to the online javadoc for the API you're using?
Avatar of sudhakar_koundinya
sudhakar_koundinya

ASKER

Actually, although this is not intended to represent a solution to the problem, you should not use available() as it is unreliable. You should simply read the stream until it is exhausted.

What kind of resource is it that you're trying to get, as a matter of interest?
here is code snippet how i am working with it


      FileOutputStream attachmentOutStream = new FileOutputStream(
          strAttachmentFile);
      int offset = 0;
      //read the buffer from http stream
      while (true) {
        int availByteCount = attachmentInStream.available();

        if (availByteCount <= 0) {
          //end of attachment buffer
          break;
        }
        byte buffer[] = new byte[availByteCount];
        offset = attachmentInStream.read(buffer);
        //write the downloaded attachment buffer in the Local System Object
        attachmentOutStream.write(buffer, 0, offset);
        buffer = null;
      }
      //clear the buffer;
      attachmentOutStream.close();
      attachmentInStream.close();
as i said earlier

it is working with HTTP protocol, i am facing problem only with https protocol
and for getting the url stream

 org.apache.webdav.lib.WebdavResource webDavResourceFile = getWebResource(
          urlEML);

      //get the attachment
      InputStream attachmentInStream = webDavResourceFile.getMethodData();
Are you sure this API supports https?
yes

because i am able to download all the emails and user acoount information using https protocol.  only i am facing with attachments of emails :(
and for creating the HTTPS URL Object
here is the snippet
url = new org.apache.commons.httpclient.HttpsURL(username, password, hostname, port, strPath)
Can you show me the value of urlEML? (disguise as little as possible)
     org.apache.commons.httpclient.HttpURL urlEML = getURL(path + "/" +
          mailbox + "/" + folder + "/" + filename);


  private org.apache.commons.httpclient.HttpURL getURL(String strPath) {
    org.apache.commons.httpclient.HttpURL url = null;
    try {
      if (bHttps == false) {
        //instantiate the email object for http protocol
        url = new org.apache.commons.httpclient.HttpURL(username, password,
            hostname, port, strPath);

      }
      else {
        //instantiate the email object for https protocol
        url = new org.apache.commons.httpclient.HttpsURL(username, password,
            hostname, port, strPath);

      }
    }
    catch (Exception ex) {
      url = null;
    }

    return url;

  }
where username is administrative user account of ms exchange server
say suppose if i want to access the particular email from inbox folder

i have to create object some thing like this

org.apache.commons.httpclient.HttpURL urlEML = getURL("comp37:80/Exchange/Sudhakar_Koundinya/Inbox/YourInfo.EML");
No i meant the *value* of that path
OK - cross-posted. Shall think about this
a. are you sure that the port is correct for your https?
b. make sure all exceptions are being reported by printing their stack traces
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@CEHJ
I tried all the possible ways that are applicable. And also I said that emails are downloading, the only problem is with attachments. SO there is no problem with port

@objects
I am using the same method for downloading either emails or attachments.
And again I am saying that the code is working for HTTP protocol. And even in HTTPS i am able to download emails only problem is with attachments download

thanks
sudhakar
>>And also I said that emails are downloading, the only problem is with attachments.

Emails and attachments are one and the same thing. When you see attachment icons in your mail client that is only a 'view' thing, not a model thing. The attachments are embedded in the email.

An eml file is not an attachment really - it's a mail that's been saved as a file.
Yes I do agree


but the way  WebDAV resources behaviour is quite different here, MS-Exchange WebDAV server gives the responses in XML format. As attachment of email downloading is different response, MS-Exchange server WebDAV server sends the responses as different XML strings.

1st XML string contains the text part and email attributes such as receiver name, sender name, date ,subject,body and so on

2nd xml string contains name of email attachment, content-type of attachment and so on.

We need to parse the 1st xml string for getting entire email content and I am able to do both it for both  http and https servers

And We need to parse this 2nd xml string for getting the attachments. And I was able to do with HTTP attachment downloads without any problem.
In Https request also i am getting the right xml information. I am able to identify correct paths of attachments. But the problem I was facing was I am not able to get the content of that attachments.

Hope you understand this

Thanks,
Sudhakar
Yes I figured all that.
An aside, did you try the method I posted above? Saves you handling the file download yourself.
Yes objects

I am doing in the same way
when u access the xml using https does the href have http or https?
this is some code snippet

 org.apache.commons.httpclient.HttpURL urlEML = getURL(path + "/" +
          mailbox + "/" + folder + "/" + filename + "/" + strAttachmentName +
          "/");

      org.apache.webdav.lib.WebdavResource webDavResourceFile = getWebResource(
          urlEML);
/************  ATTACHMENT DOWN LOAD******************/
      //get the attachment
      InputStream attachmentInStream = webDavResourceFile.getMethodData();

/************  END OF ATTACHMENT DOWNLOAD ******************/
     
String strAttachmentFile = targetPath + "/" + mailbox + "/" + folder +
          "/" + filename.replaceAll(":", "%3A") + "_Dir/" + strAttachmentName;

      FileOutputStream attachmentOutStream = new FileOutputStream(
          strAttachmentFile);
      int offset = 0;
      //read the buffer from http stream
      while (true) {
        int availByteCount = attachmentInStream.available();

        if (availByteCount <= 0) {
          //end of attachment buffer
          break;
        }
        byte buffer[] = new byte[availByteCount];
        offset = attachmentInStream.read(buffer);
        //write the downloaded attachment buffer in the Local System Object
        attachmentOutStream.write(buffer, 0, offset);
        buffer = null;
      }
      //clear the buffer;
      attachmentOutStream.close();
      attachmentInStream.close();

https
I just got a mail from Apache Slide -Mail Archive

Hi Sudhakar,

I reply you in private because i think it is not a general slide
problem.
I use slide 2.0 rc1 to connect to exchange server both in http and https
and it works perfectly.
To read mail attachment, i use X-MS-ENUMATTACH method and then i simply
use a HttpClient (indeed this is a clone of session of WebdavResource)
and a GetMethod on it and it works fine, i use this because i've extends
WebdavResource class and when i get attachments i don't want that the
status of the resource change.

Do you have any exception when you instantiate the WebdavResource ?

Are you sure that your certificate of your server is valid (and sure
that java will find it valid)?

Try this piece of code to check your certificate :
   
     HttpClient client = new HttpClient();
     GetMethod get = new GetMethod("https://yourserver.com");
     try
     {
       client.executeMethod(get);
     }
     catch ( IOException e )
     {
       e.printStackTrace();
     }
     System.out.println( get.getResponseBodyAsString() );


do you have any idea about X-MS-ENUMATTACH
> webDavResourceFile.getMethodData();

thought you said you were using getMethod.
What is the difference ?
I mean what is the difference in usage of these methods?
saves you having to worry about reading the stream yourself
http://jakarta.apache.org/slide/clientjavadoc/org/apache/webdav/lib/WebdavResource.html#method_summary

According to the API

boolean       getMethod(java.io.File file)
          Execute the GET method for this WebdavResource path.


java.io.InputStream       getMethodData()
          Get InputStream for the GET method.


I think getMethod is not the correct one for testing.

Any How let me test again with another one

getMethod(java.lang.String path, java.io.File file)


No Objects,

getMethod is not the right one, it takes WebDAVResourceFile as an argument
no it takes a java.io.File.