I have an issue when trying to send emails from my EC2 instance using SMTP to SES. For some reason I am getting sporadic timeout issues, where I can no longer contact the S
Its looks like a case of Port 25 throttling on EC2. AWS by default throttles port 25 to avoid bulk emails being sent out by malicious users/softwares. You can get this limit removed by following the instructions here: https://aws.amazon.com/premiumsupport/knowledge-center/ec2-port-25-throttle/
Alternatively, you can change the outgoing port in your application to use 587, 2587 for STARTTLS support or use 465/2465 for TLS Wrapper.
If you're running a Django app and suspect Steffen's answer might be the cause - here is a quick litmus test:
In [1]: from django.core.mail.backends.smtp import EmailBackend
In [2]: from django.core.mail import EmailMultiAlternatives
In [3]: message = EmailMultiAlternatives(
...: subject='testing the rate limit',
...: body='this is a test',
...: to=['your+email@example.com'],
...: from_email='from@example.com',
...: )
In [4]: backend_587 = EmailBackend(port=587)
In [5]: backend_25 = EmailBackend(port=25)
In [6]: backend_587.send_messages([message])
Out[6]: 1
In [7]: backend_25.send_messages([message]) # hangs for a long time. Might even timeout
Sending the email from port 25 should hang. Sending the email from 587 should send quickly.
So apparently EC2 has it's own limits. I assumed (incorrectly) that having production access to SES would also mean relaxed SMTP limitations from EC2, but as they are two completely separate products I guess that is not the case.
But as I stated in the last paragraph of my post, you can Request to Remove Email Sending Limitations to have these limits raised. I did that and the problem stopped (it took them around 5 hours to get my limits removed).
The EC2 throttling is documented in Connecting to the Amazon SES SMTP Endpoint and actually constrained to port 25, so an alternative and immediate solution is simply using port 587 instead (it's a bit unfortunate that several official SES examples are using port 25 indeed):
Important
Elastic Compute Cloud (EC2) throttles email traffic over port 25 by default. To avoid timeouts when sending email through the SMTP endpoint from EC2, use a different port (587 or 2587) or fill out a Request to Remove Email Sending Limitations to remove the throttle.
Beware that this might be slightly outdated as well, insofar both the AWS Management Console and section Amazon SES SMTP Issues are referring to the more common alternative ports 465 and 587 only:
You are sending to Amazon SES from an Amazon EC2 instance via port 25 and you cannot reach your Amazon SES sending limits or you are receiving time outs — Amazon SES EC2 imposes default sending limits on email sent via port 25 and throttles outbound connections if you attempt to exceed those limits. To remove these limits, submit a Request to Remove Email Sending Limitations. You can also connect to Amazon SES via port 465 or port 587, neither of which is throttled.