尝试使用Python通过GoDaddy发送电子邮件

时间:2014-07-28 16:01:31

标签: python email smtp

以下是我用来通过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)

运行脚本时,出现此错误:

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

我真的迷失了这个,我知道我的登录信息是正确的。

4 个答案:

答案 0 :(得分:3)

替换这两行:

server = smtplib.SMTP('smtpout.secureserver.net', 465)
server.starttls()

这两个:

server = smtplib.SMTP_SSL('smtpout.secureserver.net', 465)
#server.starttls()

引用the doc

  
    

SMTP_SSL应该用于从连接开始需要SSL并且使用starttls()不合适的情况。

  

使用端口465就是其中一种情况。使用端口25或端口587时,SMTP.starttls()是合适的。

参考文献:

答案 1 :(得分:3)

如果有人对符合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()

答案 2 :(得分:1)

Joe的代码对我有用。我唯一需要更改的是导入语句:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

然后一切正常!谢谢,乔。

(对不起,我无法对此发表评论或对其进行投票-我的声誉还不够高,无法发表评论。)

答案 3 :(得分:0)

这是有效的整体代码。

import smtplib`enter code here`
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

msg = MIMEMultipart()
msg.set_unixfrom('author')
msg['From'] = 'your email'
msg['To'] = 'xyz@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('your email', 'password')
response = mailserver.sendmail('from@mail.com','to@mail.com',msg.as_string())
mailserver.quit()