[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 771
  • Last Modified:

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
0
sudhakar_koundinya
Asked:
sudhakar_koundinya
  • 23
  • 8
  • 6
1 Solution
 
CEHJCommented:
Can't you point us to the online javadoc for the API you're using?
0
 
sudhakar_koundinyaAuthor Commented:
0
 
sudhakar_koundinyaAuthor Commented:
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
CEHJCommented:
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?
0
 
sudhakar_koundinyaAuthor Commented:
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();
0
 
sudhakar_koundinyaAuthor Commented:
as i said earlier

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

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

      //get the attachment
      InputStream attachmentInStream = webDavResourceFile.getMethodData();
0
 
CEHJCommented:
Are you sure this API supports https?
0
 
sudhakar_koundinyaAuthor Commented:
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 :(
0
 
sudhakar_koundinyaAuthor Commented:
and for creating the HTTPS URL Object
here is the snippet
url = new org.apache.commons.httpclient.HttpsURL(username, password, hostname, port, strPath)
0
 
CEHJCommented:
Can you show me the value of urlEML? (disguise as little as possible)
0
 
sudhakar_koundinyaAuthor Commented:
     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;

  }
0
 
sudhakar_koundinyaAuthor Commented:
where username is administrative user account of ms exchange server
0
 
sudhakar_koundinyaAuthor Commented:
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");
0
 
CEHJCommented:
No i meant the *value* of that path
0
 
CEHJCommented:
OK - cross-posted. Shall think about this
0
 
sudhakar_koundinyaAuthor Commented:
ok
0
 
CEHJCommented:
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
0
 
objectsCommented:
try:

webDavResourceFile .getMethod(strAttachmentFile);
0
 
sudhakar_koundinyaAuthor Commented:
@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
0
 
CEHJCommented:
>>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.
0
 
sudhakar_koundinyaAuthor Commented:
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
0
 
objectsCommented:
Yes I figured all that.
An aside, did you try the method I posted above? Saves you handling the file download yourself.
0
 
sudhakar_koundinyaAuthor Commented:
0
 
sudhakar_koundinyaAuthor Commented:
Yes objects

I am doing in the same way
0
 
objectsCommented:
when u access the xml using https does the href have http or https?
0
 
sudhakar_koundinyaAuthor Commented:
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();
0
 
sudhakar_koundinyaAuthor Commented:

https
0
 
sudhakar_koundinyaAuthor Commented:
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() );


0
 
sudhakar_koundinyaAuthor Commented:
do you have any idea about X-MS-ENUMATTACH
0
 
objectsCommented:
> webDavResourceFile.getMethodData();

thought you said you were using getMethod.
0
 
sudhakar_koundinyaAuthor Commented:
What is the difference ?
0
 
sudhakar_koundinyaAuthor Commented:
I mean what is the difference in usage of these methods?
0
 
objectsCommented:
saves you having to worry about reading the stream yourself
0
 
sudhakar_koundinyaAuthor Commented:
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)


0
 
sudhakar_koundinyaAuthor Commented:
No Objects,

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

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 23
  • 8
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now