title: send mail python asyncio
body: <p>I'm trying to learn asyncio. If I run this program normally without the asyncio library than it takes less time while it takes more time in this way so is this the right way to send mail using asyncio or there is any other way?</p>
  <pre><code>import smtplib 
  import ssl
  import time
  import asyncio
  
  
  async def send_mail(receiver_email):
      try:
          print(f&quot;trying..{receiver_email}&quot;)
          server = smtplib.SMTP(smtp_server, port)
          server.ehlo()
          server.starttls(context=context)
          server.ehlo()
          server.login(sender_email, password)
          message = &quot;test&quot;
          await asyncio.sleep(0)
          server.sendmail(sender_email, receiver_email, message)
          print(f&quot;done...{receiver_email}&quot;)
      except Exception as e:
          print(e)
      finally:
          server.quit()
  
  async def main():
       t1 = time.time()
       await asyncio.gather(
           send_mail(&quot;test@test.com&quot;),
           send_mail(&quot;test@test.com&quot;),
           send_mail(&quot;test@test.com&quot;),
           send_mail(&quot;test@test.com&quot;)
       )
      print(f&quot;End in {time.time() - t1}sec&quot;)
  
  if __name__ == &quot;__main__&quot;:
       smtp_server = &quot;smtp.gmail.com&quot;
       port = 587  # For starttls
       sender_email = &quot;*****&quot;
       password = &quot;*****&quot;
       context = ssl.create_default_context()
       asyncio.run(main())
  </code></pre>