Search code examples
androidemailexchange-serverksoap2android-ksoap2

Connect to Exchange using Ksoap2 for android


I am trying to use ksoap2 to connect to an exchange web server to create an email app in android. So far I have very little to go on since Microsoft doesn't give a straight forward explanation of how to use the autodiscover soap methods. So far this is all I have

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        String userpass = username+password;
        NTCredentials cred = new NTCredentials(userpass);

I am using Android API 21 since it still has the NTCredentials methods. My question then is how do I connect to an exchange server. And then is there anything else I need to do to view email or send email. My app already can show the inbox for a imap or pop3 mail client so can I just connect to the exchange server using ksoap then just use the already defined methods for imap and pop3 to do everything else?


Solution

  • As I mentioned in the comments above I have not done this before, but i have used ksoap2 a bit, so i would try something like this:

    String NAMESPACE = "http://www.namespace.com/";
    String METHOD_NAME = "MethodName";
    String SOAP_ACTION = NAMESPACE+METHOD_NAME;
    String URL = "https://www.namespace.com/services/Service.asmx";
    
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    // Set all input params   
    request.addProperty("property", "value");   
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    // Enable the below property if consuming .Net service
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);
    
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    try 
    {
        List<HeaderProperty> headerList = new ArrayList<HeaderProperty>();
        //I would use this for authentication
        headerList.add(new HeaderProperty("Authorization", "Basic " + Base64.encode((username+":"+password).getBytes())));
    
        androidHttpTransport.call(SOAP_ACTION, envelope, headerList);
        SoapObject response = (SoapObject)envelope.getResponse();
    }
    catch(Exception e)
    {
    }
    

    This is Basic authentication.