Getting 400 error when trying to send email through Mailgun API

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!

Yes, you’re facing the sandbox restriction issue. I encountered this while using Mailgun as well. The sandbox is designed to only allow emails to pre-approved addresses to prevent spam during the development phase. To resolve this, access your Mailgun dashboard, navigate to the Sending section, and add your test email to the authorized recipients list. This should enable your code to function correctly. Alternatively, if you prefer using a real domain for testing, you can add one, but remember that you’ll need to complete DNS verification, which can be time-consuming.

yep, that’s the sandbox domain causing issues. just add your recipient email to Mailgun’s authorized recipients list. super annoying when you forget this step!

The error you’re encountering suggests that you’re trying to send an email to an unauthorized recipient using Mailgun’s sandbox domain. To resolve this issue, you have two options: either add the recipient’s email address to your authorized recipients list in the Mailgun dashboard, or consider upgrading to a fully registered domain, which allows you to send emails to any address. I’ve faced this same problem while working with Mailgun; it can be quite frustrating when it’s an account configuration issue instead of code. It’s worth noting that this limitation might not be immediately obvious from their documentation.