我遇到了一个有趣的错误。我使用mailgun和Retrofit通过我的应用程序发送电子邮件。在打开我的应用程序后的第一次尝试中,发送尝试始终返回改进错误(400 Bad Request)。但是,所有后续尝试都会适当地发送。
MailGunClient界面:
公共接口MailGunClient {
@FormUrlEncoded
@POST("/messages")
void sendMessagePurchase(
@Field("from") String from,
@Field("to") String to,
@Field("h:Reply-To") String replyToAddress,
@Field("subject") String subject,
@Field("text") StringBuilder text,
Callback<Response> responseCallback);
}
实例化接口并尝试发送(MainActivity)的方法:
public void sendResume(String productID) {
if (productID.equals(EMAILSKU)) {
mMailGunClient = new RestAdapter.Builder()
.setEndpoint("https://api.mailgun.net/v3/mg.resumetemplatepro.com")
.setLogLevel(RestAdapter.LogLevel.FULL)
.setRequestInterceptor(new RequestInterceptor() {
@Override
public void intercept(RequestFacade request) {
final String credentials = "api" + ":" + "key-c5b8f0c0c7dcaabc23075b977a7b7e43";
final String authString = "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.DEFAULT);
request.addHeader("Authorization", authString);
}
})
.build().create(MailGunClient.class);
StringBuilder messageBody = new StringBuilder();
messageBody.append("Hello," + "\r\n" + "\r\n");
messageBody.append("Thank you for purchasing a resume. You can view the resume at the following link: " + getResumeTemplateDownloadLink(pager.getCurrentItem()) + "\r\n" + "\r\n");
messageBody.append("Regards," + "\r\n");
messageBody.append("The Resume Template Pro Team");
mMailGunClient.sendMessagePurchase("resume@mg.resumetemplatepro.com", customerEmailAddress, RBPEMAIL, "Resume Template Email",
messageBody, new Callback<Response>() {
@Override
public void success(Response response, Response response2) {
System.out.println("Success");
Toast.makeText(getApplication(), R.string.successfully_sent, Toast.LENGTH_SHORT).show();
Apptentive.engage(MainActivity.this, "Post_Sale");
}
@Override
public void failure(RetrofitError error) {
Toast.makeText(getApplicationContext(), R.string.try_again, Toast.LENGTH_LONG).show();
}
});
} else if (productID.equals(RESUMECOVERLETTER)) {
mMailGunClient = new RestAdapter.Builder()
.setEndpoint("https://api.mailgun.net/v3/mg.resumetemplatepro.com")
.setLogLevel(RestAdapter.LogLevel.FULL)
.setRequestInterceptor(new RequestInterceptor() {
@Override
public void intercept(RequestFacade request) {
final String credentials = "api" + ":" + "key-c5b8f0c0c7dcaabc23075b977a7b7e43";
final String authString = "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.DEFAULT);
request.addHeader("Authorization", authString);
}
})
.build().create(MailGunClient.class);
StringBuilder messageBody = new StringBuilder();
messageBody.append("Hello," + "\r\n" + "\r\n");
messageBody.append("The user with e-mail " + customerEmailAddress + " has purchased a professional edit. They purchased " + getResumeTemplateDownloadLink(pager.getCurrentItem()) + "." + "\r\n" + "\r\n");
messageBody.append("Regards," + "\r\n");
messageBody.append("The Resume Template Pro Team");
mMailGunClient.sendMessagePurchase("resume@mg.resumetemplatepro.com", RBPEMAIL, customerEmailAddress, "Resume Template Purchase",
messageBody, new Callback<Response>() {
@Override
public void success(Response response, Response response2) {
System.out.println("Success");
Toast.makeText(getApplication(), R.string.edit_purchase, Toast.LENGTH_SHORT).show();
Apptentive.engage(MainActivity.this, "Post_Sale");
}
@Override
public void failure(RetrofitError error) {
Toast.makeText(getApplicationContext(), R.string.try_again, Toast.LENGTH_LONG).show();
}
});
} else {
Toast.makeText(MainActivity.this, R.string.error, Toast.LENGTH_SHORT).show();
}
}