当我尝试从Python发送电子邮件时,收到错误消息。顺便说一句,我在Gmail帐户上进行了不太安全的配置和IMAP启用。这是下面的代码:
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()
我的错误消息:
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
答案 0 :(得分:0)
我这样固定
server = smtplib.SMTP_SSL('smtp.gmail.com',587),但我仍然收到如下错误消息
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)
答案 1 :(得分:0)
我能够通过以下方式使其工作,请注意,只有在您创建并使用了应用密码后,该操作才能成功
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)
答案 2 :(得分:-1)
首先,Gmail SMTP地址为“ smtp.gmail.com” ,而不是“ smptp.gmail.com” 。
第二,您必须选择使用SSL还是TLS,但不能两者都选择。 如果选择SSL,则应使用端口465并删除:
server.starttls()
如果选择TLS,请替换:
server=smtplib.SMTP_SSL('smptp.gmail.com',587)
通过:
server=smtplib.SMTP('smptp.gmail.com',587)