简体   繁体   中英

Sending email using Outlook SMTP

I want to send email in Django application using Outlook's SMTP server. The problem is, I get SSL wrong version number error every time I'm trying to send a message.

Error traceback:

Traceback (most recent call last):
File "F:\Development\Python\lib\smtplib.py", line 366, in getreply
    line = self.file.readline()
File "F:\Development\Python\lib\socket.py", line 297, in readinto
    return self._sock.recv_into(b)
File "F:\Development\Python\lib\ssl.py", line 453, in recv_into
    return self.read(nbytes, buffer)
File "F:\Development\Python\lib\ssl.py", line 327, in read
    v = self._sslobj.read(len, buffer)
ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1450)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "F:\Development\Python\lib\site-packages\django\core\handlers\base.py", line 115,   in get_response
    response = callback(request, *callback_args, **callback_kwargs)
File "E:\SkyDrive\Repositories\web\skyproject\views.py", line 13, in index
    email.send()
File "F:\Development\Python\lib\site-packages\django\core\mail\message.py", line 255, in send
    return self.get_connection(fail_silently).send_messages([self])
File "F:\Development\Python\lib\site-packages\django\core\mail\backends\smtp.py", line 88, in send_messages
    new_conn_created = self.open()
File "F:\Development\Python\lib\site-packages\django\core\mail\backends\smtp.py", line 55, in open
    self.connection.login(self.username, self.password)
File "F:\Development\Python\lib\smtplib.py", line 621, in login
    AUTH_PLAIN + " " + encode_plain(user, password))
File "F:\Development\Python\lib\smtplib.py", line 398, in docmd
    return self.getreply()
File "F:\Development\Python\lib\smtplib.py", line 370, in getreply
+ str(e))

smtplib.SMTPServerDisconnected: Connection unexpectedly closed: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1450)

This is my SMTP configuration in 'settings.py':

EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.live.com'
EMAIL_HOST_USER = 'my_email@outlook.com'
EMAIL_HOST_PASSWORD = 'my_password'
EMAIL_PORT = 587

And this is how messages being sent:

from django.core.mail import EmailMessage
email = EmailMessage('Test', 'Test', to=['email_address@example.com'])
email.send()

I have no idea why I get this error. As far as I know, there are no SSL_VERSION parameter in Django settings.

If it is important, my interpreter's version is 3.3.2 , and Django's version is 1.5.2 .

I got it working with the following settings:

EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp-mail.outlook.com'
EMAIL_HOST_USER = 'myemail@outlook.com'
EMAIL_HOST_PASSWORD = 'mypassword'
EMAIL_PORT = 25

I had a very similar issue. I was getting following error message:

SMTPServerDisconnected: Connection unexpectedly closed

The solution was to:

  1. Login to the e-mail address via web interface available at www.outlook.com
  2. Verify my account by providing MS with my phone number and typing back the received SMS.

After this I can nicely send e-mails from Django.

Try these settings for OutLook:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp-mail.outlook.com'
EMAIL_HOST_USER = "youremail@outlook.com"
EMAIL_HOST_PASSWORD = "yourpassword"

I am getting an error while sending a Django email using outlook, did you send it successfully recently?

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_PORT = 995
EMAIL_USE_TLS = True
EMAIL_HOST = 'outlook.office365.com'
EMAIL_HOST_USER = '********@*******'
EMAIL_HOST_PASSWORD = '************'
EMAIL_FROM_ADDRESS='*****@******'
EMAIL_ENCRYPTION='STARTTLS'
DEFAULT_FROM_EMAIL ='*****@********'

Due to the cancellation of Gmail's less secure apps, I tried outlook. Here is the code:

# setting.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp-mail.outlook.com'
EMAIL_HOST_USER = "your_account@outlook.com"
EMAIL_HOST_PASSWORD = "password"

You could test it in shell:

python manage.py shell

>>> from django.core.mail import send_mail
>>> send_mail("TEST","THis is a test","your_account@outlook.com",["another_account@gmail.com"],fail_silently=False)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM