簡體   English   中英

無法通過 Python 發送 email

[英]Cannot to sent email via Python

我嘗試使用 gmail Z787C75233B93AA5E45C3F85D130BFBE7 通過 python 發送 email,但接收錯誤:

代碼:

import smtplib

FROM = "mail1@gmail.com"
TO = "mail2@gmail.com"

message = "Hello"
# Send the mail

server = smtplib.SMTP('smtp.gmail.com', 465)
server.ehlo()
server.starttls()
server.login('mail1@gmail.com', 'password')
server.sendmail(FROM, TO, message)
server.quit()

回復:

server = smtplib.SMTP('smtp.gmail.com', 465)
  File "C:\Python37\lib\smtplib.py", line 251, in __init__
    (code, msg) = self.connect(host, port)
  File "C:\Python37\lib\smtplib.py", line 338, in connect
    (code, msg) = self.getreply()
  File "C:\Python37\lib\smtplib.py", line 394, in getreply
    raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed

根據 python 文檔中的示例https://docs.python.org/3/library/smtplib.ZFC35FEDC70D5FC69D5Z6

您的代碼缺少“發件人:和收件人:”標頭

import smtplib

def prompt(prompt):
    return input(prompt).strip()

fromaddr = prompt("From: ")
toaddrs  = prompt("To: ").split()
print("Enter message, end with ^D (Unix) or ^Z (Windows):")

# Add the From: and To: headers at the start!
msg = ("From: %s\r\nTo: %s\r\n\r\n"
       % (fromaddr, ", ".join(toaddrs)))
while True:
    try:
        line = input()
    except EOFError:
        break
    if not line:
        break
    msg = msg + line

print("Message length is", len(msg))
server = smtplib.SMTP('localhost')
server.set_debuglevel(1)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()

運行上面的例子,然后修改你發布的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM