I’m trying to send emails using Mailgun API with Android Volley library but keep getting a 400 error response. Here’s what I’m doing:
HashMap<String, String> emailData = new HashMap<>();
emailData.put("from", "Test Sender <[email protected]>");
emailData.put("to", recipientField.getText().toString());
emailData.put("subject", titleField.getText().toString());
emailData.put("text", bodyField.getText().toString());
HashMap<String, String> authHeaders = new HashMap<>();
String apiCredentials = "api:" + Config.API_KEY;
String basicAuth = "Basic " + Base64.encodeToString(apiCredentials.getBytes(), Base64.NO_WRAP);
authHeaders.put("Authorization", basicAuth);
mailService.sendPostRequest(emailData, authHeaders, Config.MAILGUN_URL + Config.SEND_ENDPOINT);
Then I make the HTTP request:
public void sendPostRequest(final Map data, final Map requestHeaders, String endpoint) {
StringRequest mailRequest = new StringRequest(Request.Method.POST, endpoint,
new Response.Listener<String>() {
@Override
public void onResponse(String result) {
Log.d("mail_response", result);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError err) {
Log.e("mail_error", err.toString());
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
return data;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
return requestHeaders;
}
};
RequestManager.getInstance().getQueue().add(mailRequest.setShouldCache(false));
}
The error message says: “Sandbox subdomains are for test purposes only. Please add your own domain or add the address to authorized recipients in Account Settings.”
What am I missing here? Any help would be great!