Integrations & Code Verified for 2026 Expert Reviewed

How to Send Bulk Emails in Python with smtplib & aiosmtplib

Direct Answer Quick Summary for Senders:

To send bulk emails in Python with high performance, use the asynchronous aiosmtplib library with asyncio rather than the synchronous smtplib module. Implement an asyncio.Semaphore to limit concurrent connections (e.g., max 10), reuse connections with persistent client sessions, and connect to 171mails' dedicated SMTP relay on port 587 with STARTTLS.

Key Takeaways

Synchronous smtplib blocks the Python GIL and delivers poor throughput (< 5 emails/second).
aiosmtplib combined with asyncio handles hundreds of concurrent messages non-blockingly.
Enforce rate delays and concurrency bounds using asyncio.Semaphore and asyncio.sleep.
171mails provides 4 Dedicated IPs and automated suppression scrubbing for Python developers.

1. Production-Ready aiosmtplib Script

import asyncio
import aiosmtplib
from email.message import EmailMessage

SMTP_HOST = "relay.171mails.com"
SMTP_PORT = 587
SMTP_USER = "your_171mails_username"
SMTP_PASS = "your_171mails_password"
CONCURRENCY_LIMIT = 10

semaphore = asyncio.Semaphore(CONCURRENCY_LIMIT)

async def send_single_email(client, recipient):
    async with semaphore:
        msg = EmailMessage()
        msg["From"] = "updates@yourdomain.com"
        msg["To"] = recipient
        msg["Subject"] = "Important System Update"
        msg.set_content("Hello, this is an authenticated high-speed delivery.")

        await client.send_message(msg)
        print(f"Delivered to {recipient}")

async def main(recipients):
    client = aiosmtplib.SMTP(hostname=SMTP_HOST, port=SMTP_PORT, start_tls=True)
    await client.connect()
    await client.login(SMTP_USER, SMTP_PASS)

    tasks = [send_single_email(client, r) for r in recipients]
    await asyncio.gather(*tasks)
    await client.quit()

# Run async loop
# asyncio.run(main(["user1@example.com", "user2@example.com"]))

2. The Danger of Unbounded Concurrency

If you create 5,000 asynchronous tasks without an asyncio.Semaphore, Python will attempt to open 5,000 simultaneous sockets, causing OS socket exhaustion (OSError: [Errno 24] Too many open files) and triggering destination firewall bans. Always bound concurrency between 5 and 15.

3. Integrating Celery for Distributed Python Workloads

In web applications (Django, FastAPI, Flask), offload sending to Celery task queues backed by Redis or RabbitMQ for automated retries and hourly throughput pacing.

Frequently Asked Questions

Standard smtplib is synchronous and blocking. It waits for each network packet and TLS handshake sequentially before beginning the next message. aiosmtplib uses non-blocking I/O to handle multiple connections simultaneously.

Yes, you can use the httpx or requests library to dispatch campaigns to 171mails' JSON REST API endpoint.

171mails automatically pre-scrubs invalid recipients against its 100M+ real-time suppression list, preventing your Python scripts from hitting dead accounts or honeypots.
Enterprise Bulk Email Relay

Scale Python Email Applications with Enterprise Relays

Deploy async Python email pipelines backed by 4 Dedicated IPs, automated rate pacing, and 100M+ real-time suppression scrubbing.

  • Native compatibility with aiosmtplib, smtplib, Celery & FastAPI
  • 4 Dedicated IPs (2 IPv4 + 2 IPv6 dual-stack) up to 1M sends
  • 100M+ real-time suppression database scrubbing
  • Test your Python script with our $5 (₹399) sandbox
100K Email Plan
$29 /mo
4 Dedicated IPs Included

Related Questions & Guides

Platform, CRM & Stack Integrations
Bulk Email API for Node.js (High-Throughput SMTP & REST)

Send high-throughput bulk email in Node.js using Nodemailer and REST APIs. Master connection pooling, concu...

Platform, CRM & Stack Integrations
Django Bulk Email Sending with Celery & SMTP Relay (Guide)

Send high-throughput bulk emails in Django using Celery and external SMTP relays. Includes settings.py, Cel...

Platform, CRM & Stack Integrations
Bulk Email API for PHP & PHPMailer Integration Guide

Connect PHP and PHPMailer to high-performance SMTP relays. Learn SMTPKeepAlive, timeout tuning, batch proce...

Volume, Scaling & Rate Limits
SMTP Concurrency, Connections & Rate Limits: Complete Guide

Master SMTP concurrency limits, connection pools, and MX rate limits. Learn optimal settings for Postfix, P...