I am attempting to send an email from an android app using Mailgun and Jersey. The problem is when I post the request to get a response, the app crashes with error: A message body writer for Java type, class com.sun.jersey.multipart.FormDataMultiPart, and MIME media type, application/x-www-form-urlencoded, was not found. I am not sure why this is the case. Researching this it was suggested you add the multipart class when creating the client, but that did not work either. Note that in this email it is simply plain text, but I will need to send another email in the app that may contain attachments - 0 or more images.

java class:

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
import com.sun.jersey.multipart.FormDataMultiPart;
import com.sun.jersey.multipart.impl.MultiPartWriter;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

ClientConfig cc = new DefaultClientConfig();
cc.getClasses().add(MultiPartWriter.class);
Client theClient = Client.create(cc);
theClient.addFilter(new HTTPBasicAuthFilter("api", "my_api_key"));

final WebResource theWebResource = theClient.resource("https://api.mailgun.net/v3/"
                                                        + "mailgun_domain_key"
                                                        + "/messages");

final FormDataMultiPart message = new FormDataMultiPart();
message.field("from", "My App <[email protected]>");
message.field("to", "email_recs");
message.field("subject", "subj");
message.field("text", "body_text");

ClientResponse response = theWebResource.type(MediaType.APPLICATION_FORM_URLENCODED).post(ClientResponse.class, message);
//app crashes after the above line is executed

build.gradle (module):

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.x.x"
        minSdkVersion 14
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    packagingOptions{
        pickFirst 'META-INF/jersey-module-version'
        pickFirst 'META-INF/services/com.sun.jersey.spi.inject.InjectableProvider'
        pickFirst 'META-INF/services/javax.ws.rs.ext.MessageBodyReader'
        pickFirst 'META-INF/services/javax.ws.rs.ext.MessageBodyWriter'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile 'com.parse.bolts:bolts-android:1.+'
    compile fileTree(dir: 'libs', include: 'Parse-*.jar')
    compile files('libs/jersey-client-1.19.jar')
    compile files('libs/jersey-core-1.19.jar')
    compile files('libs/jersey-multipart-1.19.jar')
    compile files('libs/javax.ws.rs.jar')
    compile files('libs/jai_imageio-1.1.jar')
}
1

There are 1 answers

0
Paul Samsotha On BEST ANSWER

Look at this

theWebResource.type(MediaType.APPLICATION_FORM_URLENCODED)    

You're trying to post an object that is meant to be used as a multipart request, FormDataMultiPart, but you are setting the Content-Type to application/x-www-form-urlencoded.

You just need change the Content-Type to multipart/form-data[1] or MediaType.MULTIPART_FORM_DATA.

1. As mentioned in the API documentation