Socket Timeout Exceptions - how to fix connection timeouts or 502 exceptions

Prevent HTTP timeouts and socket exceptions

MailSlurp uses 'Keep-Alive' connections for many endpoints to allow your code to wait for conditions.

SDK timeout settings

It is important to set a connection timeout on the client and its underlying http client implementations when using the MailSlurp SDKs.

For example, in Java pass an OkHttpClient instance to the MailSlurpClient and set read, write and connect timeouts.

public class MailSlurpExamplesTest {

    private static ApiClient apiClient;
    private static final Long TIMEOUT = 30000L;
    private static final String apiKey = System.getenv("API_KEY");

    public static void beforeAll() throws Exception {
        // get API KEY for mailslurp from environment variable
        if (StringUtils.isBlank(apiKey)) {
            throw new Exception("Must provide API KEY");

        // IMPORTANT set timeout for the http client
        OkHttpClient httpClient = new OkHttpClient.Builder()
                .connectTimeout(TIMEOUT, TimeUnit.MILLISECONDS)
                .writeTimeout(TIMEOUT, TimeUnit.SECONDS)
                .readTimeout(TIMEOUT, TimeUnit.SECONDS)

        apiClient = Configuration.getDefaultApiClient();

        // IMPORTANT set api client timeouts

        // IMPORTANT set API KEY and client

This will help to avoid errors that you may encounter with default timeout settings.

The same approach applies to other languages.

Test framework settings

Sometimes connection exceptions can occur in testing frameworks such as Junit or Jest. We recommend setting similar timeouts for your test framework. In Javascript Jest this looks like:


Most MailSlurp operations take several seconds. Waiting for emails is different and can take up to 120 seconds depending on the number of emails you are waiting for and the load on MailSlurp servers. Most clients take timeout values in milliseconds.

const TIMEOUT = 120000

Support for continuing exception

If your connection or 502 errors continue please contact support and provide code examples so we can debug the issue further.

Related content

554 Email rejected SMTP error

How to diagnose 554 smtp errors

Dealing with SendGrid 550 and 530 errors

SendGrid uses a shared pool of IP addresses between all users. This means spamming users of SendGrid can ruin the reputa...

Trouble-shooting general 530 SMTP errors (email receiving is...

Diagnosing receiving errors with Gmail, Yahoo, etc.

Socket Timeout Exceptions - how to fix connection timeouts o...

Prevent HTTP timeouts and socket exceptions

Ready to dive in?Start building email applications today.