简体   繁体   中英

Getting error while sending email using Python

When I try sending an email from Python I get an error message. By the way I made less secure configuration and IMAP Enable on gmail account. Here is the code below:

import smtplib

sender_email="example@gmail.com"
receveir_email="example2@gmail.com"
password=input("please enter your password")
message="this email  from python "

server=smtplib.SMTP_SSL('smptp.gmail.com',587)

server.ehlo
server.starttls()
server.login(sender_email,password)
print("login success")
server.sendmail(sender_email,receveir_email,message)
print("email has been sent to",receveir_email)
server.close()

My error message:

Traceback (most recent call last):
  File "C:/Users/Administrator/PycharmProjects/codebook/Training0718.py", line 23, in <module>
    server=smtplib.SMTP_SSL('smptp.gmail.com',465)
  File "C:\Program Files\Python38\lib\smtplib.py", line 1034, in __init__
    SMTP.__init__(self, host, port, local_hostname, timeout,
  File "C:\Program Files\Python38\lib\smtplib.py", line 253, in __init__
    (code, msg) = self.connect(host, port)
  File "C:\Program Files\Python38\lib\smtplib.py", line 339, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "C:\Program Files\Python38\lib\smtplib.py", line 1040, in _get_socket
    new_socket = socket.create_connection((host, port), timeout,
  File "C:\Program Files\Python38\lib\socket.py", line 787, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "C:\Program Files\Python38\lib\socket.py", line 918, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed

i fixed like this
server=smtplib.SMTP_SSL('smtp.gmail.com',587) but still i am getting error message like below

Traceback (most recent call last):
  File "C:/Users/Administrator/PycharmProjects/codebook/Training0718.py", line 23, in <module>
    server=smtplib.SMTP_SSL('smtp.gmail.com',587)
  File "C:\Program Files\Python38\lib\smtplib.py", line 1034, in __init__
    SMTP.__init__(self, host, port, local_hostname, timeout,
  File "C:\Program Files\Python38\lib\smtplib.py", line 253, in __init__
    (code, msg) = self.connect(host, port)
  File "C:\Program Files\Python38\lib\smtplib.py", line 339, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "C:\Program Files\Python38\lib\smtplib.py", line 1042, in _get_socket
    new_socket = self.context.wrap_socket(new_socket,
  File "C:\Program Files\Python38\lib\ssl.py", line 500, in wrap_socket
    return self.sslsocket_class._create(
  File "C:\Program Files\Python38\lib\ssl.py", line 1040, in _create
    self.do_handshake()
  File "C:\Program Files\Python38\lib\ssl.py", line 1309, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1123)

I was able to make it work with the following, note that this will only succeed if you have created and are using an app password

import smtplib


password = input('please enter your password')

to = 'example2@gmail.com'
_from = 'example@gmail.com'
message = 'hello'

try:
    server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
    server.ehlo()
    server.login(_from, password)
    server.sendmail(_from, to, message)
    server.close()

except Exception as err:
    print(err)

First of all, Gmail smtp address is "smtp.gmail.com" not "smptp.gmail.com" .

Secondly, you have to choose whether you will use SSL or TLS, you can't do both. If you choose SSL, you should use port 465 and remove:

server.starttls()

If you choose TLS, replace:

server=smtplib.SMTP_SSL('smptp.gmail.com',587)

By:

server=smtplib.SMTP('smptp.gmail.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