title: Django: send email via Office365
body: <p><strong>What I have:</strong></p>
  
  <ul>
  <li>A domain purchased in webs.com (I'll call it contoso.com);</li>
  <li>An Office 365 account connected to that domain (my email address is something like <code>john@contoso.com</code>);</li>
  <li>A Django application hosted in Azure;</li>
  </ul>
  
  <p><strong>What I want:</strong></p>
  
  <p>I would like to configure Django in order to send emails using my <code>john@contoso.com</code> address.</p>
  
  <p><strong>What I'v done:</strong></p>
  
  <p>I tried this Django configuration:</p>
  
  <pre><code>EMAIL_USE_TLS = True
  EMAIL_HOST = 'smtp.office365.com'
  EMAIL_PORT = 587
  EMAIL_HOST_USER = 'john@contoso.com'
  EMAIL_HOST_PASSWORD = 'xxxxxx'
  </code></pre>
  
  <p>but it didn't work. So, just for a test, I tried the free Google SMTP server:</p>
  
  <pre><code>EMAIL_USE_TLS = True
  EMAIL_HOST = 'smtp.gmail.com'
  EMAIL_PORT = 587
  EMAIL_HOST_USER = 'john@gmail.com'
  EMAIL_HOST_PASSWORD = 'yyyyyy'
  </code></pre>
  
  <p>This time it works but, obviously, the email is sent from <code>john@gmail.com</code> and not from <code>john@contoso.com</code>.</p>
  
  <p>What's wrong in the previous configuration? Or maybe I need to make some changes in my Office 365 account?</p>
  
  <hr>
  
  <h2>Update</h2>
  
  <p>So far I've tried to receive emails by setting Django's ADMINS variable in this way:</p>
  
  <pre><code>ADMINS = [('Admin Name', 'admin@gmail.com')]
  </code></pre>
  
  <p>And then I send the email using the logging object:</p>
  
  <pre><code>logging.error("Hello admin!!")
  </code></pre>
  
  <p>As said, this works only when I use <code>smtp.gmail.com</code>. Today I tried to use the <code>send_mail</code> function:</p>
  
  <pre><code>send_mail(
      'Subject here',
      'Here is the message.',
      'john@gmail.com',
      ['mail@gmail.com'],
      fail_silently=False,
  )
  </code></pre>
  
  <p>And it works both using <code>smtp.gmail.com</code> and <code>smtp.office365.com</code> configuration. Unfortunatelly this can't be a valid solution for me since I need to notify the admin in case of an error or an exception. So, why the <code>send_mail</code> function works with <code>smtp.office365.com</code> and the <code>ADMIN</code> setting don't?</p>