How To Solve The Error “ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] Wrong Version Number (_ssl.c:1056)” In Python

How To Solve The Error “ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] Wrong Version Number (_ssl.c:1056)” In Python

If you don’t know how to fix the Error “ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] Wrong Version Number (_ssl.c:1056)” In Python, this article is for you. Follow the article for more information.

How does the “ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1056)” error occur?

The error occurs when you are trying to send an email using a script, you end up with an error. Let’s check out the code:

Example code:

import smtplib, ssl

server = smtplib.SMTP_SSL('smtp.mail.com', 587)
server.login("[email protected]", "password")
server.sendmail("[email protected]",
                "[email protected]",
                "email_messages") 
     
server.quit()

Output:

ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1056)

Now that you know how you get the error warning. Next we will move to the solution to fix the error.

Solutions to fix this error

Solution 1: Use a valid key for the dictionary

The SSL port is not 587, the correct one is 456. Your email will go to the junk if you are using SSL. Check out the following code to send mail.

Example code:

import smtplib, ssl
port = 587
smtp_server = "smtp.gmail.com"
sender = "[email protected]"
receiver = "[email protected]"
password = "your smtp email password"
message = "this message you want to send"
context = ssl.create_default_context()
with smtplib.SMTP(smtp_server, port) as server:
    server.ehlo()
    server.starttls(context=context)
    server.ehlo()
    server.login(sender, password)
    server.sendmail(sender, receiver, message)

Solution 2: Mailbox Configuration

The cause of the error is the mail server connection error. Follow the below code to get the issue solved: 

smtplib.SMTP(self.host, self.port, timeout = 300) # Use when TLS is turned off

And you can also do this:

smtplib.SMTP(self.host, self.port, timeout = 300) # Use when TLS is turned off

For the 163 configuration of the mailbox, use the following code:

smtplib.SMTP_SSL(self.host, self.port, timeout = 300). # Applied when TLS is activated

Solution 3: Trace mode

If the traceback is the issue that is causing the error to pop up, you need to run your program in the trace mode and report the output.

Example code:

$ HTTPX_LOG_LEVEL = trace python program.py

This solution is also an effective yet simple one to fix the SSL error.

Summary

In this tutorial, I showed you how to solve the error “ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1056)” in Python. It is on you to use the solution that is suitable for the program you are working on. Let’s follow the steps of the article to solve the error.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *