Last updated: Apr 13, 2024
Reading timeยท3 min
The Python "ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1002)" occurs when you incorrectly specify the port number when using SSL.
To solve the error, set the port to 465
when using SSL.
Here is an example of how the error occurs.
import smtplib # ๐๏ธ Port set incorrectly (must be 465) port = 587 smtp_server = 'smtp.gmail.com' sender_email = 'sender@gmail.com' receiver_email = "receiver@gmail.com" password = 'dogsandcats123' message = '''\ Subject: Your Subject Your message contents.''' with smtplib.SMTP_SSL(smtp_server, port) as server: server.login(sender_email, password) server.sendmail( sender_email, receiver_email, message ) server.quit()
I set the port to 587
when using the smtplib.SMTP_SSL()
class which caused
the error.
When connecting to an SMTP server with SSL encryption, set the port to 465
.
import smtplib # โ Port correctly set to 465 port = 465 smtp_server = 'smtp.gmail.com' sender_email = 'sender@gmail.com' receiver_email = "receiver@gmail.com" password = 'dogsandcats123' message = '''\ Subject: Your Subject Your message contents.''' with smtplib.SMTP_SSL(smtp_server, port) as server: server.login(sender_email, password) server.sendmail( sender_email, receiver_email, message ) server.quit()
Set the port to 465
when calling the smtplib.SMTP_SSL()
class to solve the
error.
The smtplib.SMTP_SSL
class takes the following 2 arguments:
host
- the name of the remote host to which to connect.port
- the port to which to connect.When you use SSL encryption on port 465
, you might run into issues, e.g. your
mail being processed as spam.
The 465
port has recently been deprecated and its use is not recommended.
Instead, it is better to use TLS over port 587
.
import smtplib import ssl # โ Set port to 587 port = 587 smtp_server = 'smtp.gmail.com' sender_email = 'sender@gmail.com' receiver_email = "receiver@gmail.com" password = 'dogsandcats123' message = '''\ Subject: Your Subject Your message contents.''' # โ Create the SSLContext object context = ssl.create_default_context() # โ Use smtplib.SMTP() class with smtplib.SMTP(smtp_server, port) as server: # โ Put the connection into TLS mode server.starttls(context=context) server.login(sender_email, password) server.sendmail( sender_email, receiver_email, message ) server.quit()
Notice that we set the port
variable to 587
.
The port is used for email submissions by a mail client to a mail server.
Port 587
requires authentication and encryption, so your mail is less likely
to end in the spam folder.
The ssl.create_default_context()
method creates an SSLContext
object with
the default settings.
context = ssl.create_default_context()
Notice that we used the smtplib.SMTP
class and not smtplib.SMTP_SSL
.
with smtplib.SMTP(smtp_server, port) as server:
The server.starttls()
method call puts the connection into TLS mode.
server.starttls(context=context)
requests
If you got the error when using requests
or urllib3
, try to
upgrade your urllib3
version.
pip install urllib3 --upgrade # Or with pip3 pip3 install urllib3 --upgrade
The error is caused because of a bug in an older urllib3
version.
Another thing you can try if you get the error when using the requests
package
with a proxy, set the https
proxy URL to use the http
protocol.
import requests # ๐๏ธ Notice that it uses http:// protocol https_proxy = "http://127.0.0.1:5000" proxies = { "http": https_proxy, } request = requests.get( 'https://www.google.com', verify=False, proxies=proxies )
Notice that the https
proxy uses the http
protocol.
This is necessary because of a bug in urllib3
.
If you got the error when using Django, make sure:
"django.core.mail.backends.smtp.EmailBackend"
.EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
The line should be in your settings.
EMAIL_USE_TLS
to True
, then set EMAIL_PORT
to 587
.EMAIL_USE_TLS = True EMAIL_USE_SSL = False EMAIL_PORT = 587
EMAIL_USE_SSL
to True
, then set EMAIL_PORT
to 465
.EMAIL_USE_SSL = True EMAIL_USE_TLS = False EMAIL_PORT = 465
Make sure that only one of EMAIL_USE_TLS
and EMAIL_USE_SSL
is set to True
and set the corresponding port.
The port for TLS is 587
and the port for SSL is 465
.
The error occurs if you get the port number wrong.
You can learn more about the related topics by checking out the following tutorials: