这是我用来通过 GoDaddy 发送电子邮件的代码:
import smtplib
server = smtplib.SMTP('smtpout.secureserver.net', 465)
server.starttls()
server.ehlo()
server.login("username", "password")
msg = "Please work!!!!!!"
fromaddr = "fromemail"
toaddr = "toemail"
server.sendmail(fromaddr, toaddr, msg)
Run Code Online (Sandbox Code Playgroud)
运行脚本时,我收到此错误:
Traceback (most recent call last):
File "emailTest.py", line 3, in <module>
server = smtplib.SMTP('smtpout.secureserver.net', 465)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 250, in __init__
(code, msg) = self.connect(host, port)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 311, in connect
(code, msg) = self.getreply()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 362, in getreply
raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed
Run Code Online (Sandbox Code Playgroud)
我真的很迷茫,我知道我的登录信息是正确的。
替换这两行:
server = smtplib.SMTP('smtpout.secureserver.net', 465)
server.starttls()
Run Code Online (Sandbox Code Playgroud)
与这两个:
server = smtplib.SMTP_SSL('smtpout.secureserver.net', 465)
#server.starttls()
Run Code Online (Sandbox Code Playgroud)
引用文档:
SMTP_SSL 应用于从连接开始就需要 SSL 且不适合使用 starttls() 的情况。
使用端口 465 就是其中一种情况。SMTP.starttls()当您使用端口 25 或端口 587 时适用。
参考:
小智 5
如果有人对符合 RFC 5322 的电子邮件有问题,这最终对我有用:
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
msg = MIMEMultipart()
msg.set_unixfrom('author')
msg['From'] = 'you@mail.com'
msg['To'] = 'them@mail.com'
msg['Subject'] = 'simple email in python'
message = 'here is the email'
msg.attach(MIMEText(message))
mailserver = smtplib.SMTP_SSL('smtpout.secureserver.net', 465)
mailserver.ehlo()
mailserver.login('yourgodaddy@mail.com', 'PASSWORD')
mailserver.sendmail('you@mail.com','them@mail.com',msg.as_string())
mailserver.quit()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3960 次 |
| 最近记录: |