使用 Mailgun 发送电子邮件 - 未找到 Java 类型 FormDataMultiPart 和 MIME 媒体类型 application/x-www-form-urlencoded 的邮件正文编写器
Sending email with Mailgun - a message body writer for Java type FormDataMultiPart and MIME media type application/x-www-form-urlencoded was not found
我正在尝试使用 Mailgun 和 Jersey 从 android 应用程序发送电子邮件。问题是当我 post 请求获得响应时,应用程序崩溃并出现错误: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
。我不确定为什么会这样。对此进行研究,建议您在创建客户端时添加 multipart class ,但这也不起作用。请注意,在这封电子邮件中,它只是纯文本,但我需要在应用程序中发送另一封可能包含附件的电子邮件 - 0 或更多图片。
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 <donotreply@dnr.com>");
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(模块):
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')
}
看看这个
theWebResource.type(MediaType.APPLICATION_FORM_URLENCODED)
您正在尝试 post 一个旨在用作多部分请求的对象,FormDataMultiPart
,但您将 Content-Type
设置为 application/x-www-form-urlencoded
.
您只需要将 Content-Type
更改为 multipart/form-data
[1] 或 MediaType.MULTIPART_FORM_DATA
.
中所述
我正在尝试使用 Mailgun 和 Jersey 从 android 应用程序发送电子邮件。问题是当我 post 请求获得响应时,应用程序崩溃并出现错误: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
。我不确定为什么会这样。对此进行研究,建议您在创建客户端时添加 multipart class ,但这也不起作用。请注意,在这封电子邮件中,它只是纯文本,但我需要在应用程序中发送另一封可能包含附件的电子邮件 - 0 或更多图片。
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 <donotreply@dnr.com>");
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(模块):
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')
}
看看这个
theWebResource.type(MediaType.APPLICATION_FORM_URLENCODED)
您正在尝试 post 一个旨在用作多部分请求的对象,FormDataMultiPart
,但您将 Content-Type
设置为 application/x-www-form-urlencoded
.
您只需要将 Content-Type
更改为 multipart/form-data
[1] 或 MediaType.MULTIPART_FORM_DATA
.