[英]getting error in sending email through python
當我嘗試使用host:cpanel.freehosting.com發送電子郵件時,它會引發類似以下錯誤:
這是我的代碼:
import smtplib
s = smtplib.SMTP('cpanel.freehosting.com', 465)
s.starttls()
s.login("myusername", "mypassword")
message = "Message_you_need_to_send"
s.sendmail("myemailid", "receiver_email_id", message)
s.quit()
這是我得到的錯誤:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.5/smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python3.5/smtplib.py", line 337, in connect
(code, msg) = self.getreply()
File "/usr/lib/python3.5/smtplib.py", line 393, in getreply
raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed
我可以幫忙嗎...
考慮到您正在使用的端口號,我將嘗試使用SMTP_SSL
而不是SMTP
和starttls()
。
https://docs.python.org/3/library/smtplib.html :
SMTP_SSL實例的行為與SMTP實例完全相同。 SMTP_SSL應該用於從連接開始就需要SSL且不適合使用starttls()的情況。 如果未指定host,則使用本地主機。 如果端口為零,則使用標准的SMTP over SSL端口(465)。
STARTTLS
是機會TLS的一種形式,它應該與最初不支持TLS的舊協議一起使用,以升級連接。 在引入SMTPS
的STARTTLS
之前已使用端口465,現在已棄用該端口。
import smtplib
s = smtplib.SMTP_SSL('cpanel.freehosting.com', 465)
s.login("myusername", "mypassword")
message = "Message_you_need_to_send"
s.sendmail("myemailid", "receiver_email_id", message)
s.quit()
或者,您應該能夠將端口25與原始代碼一起使用。
import smtplib
s = smtplib.SMTP('cpanel.freehosting.com', 25)
s.starttls()
s.login("myusername", "mypassword")
message = "Message_you_need_to_send"
s.sendmail("myemailid", "receiver_email_id", message)
s.quit()
在這兩個示例中,您都可以完全忽略端口號,就像使用默認端口一樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.