Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SMTP Python连接意外关闭_Python_Email_Smtplib - Fatal编程技术网

SMTP Python连接意外关闭

SMTP Python连接意外关闭,python,email,smtplib,Python,Email,Smtplib,大家好,在我的问题页面上 所以我想创建一个程序,用我每小时发一封垃圾邮件来自动给自己发电子邮件,只是为了学习一些很酷的python库,但是当我运行这个程序时,它给了我一个错误。错误就在我的代码下面。我已经封锁了密码,因为它是私人的,是的。在你们说这是因为密码错误之前,并不是因为如果密码错误,它会说“身份验证失败” 我的代码: #imports import smtplib port = 465 smtp_server = "mail.gmx.com" email = "kbodfkghset

大家好,在我的问题页面上

所以我想创建一个程序,用我每小时发一封垃圾邮件来自动给自己发电子邮件,只是为了学习一些很酷的python库,但是当我运行这个程序时,它给了我一个错误。错误就在我的代码下面。我已经封锁了密码,因为它是私人的,是的。在你们说这是因为密码错误之前,并不是因为如果密码错误,它会说“身份验证失败”

我的代码:

#imports
import smtplib

port = 465

smtp_server = "mail.gmx.com"
email = "kbodfkghset232gvja23@gmx.us"
password = "****************"
target = "gerik700@gmail.com"
message = """
Subject: Test

This is a test message
"""

#login to server to send email
server = smtplib.SMTP(host=smtp_server, port=port)
try:
    server.starttls()
    server.login(email, password)
    server.set_debuglevel(1)
    server.ehlo()
    #sending message
    server.sendmail(email, target, message)


except Exception as e:
    print(e)
finally:
    server.quit()
我的错误:

Traceback (most recent call last):
  File "Email_Program.py", line 17, in <module>
    server = smtplib.SMTP(host=smtp_server, port=port)
  File "C:\Program Files (x86)\Python37-32\lib\smtplib.py", line 251, in __init__
    (code, msg) = self.connect(host, port)
  File "C:\Program Files (x86)\Python37-32\lib\smtplib.py", line 338, in connect
    (code, msg) = self.getreply()
  File "C:\Program Files (x86)\Python37-32\lib\smtplib.py", line 394, in getreply
    raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed
回溯(最近一次呼叫最后一次):
文件“Email_Program.py”,第17行,在
服务器=smtplib.SMTP(主机=SMTP_服务器,端口=port)
文件“C:\Program Files(x86)\Python37-32\lib\smtplib.py”,第251行,在\uuu init中__
(代码,消息)=自连接(主机,端口)
文件“C:\ProgramFiles(x86)\Python37-32\lib\smtplib.py”,第338行,在connect中
(代码,msg)=self.getreply()
getreply中第394行的文件“C:\ProgramFiles(x86)\Python37-32\lib\smtplib.py”
升起SMTPServerDisconnected(“连接意外关闭”)
smtplib.SMTPServerDisconnected:连接意外关闭
任何帮助都将不胜感激

根据,通过mail.gmx.com发送邮件的客户应连接端口587并使用
starttls
。因此,首先尝试更改脚本以连接端口587而不是端口465。另外,您需要在
starttls
命令之前发送
ehlo
命令,然后在
starttls
命令之后再次发送。以下方面应起作用:

import smtplib

to='to@to.com'
fromname='sender'
fromemail='from@from.com'
subject='this is the subject'
body='this is the message body'

message=''
message+= "To: " + to + "\n"
message+= "From: \"" + fromname + "\" <" + fromemail + ">\n"
message+= "Subject: " + subject + "\n"
message+= "\n"
message+= body

mailserver = smtplib.SMTP('mail.gmx.com',587)
mailserver.ehlo()
mailserver.starttls()
mailserver.ehlo()  #again
mailserver.login('username', 'password')
mailserver.sendmail(fromemail, to, message)
mailserver.quit()
导入smtplib
到to@to.com'
fromname='sender'
来自电子邮件from@from.com'
主题='这是主题'
body='这是消息正文'
消息=“”
消息+=“收件人:”+收件人+“\n”
消息+=“发件人:\”“+fromname+\”\n
消息+=“主题:”+Subject+“\n”
消息+=“\n”
消息+=正文
mailserver=smtplib.SMTP('mail.gmx.com',587)
mailserver.ehlo()
mailserver.starttls()
mailserver.ehlo()#再次
mailserver.login('用户名','密码')
mailserver.sendmail(从email、到、消息)
mailserver.quit()

我尝试了上面的代码,它不起作用。正在引发SMTPAuthenticationError:(535,b“身份验证凭据无效”)您能帮助尝试smtplib.SMTP('SMTP.gmail.com',587)吗