简体   繁体   中英

How to fix the error ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number?

I am trying to send email using python but i am getting error while sending the email, i have also tried with both the commands (with python2 and python3) while executing the code

import smtplib,ssl

content= "prince kumar"

mail = smtplib.SMTP_SSL('gmail-smtp-in.l.google.com', 25)
#mail.ehlo()

#mail.starttls()

mail.login('kmrprncanu@gmail.com','83412adsf234sad')

mail.sendmail('kmrprncanu@gmail.com', 'kmrprnc44@gmail.com',content)

mail.close()

Error:

  root@localhost:~# python smtp.py 
   Traceback (most recent call last):
 File "smtp.py", line 6, in <module>
mail = smtplib.SMTP_SSL('smtp.gmail.com', 587)
File "/usr/lib/python2.7/smtplib.py", line 802, in __init__
 SMTP.__init__(self, host, port, local_hostname, timeout)
File "/usr/lib/python2.7/smtplib.py", line 256, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python2.7/smtplib.py", line 317, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python2.7/smtplib.py", line 808, in _get_socket
new_socket = ssl.wrap_socket(new_socket, self.keyfile, self.certfile)
File "/usr/lib/python2.7/ssl.py", line 949, in wrap_socket
ciphers=ciphers)
File "/usr/lib/python2.7/ssl.py", line 617, in __init__
  self.do_handshake()
 File "/usr/lib/python2.7/ssl.py", line 846, in do_handshake
self._sslobj.do_handshake()
 ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:726)

It looks like the error is stemming from the wrong use of a port number.

You need to change the port from 25 to 465 or 587(ideally) to use SMTP with SSL; port 25 is often used for non-encrypted email. So apply the change to this line and it should solve your problem.

mail = smtplib.SMTP_SSL('gmail-smtp-in.l.google.com', 587)

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