title: How to correctly configure Gmail to send E-mail with Django. The official configuration does not work for me
body: <p>I am preparing an online portfolio made with Django and one of the features is a form with which you can contact me via E-mail. To verify that this functionality worked correctly, I have been testing it with MailTrap and it works perfectly, now for the production version I want the E-mails to reach my Gmail mail, but despite the fact that in theory I have followed all the steps, no I get it to work, it only worked for me with MailTrap.</p>
  <p><strong>In my Gmail account I already activated two-step verification and created a specific password for the application.</strong> The lower security option apparently does not exist since May of this year.</p>
  <p>This is the configuration of my Settings.py:</p>
  <pre><code>EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
  EMAIL_HOST = &quot;smtp.gmail.com&quot;
  EMAIL_HOST_USER = &quot;my-e-mail@gmail.com&quot;
  EMAIL_HOST_PASSWORD = 'smbumqjiurmqrywn'
  EMAIL_PORT = 587
  EMAIL_USE_TLS = True
  </code></pre>
  <p>This password is just an example, it is just to show how it is set.</p>
  <p>And this is the method to send the E-mail. I am using EmailMessage but with send_mail the result is the same:</p>
  <pre><code>def contact(request):
  contact_form = ContactForm()
  
  if request.method == 'POST':
      contact_form = ContactForm(data = request.POST)
  
      if contact_form.is_valid():
          name = request.POST.get('name', '')
          email = request.POST.get('email', '')
          content = request.POST.get('content', '')
  
          # Send E-mail.
          email = EmailMessage(
              'New message from {} &lt;{}&gt;'.format(name, email),
              'From {} &lt;{}&gt;\n\nWrote:\n\n{}'.format(name, email, content),
              email,
              [settings.EMAIL_HOST_USER]
          )
          
          email.send()
          
  return render(request, 'core/contact.html', {'form' : contact_form})
  </code></pre>
  <p>As I said, if I change the credentials in Settings.py to MailTrap it works without changing anything else, but Gmail doesn't and I don't know where the problem could be.</p>
  <p>At the end of the method, I put the email.send() inside a try to know more easily when it failed and when it didn't, with the Gmail configuration it always enters the except.</p>
  <p>I have entered the section where the specific passwords are created in my Gmail account several times after trying to send an E-mail and the part where it says &quot;Last use&quot; is empty, I do not know if it is related to the problem.</p>
  <p>Traceback:</p>
  <pre><code>Environment:
  
  Request Method: POST
  Request URL: http://127.0.0.1:8000/contact-me/
  
  Django Version: 3.2.7
  Python Version: 3.9.6
  Installed Applications:
  ['django.contrib.admin',
   'django.contrib.auth',
   'django.contrib.contenttypes',
   'django.contrib.sessions',
   'django.contrib.messages',
   'django.contrib.staticfiles',
   'core',
   'portfolio.apps.PortfolioConfig']
  Installed Middleware:
  ['django.middleware.security.SecurityMiddleware',
   'django.contrib.sessions.middleware.SessionMiddleware',
   'django.middleware.common.CommonMiddleware',
   'django.middleware.csrf.CsrfViewMiddleware',
   'django.contrib.auth.middleware.AuthenticationMiddleware',
   'django.contrib.messages.middleware.MessageMiddleware',
   'django.middleware.clickjacking.XFrameOptionsMiddleware']
  
  Traceback (most recent call last):
    File &quot;C:\Python\Python39\lib\site-packages\django\core\handlers\exception.py&quot;, line 47, in inner
      response = get_response(request)
    File &quot;C:\Python\Python39\lib\site-packages\django\core\handlers\base.py&quot;, line 181, in _get_response
      response = wrapped_callback(request, *callback_args, **callback_kwargs)
    File &quot;C:\Users\PC CASE Gerardo\OneDrive\Documents\Proyectos\Python\webpersonal\core\views.py&quot;, line 34, in contact
      email.send()
    File &quot;C:\Python\Python39\lib\site-packages\django\core\mail\message.py&quot;, line 284, in send
      return self.get_connection(fail_silently).send_messages([self])
    File &quot;C:\Python\Python39\lib\site-packages\django\core\mail\backends\smtp.py&quot;, line 102, in send_messages
      new_conn_created = self.open()
    File &quot;C:\Python\Python39\lib\site-packages\django\core\mail\backends\smtp.py&quot;, line 67, in open
      self.connection.starttls(keyfile=self.ssl_keyfile, certfile=self.ssl_certfile)
    File &quot;C:\Python\Python39\lib\smtplib.py&quot;, line 764, in starttls
      self.ehlo_or_helo_if_needed()
    File &quot;C:\Python\Python39\lib\smtplib.py&quot;, line 607, in ehlo_or_helo_if_needed
      (code, resp) = self.helo()
    File &quot;C:\Python\Python39\lib\smtplib.py&quot;, line 436, in helo
      (code, msg) = self.getreply()
    File &quot;C:\Python\Python39\lib\smtplib.py&quot;, line 400, in getreply
      raise SMTPServerDisconnected(&quot;Connection unexpectedly closed&quot;)
  
  Exception Type: SMTPServerDisconnected at /contact-me/
  Exception Value: Connection unexpectedly closed
  </code></pre>